public static void WriteImport(CodeTextBuilder code, ImportDirective import) { code.Write("import "); if (import.AsDefault != null) { code.Write(import.AsDefault.Name); } else if (import.AsNamespace != null) { code.Write($"* as {import.AsNamespace.Name}"); } else if (import.AsTuple != null) { JavaScriptExpressionWriter.WriteTuple(code, import.AsTuple); } if (import.From != null) { code.Write($" from '{import.From.GetModulePath()}'"); } else if (import.What != null) { code.Write($"'{import.What.GetModulePath()}'"); } code.WriteLine(";"); }
private static void WriteReturn(CodeTextBuilder code, ReturnStatement statement) { code.Write("return"); if (statement.Expression != null) { code.Write(" "); JavaScriptExpressionWriter.WriteExpression(code, statement.Expression); } }
private static void WriteVariableDeclaration(CodeTextBuilder code, VariableDeclarationStatement statement) { code.Write(statement.Variable.IsFinal ? "const " : "let "); code.Write(statement.Variable.Name); if (statement.InitialValue != null) { code.Write(" = "); JavaScriptExpressionWriter.WriteExpression(code, statement.InitialValue); } }
private static void WriteIf(CodeTextBuilder code, IfStatement statement) { code.Write("if ("); JavaScriptExpressionWriter.WriteExpression(code, statement.Condition); code.WriteLine(") "); WriteBlockInsideBraces(code, statement.ThenBlock); if (statement.ElseBlock != null) { WriteBlockInsideBraces(code, statement.ElseBlock); } }
public static void WriteArrowFunction(CodeTextBuilder code, MethodSignature signature, BlockStatement body) { if (signature.IsAsync) { code.Write("async "); } if (signature.Parameters.Count != 1) { WriteParameters(code, signature); } else { code.Write($"{signature.Parameters[0].Name} "); } code.Write("=> "); switch (body.Statements.Count) { case 0: code.Write("{ }"); break; case 1: if (body.Statements[0] is ReturnStatement @return) { var needParentheses = (@return.Expression is ObjectInitializerExpression init && init.PropertyValues.Count > 1); code.WriteIf(needParentheses, "("); JavaScriptExpressionWriter.WriteExpression(code, @return.Expression); code.WriteIf(needParentheses, ")"); } else { // code.WriteLine(); // code.Indent(1); JavaScriptStatementWriter.WriteStatement(code, body.Statements[0]); // code.WriteLine(); // code.Indent(-1); } break; default: WriteBody(code, body); break; } }
private static void WriteParameters(CodeTextBuilder code, MethodSignature signature) { code.WriteListStart(opener: "(", separator: ", ", closer: ") "); signature.Parameters.ForEach(parameter => { code.WriteListItem(); if (parameter.Tuple != null) { JavaScriptExpressionWriter.WriteTuple(code, parameter.Tuple); } else { code.Write(parameter.Name); } }); code.WriteListEnd(); }
private void WriteStartElement(XElement element) { _code.Write($"<{element.Name}"); foreach (var attribute in element.Attributes()) { _code.Write(" "); if (attribute is JsxExpressionAttribute expressionAttribute) { _code.Write(attribute.Name.ToString()); _code.Write("={"); JavaScriptExpressionWriter.WriteExpression(_code, expressionAttribute.Expression); _code.Write("}"); } else { _code.Write(attribute.ToString()); } } _code.Write(element.IsEmpty ? " />" : ">"); }
private static void WriteExpression(CodeTextBuilder code, ExpressionStatement statement) { JavaScriptExpressionWriter.WriteExpression(code, statement.Expression); }