Ejemplo n.º 1
0
 public void ConditionalDoesNotContainEmbeddedSpaces()
 {
     AssertCorrect(JsExpression.Conditional(
                       JsExpression.Number(1),
                       JsExpression.Number(2),
                       JsExpression.Number(3)
                       ),
                   "(1?2:3)");
 }
 public void ConditionalIsAlwaysParenthesized()
 {
     AssertCorrect(JsExpression.Conditional(
                       JsExpression.Number(1),
                       JsExpression.Number(2),
                       JsExpression.Number(3)
                       ),
                   "(1 ? 2 : 3)");
 }
 public void ExpressionNodeTypeIsNotParenthesizedInsideConditional()
 {
     AssertCorrect(JsExpression.Conditional(
                       JsExpression.Unary(ExpressionNodeType.Negate, JsExpression.Number(1)),
                       JsExpression.Unary(ExpressionNodeType.Negate, JsExpression.Number(2)),
                       JsExpression.Unary(ExpressionNodeType.Negate, JsExpression.Number(3))
                       ),
                   "(-1 ? -2 : -3)");
 }
 public void ConditionalIsNotDoublyParenthesized()
 {
     AssertCorrect(JsExpression.Binary(ExpressionNodeType.Add,
                                       JsExpression.Number(1),
                                       JsExpression.Conditional(
                                           JsExpression.Number(2),
                                           JsExpression.Number(3),
                                           JsExpression.Number(4)
                                           )
                                       ),
                   "1 + (2 ? 3 : 4)");
 }
 public void ExpressionStatementIsParenthesizedWhenItStartsWithAnObjectLiteral()
 {
     AssertCorrect((JsStatement)JsExpression.ObjectLiteral(new JsObjectLiteralProperty("a", JsExpression.Number(0))), "({ a: 0 });\n");
     AssertCorrect((JsStatement)JsExpression.Invocation(JsExpression.ObjectLiteral(new JsObjectLiteralProperty("a", JsExpression.Number(0)))), "({ a: 0 }());\n");
     AssertCorrect((JsStatement)JsExpression.Member(JsExpression.ObjectLiteral(new JsObjectLiteralProperty("a", JsExpression.Number(0))), "a"), "({ a: 0 }.a);\n");
     AssertCorrect((JsStatement)JsExpression.Comma(JsExpression.ObjectLiteral(new JsObjectLiteralProperty("a", JsExpression.Number(0))), JsExpression.Null), "({ a: 0 }, null);\n");
     AssertCorrect((JsStatement)JsExpression.Conditional(JsExpression.ObjectLiteral(new JsObjectLiteralProperty("a", JsExpression.Number(0))), JsExpression.Number(0), JsExpression.Number(1)), "({ a: 0 } ? 0 : 1);\n");
     AssertCorrect((JsStatement)JsExpression.PrefixPlusPlus(JsExpression.ObjectLiteral(new JsObjectLiteralProperty("a", JsExpression.Number(0)))), "++{ a: 0 };\n");
     AssertCorrect((JsStatement)JsExpression.PostfixPlusPlus(JsExpression.ObjectLiteral(new JsObjectLiteralProperty("a", JsExpression.Number(0)))), "({ a: 0 }++);\n");
     AssertCorrect((JsStatement)JsExpression.PostfixMinusMinus(JsExpression.ObjectLiteral(new JsObjectLiteralProperty("a", JsExpression.Number(0)))), "({ a: 0 }--);\n");
     AssertCorrect((JsStatement)JsExpression.Index(JsExpression.ObjectLiteral(new JsObjectLiteralProperty("a", JsExpression.Number(0))), JsExpression.Number(0)), "({ a: 0 }[0]);\n");
     AssertCorrect((JsStatement)JsExpression.Member(JsExpression.Invocation(JsExpression.Member(JsExpression.ObjectLiteral(new JsObjectLiteralProperty("a", JsExpression.Number(0))), "a")), "b"), "({ a: 0 }.a().b);\n");
 }
Ejemplo n.º 6
0
        private JsExpression GetFieldHashCode(IField field)
        {
            var impl = _metadataImporter.GetFieldSemantics(field);

            if (impl.Type != FieldScriptSemantics.ImplType.Field)
            {
                return(null);
            }

            IType        type          = NullableType.GetUnderlyingType(field.Type);
            bool         needNullCheck = field.Type.IsReferenceType != false || field.Type.IsKnownType(KnownTypeCode.NullableOfT) || type.Kind == TypeKind.Enum && MetadataUtils.IsNamedValues(field.Type.GetDefinition(), _attributeStore);
            JsExpression member        = JsExpression.Member(JsExpression.This, impl.Name);

            JsExpression result = JsExpression.Invocation(JsExpression.Member(_systemScript, "getHashCode"), member);

            if (needNullCheck)
            {
                result = JsExpression.Conditional(member, result, JsExpression.Number(0));
            }

            if (type.Kind == TypeKind.Enum && !MetadataUtils.IsNamedValues(type.GetDefinition(), _attributeStore))
            {
                result = needNullCheck ? JsExpression.LogicalOr(member, JsExpression.Number(0)) : member;
            }
            else if (type is ITypeDefinition)
            {
                switch (((ITypeDefinition)type).KnownTypeCode)
                {
                case KnownTypeCode.Boolean:
                    result = JsExpression.Conditional(member, JsExpression.Number(1), JsExpression.Number(0));
                    break;

                case KnownTypeCode.Byte:
                case KnownTypeCode.SByte:
                case KnownTypeCode.Char:
                case KnownTypeCode.Int16:
                case KnownTypeCode.UInt16:
                case KnownTypeCode.Int32:
                case KnownTypeCode.UInt32:
                case KnownTypeCode.Int64:
                case KnownTypeCode.UInt64:
                case KnownTypeCode.Decimal:
                case KnownTypeCode.Single:
                case KnownTypeCode.Double:
                    result = needNullCheck ? JsExpression.LogicalOr(member, JsExpression.Number(0)) : member;
                    break;
                }
            }

            return(result);
        }
 public void MultiplicationInConditionalIsParenthesized()
 {
     AssertCorrect(JsExpression.Conditional(
                       JsExpression.Binary(ExpressionNodeType.Multiply,
                                           JsExpression.Number(1),
                                           JsExpression.Number(2)
                                           ),
                       JsExpression.Binary(ExpressionNodeType.Multiply,
                                           JsExpression.Number(3),
                                           JsExpression.Number(4)
                                           ),
                       JsExpression.Binary(ExpressionNodeType.Multiply,
                                           JsExpression.Number(5),
                                           JsExpression.Number(6)
                                           )
                       ),
                   "((1 * 2) ? (3 * 4) : (5 * 6))");
 }
        public virtual JsExpression VisitConditionalExpression(JsConditionalExpression expression, TData data)
        {
            var test      = VisitExpression(expression.Test, data);
            var truePart  = VisitExpression(expression.TruePart, data);
            var falsePart = VisitExpression(expression.FalsePart, data);

            return(ReferenceEquals(test, expression.Test) && ReferenceEquals(truePart, expression.TruePart) && ReferenceEquals(falsePart, expression.FalsePart) ? expression : JsExpression.Conditional(test, truePart, falsePart));
        }