private static Node GetArrayLiteralType(ArrayLiteralExpression arrayLiteral) { Node type = GetDeclarationType(arrayLiteral); if (type != null) { return(type); } if (arrayLiteral.Elements.Find(n => n.Kind == NodeKind.SpreadElement) is SpreadElement spreadElement) { type = GetNodeType(spreadElement.Expression); if (type != null) { return(type); } } ArrayType arrayType = (ArrayType)NodeHelper.CreateNode(NodeKind.ArrayType); Node elementType = GetItemType(arrayLiteral); if (elementType == null) { elementType = NodeHelper.CreateNode(NodeKind.AnyKeyword); } arrayType.SetElementType(elementType, elementType.Parent == null); return(arrayType); }
public static void AddModify(this Node node, NodeKind modify) { List <Node> modifiers = node.GetValue("Modifiers") as List <Node>; if (modifiers == null) { return; } if (modifiers.Exists(n => n.Kind == modify)) { return; } modifiers.Add(NodeHelper.CreateNode(modify)); }
public static Node GetNodeType(Node node) { switch (node.Kind) { case NodeKind.StringLiteral: return(NodeHelper.CreateNode(NodeKind.StringKeyword)); case NodeKind.NumericLiteral: return(NodeHelper.CreateNode(NodeKind.NumberKeyword)); case NodeKind.PrefixUnaryExpression: return(NodeHelper.CreateNode(NodeKind.NumberKeyword)); case NodeKind.BinaryExpression: return(GetBinaryExpressionType(node as BinaryExpression)); case NodeKind.TrueKeyword: case NodeKind.FalseKeyword: return(NodeHelper.CreateNode(NodeKind.BooleanKeyword)); case NodeKind.NewExpression: return((node as NewExpression).Type); case NodeKind.Identifier: return(GetIdentifierType(node as Identifier)); case NodeKind.PropertyAccessExpression: case NodeKind.CallExpression: return(GetPropertyAccessType(node)); case NodeKind.ArrayLiteralExpression: return(GetArrayLiteralType(node as ArrayLiteralExpression)); case NodeKind.ObjectLiteralExpression: return(GetObjectLiteralType(node as ObjectLiteralExpression)); case NodeKind.ElementAccessExpression: return(GetElementAccessType(node as ElementAccessExpression)); case NodeKind.ParenthesizedExpression: return(GetNodeType((node as ParenthesizedExpression).Expression)); case NodeKind.AsExpression: return((node as AsExpression).Type); default: return(node.GetValue("Type") as Node); } }
internal static Node ToQualifiedName(this Node node) { if (node.Kind != NodeKind.PropertyAccessExpression) { return(node); } PropertyAccessExpression expression = node as PropertyAccessExpression; //PropertyAccessExpression string jsonString = expression.TsNode.ToString(); jsonString = jsonString .Replace("PropertyAccessExpression", "QualifiedName") .Replace("\"expression\":", "\"left\":") .Replace("\"name\":", "\"right\":"); return(NodeHelper.CreateNode(jsonString)); }
private static Node GetItemType(ArrayLiteralExpression value) { Node elementType = null; List <Node> elements = value.Elements; if (elements.Find(n => n.Kind == NodeKind.StringLiteral) != null) { elementType = NodeHelper.CreateNode(NodeKind.StringKeyword); } else if (elements.Find(n => n.Kind == NodeKind.TrueKeyword || n.Kind == NodeKind.FalseKeyword) != null) { elementType = NodeHelper.CreateNode(NodeKind.BooleanKeyword); } else if (elements.Find(n => n.Kind == NodeKind.NumericLiteral || (n.Kind == NodeKind.PrefixUnaryExpression && (n as PrefixUnaryExpression).Operand.Kind == NodeKind.NumericLiteral)) != null) { elementType = NodeHelper.CreateNode(NodeKind.NumberKeyword); } else if (elements.Count > 0) { foreach (Node element in elements) { Node type = GetNodeType(element); if (type != null) { elementType = type; break; } } } else if (elements.Count == 0 && value.Parent.Kind == NodeKind.ArrayLiteralExpression) { Node arrarrType = GetArrayLiteralType((ArrayLiteralExpression)value.Parent); if (arrarrType != null) { elementType = ((arrarrType as ArrayType).ElementType as ArrayType).ElementType; } } elementType = elementType ?? NodeHelper.CreateNode(NodeKind.AnyKeyword); elementType.NodeName = "elementType"; return(elementType); }
private static Node GetObjectLiteralType(ObjectLiteralExpression objectLiteral) { Node type = GetDeclarationType(objectLiteral); if (type != null) { return(type); } List <Node> properties = objectLiteral.Properties; if (properties.Count == 0) { TypeLiteral typeLiteral = NodeHelper.CreateNode(NodeKind.TypeLiteral) as TypeLiteral; typeLiteral.Members.Add(NodeHelper.CreateNode(NodeKind.IndexSignature)); return(typeLiteral); } else { TypeLiteral typeLiteral = NodeHelper.CreateNode(NodeKind.TypeLiteral) as TypeLiteral; foreach (PropertyAssignment prop in properties) { Node initValue = prop.Initializer; Node elementType = null; if (initValue.Kind != NodeKind.ObjectLiteralExpression && initValue.Kind != NodeKind.ArrayLiteralExpression) { elementType = GetNodeType(initValue); } elementType = elementType ?? NodeHelper.CreateNode(NodeKind.AnyKeyword); elementType.NodeName = "type"; Node propSignature = NodeHelper.CreateNode(NodeKind.PropertySignature); propSignature.AddChild(prop.Name.TsNode); propSignature.AddChild(elementType); typeLiteral.Members.Add(propSignature); } return(typeLiteral); } }
private static Node GetBinaryExpressionType(BinaryExpression binary) { switch (binary.OperatorToken.Kind) { case NodeKind.MinusToken: case NodeKind.AsteriskToken: case NodeKind.SlashToken: return(NodeHelper.CreateNode(NodeKind.NumberKeyword)); case NodeKind.PlusToken: Node leftType = GetNodeType(binary.Left); Node rightType = GetNodeType(binary.Right); if (IsNumberType(leftType) && IsNumberType(rightType)) { return(NodeHelper.CreateNode(NodeKind.NumberKeyword)); } return(NodeHelper.CreateNode(NodeKind.StringKeyword)); default: return(null); } }
public void AddChild(JObject tsNode) { this.AddChild(NodeHelper.CreateNode(tsNode)); }