Beispiel #1
0
        private static void WriteObjectInitializer(CodeTextBuilder code, ObjectInitializerExpression expression)
        {
            if (expression.PropertyValues == null || expression.PropertyValues.Count == 0)
            {
                code.Write("{ }");
                return;
            }

            code.WriteListStart(opener: "{", closer: "}", separator: ",", newLine: true);

            foreach (var keyValue in expression.PropertyValues)
            {
                code.WriteListItem();
                code.Write(keyValue.Name.GetSealedOrCased(CasingStyle.Camel, sealLanguage: LanguageInfo.Entries.JavaScript()));

                if (keyValue.Value is IAssignable assignable && assignable.Name == keyValue.Name)
                {
                    continue;
                }

                code.Write(": ");
                WriteExpression(code, keyValue.Value);
            }

            code.WriteListEnd();
        }
Beispiel #2
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(";");
        }
Beispiel #3
0
        private static void WriteMember(CodeTextBuilder code, MemberExpression expression)
        {
            if (expression.Target != null)
            {
                WriteExpression(code, expression.Target);
                code.Write(".");
            }

            code.Write(ToCamelCase(expression.MemberName ?? expression.Member.Name));
        }
Beispiel #4
0
        private static void WriteReturn(CodeTextBuilder code, ReturnStatement statement)
        {
            code.Write("return");

            if (statement.Expression != null)
            {
                code.Write(" ");
                JavaScriptExpressionWriter.WriteExpression(code, statement.Expression);
            }
        }
Beispiel #5
0
 private static void WriteConditional(CodeTextBuilder code, ConditionalExpression expression)
 {
     code.Write("(");
     WriteExpression(code, expression.Condition);
     code.Write(" ? ");
     WriteExpression(code, expression.WhenTrue);
     code.Write(" : ");
     WriteExpression(code, expression.WhenFalse);
     code.Write(")");
 }
Beispiel #6
0
        public void Write(XElement element)
        {
            WriteStartElement(element);

            if (!element.IsEmpty)
            {
                _code.WriteListStart(opener: "", closer: "", separator: "", newLine: true);

                foreach (var child in element.Nodes())
                {
                    _code.WriteListItem();

                    if (child is XElement childElement)
                    {
                        Write(childElement);
                    }
                    else
                    {
                        _code.Write(child.ToString());
                    }
                }

                _code.WriteListEnd();
            }

            WriteEndElement(element);
        }
Beispiel #7
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);
            }
        }
Beispiel #8
0
 public static void WriteLiteral(CodeTextBuilder code, object value)
 {
     if (value == null)
     {
         code.Write("null");
     }
     else if (value is string @string)
     {
         WriteString(code, @string);
     }
     else if (value is bool @bool)
     {
         WriteBool(code, @bool);
     }
     else if (value is int @int)
     {
         WriteInteger(code, @int);
     }
     else if (value is float @float)
     {
         WriteFloat(code, @float);
     }
     else if (value is double @double)
     {
         WriteNumber(code, @double);
     }
     else if (value is Array @array)
     {
         WriteArray(code, @array);
     }
     else
     {
         throw new NotSupportedException($"Literal of type '{value.GetType().Name}' is not supported.");
     }
 }
Beispiel #9
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;
            }
        }
Beispiel #10
0
        private static void WriteModifiers(CodeTextBuilder code, MethodMember method)
        {
            if (method.Modifier == MemberModifier.Static)
            {
                code.Write("static ");
            }

            if (method.IsAsync)
            {
                code.Write("async ");
            }

            if (method.DeclaringType == null)
            {
                code.Write("function ");
            }
        }
Beispiel #11
0
        private static void WriteMethodCall(CodeTextBuilder code, MethodCallExpression call)
        {
            if (call.Target != null)
            {
                WriteExpression(code, call.Target);
                code.Write(".");
            }

            code.Write(ToCamelCase(call.MethodName ?? call.Method.Name));
            code.WriteListStart(opener: "(", separator: ", ", closer: ")");

            foreach (var argument in call.Arguments)
            {
                code.WriteListItem();
                WriteExpression(code, argument.Expression);
            }

            code.WriteListEnd();
        }
Beispiel #12
0
 public static void WriteParameter(CodeTextBuilder code, ParameterExpression expression)
 {
     if (expression.Parameter.Tuple != null)
     {
         WriteTuple(code, expression.Parameter.Tuple);
     }
     else
     {
         code.Write(ToCamelCase(expression.Parameter.Name));
     }
 }
Beispiel #13
0
        public static void WriteFunction(CodeTextBuilder code, MethodMember method)
        {
            WriteModifiers(code, method);
            code.Write(method.Name.GetSealedOrCased(
                           CasingStyle.Camel,
                           sealOrigin: IdentifierName.OriginKind.Generator,
                           sealLanguage: LanguageInfo.Entries.JavaScript()));

            WriteParameters(code, method.Signature);
            WriteBody(code, method.Body);
            code.WriteLine();
        }
Beispiel #14
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);
            }
        }
Beispiel #15
0
        private static void WriteBody(CodeTextBuilder code, BlockStatement body)
        {
            code.WriteLine("{");
            code.Indent(1);

            foreach (var statement in body.Statements)
            {
                JavaScriptStatementWriter.WriteStatementLine(code, statement);
            }

            code.Indent(-1);
            code.Write("}");
        }
Beispiel #16
0
        private static void WriteInterpolatedString(CodeTextBuilder code, InterpolatedStringExpression expression)
        {
            code.Write("`");

            foreach (var part in expression.Parts)
            {
                switch (part)
                {
                case InterpolatedStringExpression.TextPart text:
                    WriteTextPart(text.Text);
                    break;

                case InterpolatedStringExpression.InterpolationPart interpolation:
                    WriteInterpolationPart(interpolation);
                    break;
                }
            }

            code.Write("`");

            void WriteTextPart(string s)
            {
                for (int i = 0; i < s.Length; i++)
                {
                    code.Write(InterpolatedEscapes.TryGetValue(s[i], out var escape)
                        ? escape
                        : string.Empty + s[i]);
                }
            }

            void WriteInterpolationPart(InterpolatedStringExpression.InterpolationPart interpolation)
            {
                code.Write("${");
                WriteExpression(code, interpolation.Value);
                code.Write("}");
            }
        }
Beispiel #17
0
        private static void WriteJsx(CodeTextBuilder code, XmlExpression expression)
        {
            if (expression.Xml == null)
            {
                code.Write("null");
                return;
            }

            var xmlText   = new StringBuilder();
            var jsxWriter = new JsxCodeWriter(code);

            code.WriteListStart(opener: "(", separator: "", closer: ")", newLine: true);
            code.WriteListItem();

            jsxWriter.Write(expression.Xml);

            code.WriteListEnd();
        }
Beispiel #18
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();
        }
Beispiel #19
0
        private static void WriteString(CodeTextBuilder code, string s)
        {
            var result = new StringBuilder(capacity: s.Length + 4);

            result.Append("\"");

            for (int i = 0; i < s.Length; i++)
            {
                if (CharacterEscapes.TryGetValue(s[i], out var escape))
                {
                    result.Append(escape);
                }
                else
                {
                    result.Append(s[i]);
                }
            }

            result.Append("\"");
            code.Write(result.ToString());
        }
Beispiel #20
0
 private static void WriteInteger(CodeTextBuilder code, int value)
 {
     code.Write(value.ToString());
 }
Beispiel #21
0
 private static void WriteBool(CodeTextBuilder code, bool value)
 {
     code.Write(value ? "true" : "false");
 }
Beispiel #22
0
 private static void WriteAwait(CodeTextBuilder code, AwaitExpression expression)
 {
     code.Write("await ");
     WriteExpression(code, expression.Expression);
 }
Beispiel #23
0
        public static void WriteTuple(CodeTextBuilder code, TupleExpression tuple)
        {
            var variableListText = string.Join(", ", tuple.Variables.Select(v => v.Name));

            code.Write($"{{ {variableListText} }}");
        }
Beispiel #24
0
 private static void WriteAssignment(CodeTextBuilder code, AssignmentExpression expression)
 {
     WriteExpression(code, expression.Left.AsExpression());
     code.Write(" = ");
     WriteExpression(code, expression.Right);
 }
Beispiel #25
0
 private static void WriteThis(CodeTextBuilder code, ThisExpression expression)
 {
     code.Write("this");
 }
Beispiel #26
0
 private static void WriteFloat(CodeTextBuilder code, float value)
 {
     code.Write(value.ToString(CultureInfo.InvariantCulture));
 }
Beispiel #27
0
 private static void WriteBinary(CodeTextBuilder code, BinaryExpression expression)
 {
     WriteExpression(code, expression.Left);
     code.Write($" {BinarySyntaxByOperator[expression.Operator]} ");
     WriteExpression(code, expression.Right);
 }
Beispiel #28
0
 private static void WriteNumber(CodeTextBuilder code, double value)
 {
     code.Write(value.ToString(CultureInfo.InvariantCulture));
 }
Beispiel #29
0
        public static void WriteVariable(CodeTextBuilder code, LocalVariableExpression variable)
        {
            var variableName = variable.VariableName ?? variable.Variable.Name;

            code.Write(ToCamelCase(variableName));
        }