public CSharpSyntaxNode Convert(NewExpression node) { if (node.Arguments.Count == 1 && node.Arguments[0].Kind == NodeKind.ObjectLiteralExpression) { ObjectLiteralExpression literaExpression = node.Arguments[0] as ObjectLiteralExpression; ObjectCreationExpressionSyntax csObjNewExpr = SyntaxFactory.ObjectCreationExpression(node.Type.ToCsNode <TypeSyntax>()); InitializerExpressionSyntax csInitExpr = SyntaxFactory.InitializerExpression(SyntaxKind.ObjectInitializerExpression); foreach (PropertyAssignment prop in literaExpression.Properties) { csInitExpr = csInitExpr.AddExpressions(SyntaxFactory.AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, prop.Name.ToCsNode <ExpressionSyntax>(), prop.Initializer.ToCsNode <ExpressionSyntax>())); } return(csObjNewExpr.WithInitializer(csInitExpr).AddArgumentListArguments()); } if (node.TypeArguments.Count > 0) { return(SyntaxFactory .ObjectCreationExpression(SyntaxFactory .GenericName(this.StripType(node.Type.Text)) .AddTypeArgumentListArguments(node.TypeArguments.ToCsNodes <TypeSyntax>())) .AddArgumentListArguments(this.ToArgumentList(node.Arguments))); } else { return(SyntaxFactory .ObjectCreationExpression(node.Type.ToCsNode <TypeSyntax>()) .AddArgumentListArguments(this.ToArgumentList(node.Arguments))); } }
public void ObjectLiteralExpressionHelpersRequireExpression() { ObjectLiteralExpression expression = null; Expect.Throw <ArgumentNullException>(() => expression.WithProperty("name", "value")); Expect.Throw <ArgumentNullException>(() => expression.WithProperties(new Dictionary <Expression, Expression>())); }
private void InferType(ObjectLiteralExpression node) { if (node.Type == null) { Node type = TypeHelper.GetNodeType(node); node.SetType(type, type.Parent == null); } }
public CSharpSyntaxNode Convert(ObjectLiteralExpression node) { List <Node> properties = node.Properties; Node type = node.Type; if (type.Kind == NodeKind.TypeLiteral && properties.Count >= 2 && (type as TypeLiteral).Members[0].Kind != NodeKind.IndexSignature) { return(SyntaxFactory.TupleExpression().AddArguments(properties.ToCsNodes <ArgumentSyntax>())); } else if (type.Kind == NodeKind.AnyKeyword) { AnonymousObjectCreationExpressionSyntax csAnonyNewExpr = SyntaxFactory.AnonymousObjectCreationExpression(); foreach (PropertyAssignment prop in node.Properties) { string propName = prop.Name.Text; Node initValue = prop.Initializer; ExpressionSyntax valueExpr = initValue.ToCsNode <ExpressionSyntax>(); if (type.Kind == NodeKind.TypeLiteral && initValue.Kind == NodeKind.NullKeyword) { Node memType = TypeHelper.GetTypeLiteralMemberType(type as TypeLiteral, propName); if (memType != null) { valueExpr = SyntaxFactory.CastExpression(memType.ToCsNode <TypeSyntax>(), valueExpr); } } csAnonyNewExpr = csAnonyNewExpr.AddInitializers(SyntaxFactory.AnonymousObjectMemberDeclarator( SyntaxFactory.NameEquals(propName), valueExpr)); } return(csAnonyNewExpr); } else { ObjectCreationExpressionSyntax csObjLiteral = SyntaxFactory.ObjectCreationExpression(type.ToCsNode <TypeSyntax>()).AddArgumentListArguments(); List <ExpressionSyntax> initItemExprs = new List <ExpressionSyntax>(); foreach (PropertyAssignment prop in properties) { ExpressionSyntax csNameExpression = SyntaxFactory.LiteralExpression( SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(prop.Name.Text)); InitializerExpressionSyntax itemInitExpr = SyntaxFactory .InitializerExpression(SyntaxKind.ComplexElementInitializerExpression) .AddExpressions(csNameExpression, prop.Initializer.ToCsNode <ExpressionSyntax>()); initItemExprs.Add(itemInitExpr); } if (initItemExprs.Count > 0) { return(csObjLiteral.WithInitializer(SyntaxFactory.InitializerExpression( SyntaxKind.CollectionInitializerExpression, SyntaxFactory.SeparatedList(initItemExprs)))); } return(csObjLiteral); } }
/// <summary> /// DScript has slightly different formatting in objects for readability of large lists /// </summary> public override void VisitObjectLiteralExpression(ObjectLiteralExpression node) { var previousContext = IsInObjectLiteralContext; IsInObjectLiteralContext = true; base.VisitObjectLiteralExpression(node); IsInObjectLiteralContext = previousContext; }
public void ObjectLiteralExpressionProducesObjectLiterals() { var expression = new ObjectLiteralExpression(new Dictionary <Expression, Expression> { { JS.Id("a"), JS.Number(12) }, { JS.Id("b"), JS.String("Wrong!") }, { JS.Id("c"), null } }); Assert.AreEqual(3, expression.Properties.Count); Assert.AreEqual("{a:12,b:\"Wrong!\",c:null};", expression.ToString()); }
public void ObjectLiteralExpressionProducesObjectLiterals() { var expression = new ObjectLiteralExpression(new Dictionary<Expression, Expression> { {JS.Id("a"), JS.Number(12)}, {JS.Id("b"), JS.String("Wrong!")}, {JS.Id("c"), null} }); Assert.AreEqual(3, expression.Properties.Count); Assert.AreEqual("{a:12,b:\"Wrong!\",c:null};", expression.ToString()); }
/// <summary> /// Creates a new instance of <see cref="ObjectLiteralExpression" />, by copying the specified expression's properties, and adding the specified name and value to the properties. /// </summary> /// <param name="expression">The expression to copy the properties from.</param> /// <param name="name">The name of the property to add.</param> /// <param name="value">The value of the property to add.</param> /// <returns>a new instance of <see cref="ObjectLiteralExpression" /></returns> public static ObjectLiteralExpression WithProperty(this ObjectLiteralExpression expression, Expression name, Expression value) { if (expression == null) { throw new ArgumentNullException("expression"); } ObjectLiteralExpression result = new ObjectLiteralExpression(expression.Properties); result.Properties[name] = value; return result; }
public void ObjectLiteralExpressionHasHelpers() { var expression = new ObjectLiteralExpression(); Assert.AreEqual("{};", expression.ToString()); expression = expression.WithProperty(JS.Id("name"), "value"); Assert.AreEqual("{name:\"value\"};", expression.ToString()); expression = expression.WithProperties(new Dictionary <Expression, Expression> { { JS.Id("key"), "value" }, { JS.Id("price"), 1200 } }); Assert.AreEqual("{name:\"value\",key:\"value\",price:1200};", expression.ToString()); }
public void ObjectLiteralExpressionHasHelpers() { var expression = new ObjectLiteralExpression(); Assert.AreEqual("{};", expression.ToString()); expression = expression.WithProperty(JS.Id("name"), "value"); Assert.AreEqual("{name:\"value\"};", expression.ToString()); expression = expression.WithProperties(new Dictionary<Expression, Expression> { {JS.Id("key"), "value"}, {JS.Id("price"), 1200} }); Assert.AreEqual("{name:\"value\",key:\"value\",price:1200};", expression.ToString()); }
/// <summary> /// Creates a new instance of <see cref="ObjectLiteralExpression" />, by copying the specified expression's properties, and adding the specified properties. /// </summary> /// <param name="expression">The expression to copy the properties from.</param> /// <param name="values">A dictionary containing properties to add to the new instance.</param> /// <returns>a new instance of <see cref="ObjectLiteralExpression" /></returns> public static ObjectLiteralExpression WithProperties(this ObjectLiteralExpression expression, IDictionary<Expression, Expression> values) { if (expression == null) { throw new ArgumentNullException("expression"); } if (values == null) { throw new ArgumentNullException("values"); } ObjectLiteralExpression result = new ObjectLiteralExpression(expression.Properties); foreach (KeyValuePair<Expression, Expression> property in values) { result.Properties[property.Key] = property.Value; } return result; }
public void ObjectLiteralExpressionProducesEmptyObject() { var expression = new ObjectLiteralExpression(); Assert.AreEqual("{};", expression.ToString()); }