예제 #1
0
        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(";");
        }
예제 #2
0
        private static void WriteReturn(CodeTextBuilder code, ReturnStatement statement)
        {
            code.Write("return");

            if (statement.Expression != null)
            {
                code.Write(" ");
                JavaScriptExpressionWriter.WriteExpression(code, statement.Expression);
            }
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
        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);
            }
        }
예제 #5
0
        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;
            }
        }
예제 #6
0
        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();
        }
예제 #7
0
        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 ? " />" : ">");
        }
예제 #8
0
 private static void WriteExpression(CodeTextBuilder code, ExpressionStatement statement)
 {
     JavaScriptExpressionWriter.WriteExpression(code, statement.Expression);
 }