From 32a5984601c078081e4d350f3de45ed1662a7a84 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 12 Aug 2014 10:00:27 -0700 Subject: [PATCH] expose utility functions --- src/compiler/checker.ts | 100 ---------------------------------------- src/compiler/parser.ts | 100 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 100 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cbc9096875..3a804815e9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6546,106 +6546,6 @@ module ts { return mapToArray(symbols); } - // True if the given identifier is the identifier of a declaration node - function isDeclarationIdentifier(identifier: Identifier): boolean { - if (identifier.parent) { - switch (identifier.parent.kind) { - case SyntaxKind.TypeParameter: - case SyntaxKind.Parameter: - case SyntaxKind.VariableDeclaration: - case SyntaxKind.Property: - case SyntaxKind.PropertyAssignment: - case SyntaxKind.EnumMember: - case SyntaxKind.Method: - case SyntaxKind.FunctionDeclaration: - case SyntaxKind.FunctionExpression: - case SyntaxKind.GetAccessor: - case SyntaxKind.SetAccessor: - case SyntaxKind.ClassDeclaration: - case SyntaxKind.InterfaceDeclaration: - case SyntaxKind.EnumDeclaration: - case SyntaxKind.ModuleDeclaration: - case SyntaxKind.ImportDeclaration: - return (identifier.parent).name === identifier; - case SyntaxKind.CatchBlock: - return (identifier.parent).variable === identifier; - } - } - return false; - } - - // True if the given identifier is part of a type reference - function isTypeReferenceIdentifier(entityName: EntityName): boolean { - var node: Node = entityName; - while (node.parent && node.parent.kind === SyntaxKind.QualifiedName) node = node.parent; - return node.parent && node.parent.kind === SyntaxKind.TypeReference; - } - - function isExpression(node: Node): boolean { - switch (node.kind) { - case SyntaxKind.ThisKeyword: - case SyntaxKind.SuperKeyword: - case SyntaxKind.NullKeyword: - case SyntaxKind.TrueKeyword: - case SyntaxKind.FalseKeyword: - case SyntaxKind.RegularExpressionLiteral: - case SyntaxKind.ArrayLiteral: - case SyntaxKind.ObjectLiteral: - case SyntaxKind.PropertyAccess: - case SyntaxKind.IndexedAccess: - case SyntaxKind.CallExpression: - case SyntaxKind.NewExpression: - case SyntaxKind.TypeAssertion: - case SyntaxKind.ParenExpression: - case SyntaxKind.FunctionExpression: - case SyntaxKind.ArrowFunction: - case SyntaxKind.PrefixOperator: - case SyntaxKind.PostfixOperator: - case SyntaxKind.BinaryExpression: - case SyntaxKind.ConditionalExpression: - return true; - case SyntaxKind.QualifiedName: - while (node.parent && node.parent.kind === SyntaxKind.QualifiedName) node = node.parent; - return node.parent && node.parent.kind === SyntaxKind.TypeQuery; - case SyntaxKind.Identifier: - case SyntaxKind.NumericLiteral: - case SyntaxKind.StringLiteral: - var parent = node.parent; - if (parent) { - if (isExpression(parent)) return true; - switch (parent.kind) { - case SyntaxKind.VariableDeclaration: - case SyntaxKind.Parameter: - case SyntaxKind.Property: - case SyntaxKind.EnumMember: - return (parent).initializer === node; - case SyntaxKind.ExpressionStatement: - case SyntaxKind.IfStatement: - case SyntaxKind.DoStatement: - case SyntaxKind.WhileStatement: - case SyntaxKind.ReturnStatement: - case SyntaxKind.WithStatement: - case SyntaxKind.SwitchStatement: - case SyntaxKind.CaseClause: - case SyntaxKind.ThrowStatement: - case SyntaxKind.SwitchStatement: - return (parent).expression === node; - case SyntaxKind.ForStatement: - return (parent).initializer === node || (parent).condition === node || - (parent).iterator === node; - case SyntaxKind.ForInStatement: - return (parent).variable === node || (parent).expression === node; - } - } - } - return false; - } - - function isRightSideOfQualifiedNameOrPropertyAccess(node: Node) { - return (node.parent.kind === SyntaxKind.QualifiedName || node.parent.kind === SyntaxKind.PropertyAccess) && - (node.parent).right === node; - } - function getSymbolOfIdentifier(identifier: Identifier) { if (isDeclarationIdentifier(identifier)) { return getSymbolOfNode(identifier.parent); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index d75fea408d..1a12cbdaa3 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -335,6 +335,106 @@ module ts { return false; } + // True if the given identifier is the identifier of a declaration node + export function isDeclarationIdentifier(identifier: Identifier): boolean { + if (identifier.parent) { + switch (identifier.parent.kind) { + case SyntaxKind.TypeParameter: + case SyntaxKind.Parameter: + case SyntaxKind.VariableDeclaration: + case SyntaxKind.Property: + case SyntaxKind.PropertyAssignment: + case SyntaxKind.EnumMember: + case SyntaxKind.Method: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.FunctionExpression: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: + case SyntaxKind.ClassDeclaration: + case SyntaxKind.InterfaceDeclaration: + case SyntaxKind.EnumDeclaration: + case SyntaxKind.ModuleDeclaration: + case SyntaxKind.ImportDeclaration: + return (identifier.parent).name === identifier; + case SyntaxKind.CatchBlock: + return (identifier.parent).variable === identifier; + } + } + return false; + } + + // True if the given identifier is part of a type reference + export function isTypeReferenceIdentifier(entityName: EntityName): boolean { + var node: Node = entityName; + while (node.parent && node.parent.kind === SyntaxKind.QualifiedName) node = node.parent; + return node.parent && node.parent.kind === SyntaxKind.TypeReference; + } + + export function isExpression(node: Node): boolean { + switch (node.kind) { + case SyntaxKind.ThisKeyword: + case SyntaxKind.SuperKeyword: + case SyntaxKind.NullKeyword: + case SyntaxKind.TrueKeyword: + case SyntaxKind.FalseKeyword: + case SyntaxKind.RegularExpressionLiteral: + case SyntaxKind.ArrayLiteral: + case SyntaxKind.ObjectLiteral: + case SyntaxKind.PropertyAccess: + case SyntaxKind.IndexedAccess: + case SyntaxKind.CallExpression: + case SyntaxKind.NewExpression: + case SyntaxKind.TypeAssertion: + case SyntaxKind.ParenExpression: + case SyntaxKind.FunctionExpression: + case SyntaxKind.ArrowFunction: + case SyntaxKind.PrefixOperator: + case SyntaxKind.PostfixOperator: + case SyntaxKind.BinaryExpression: + case SyntaxKind.ConditionalExpression: + return true; + case SyntaxKind.QualifiedName: + while (node.parent && node.parent.kind === SyntaxKind.QualifiedName) node = node.parent; + return node.parent && node.parent.kind === SyntaxKind.TypeQuery; + case SyntaxKind.Identifier: + case SyntaxKind.NumericLiteral: + case SyntaxKind.StringLiteral: + var parent = node.parent; + if (parent) { + if (isExpression(parent)) return true; + switch (parent.kind) { + case SyntaxKind.VariableDeclaration: + case SyntaxKind.Parameter: + case SyntaxKind.Property: + case SyntaxKind.EnumMember: + return (parent).initializer === node; + case SyntaxKind.ExpressionStatement: + case SyntaxKind.IfStatement: + case SyntaxKind.DoStatement: + case SyntaxKind.WhileStatement: + case SyntaxKind.ReturnStatement: + case SyntaxKind.WithStatement: + case SyntaxKind.SwitchStatement: + case SyntaxKind.CaseClause: + case SyntaxKind.ThrowStatement: + case SyntaxKind.SwitchStatement: + return (parent).expression === node; + case SyntaxKind.ForStatement: + return (parent).initializer === node || (parent).condition === node || + (parent).iterator === node; + case SyntaxKind.ForInStatement: + return (parent).variable === node || (parent).expression === node; + } + } + } + return false; + } + + export function isRightSideOfQualifiedNameOrPropertyAccess(node: Node) { + return (node.parent.kind === SyntaxKind.QualifiedName || node.parent.kind === SyntaxKind.PropertyAccess) && + (node.parent).right === node; + } + enum ParsingContext { SourceElements, // Elements in source file ModuleElements, // Elements in module declaration