diff --git a/src/services/services.ts b/src/services/services.ts index 71248b007b6..fc4d1eff223 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -58,9 +58,10 @@ module ts { } export interface SourceFile { - version: string; - scriptSnapshot: IScriptSnapshot; - nameTable: Map; + /* @internal */ version: string; + /* @internal */ scriptSnapshot: IScriptSnapshot; + /* @internal */ nameTable: Map; + getNamedDeclarations(): Declaration[]; getLineAndCharacterOfPosition(pos: number): LineAndCharacter; getLineStarts(): number[]; @@ -4138,27 +4139,6 @@ module ts { return getReferencesForNode(node, program.getSourceFiles(), /*searchOnlyInCurrentFile*/ false, findInStrings, findInComments); } - function initializeNameTable(sourceFile: SourceFile): void { - var nameTable: Map = {}; - - walk(sourceFile); - sourceFile.nameTable = nameTable; - - function walk(node: Node) { - switch (node.kind) { - case SyntaxKind.Identifier: - nameTable[(node).text] = (node).text; - break; - case SyntaxKind.StringLiteral: - case SyntaxKind.NumericLiteral: - nameTable[(node).text] = (node).text; - break; - default: - forEachChild(node, walk); - } - } - } - function getReferencesForNode(node: Node, sourceFiles: SourceFile[], searchOnlyInCurrentFile: boolean, findInStrings: boolean, findInComments: boolean): ReferenceEntry[] { // Labels if (isLabelName(node)) { @@ -4225,13 +4205,9 @@ module ts { forEach(sourceFiles, sourceFile => { cancellationToken.throwIfCancellationRequested(); - if (!sourceFile.nameTable) { - initializeNameTable(sourceFile) - } + var nameTable = getNameTable(sourceFile); - Debug.assert(sourceFile.nameTable !== undefined); - - if (lookUp(sourceFile.nameTable, internedName)) { + if (lookUp(nameTable, internedName)) { result = result || []; getReferencesInNode(sourceFile, symbol, declaredName, node, searchMeaning, findInStrings, findInComments, result); } @@ -5775,6 +5751,52 @@ module ts { }; } + /* @internal */ + export function getNameTable(sourceFile: SourceFile): Map { + if (!sourceFile.nameTable) { + initializeNameTable(sourceFile) + } + + return sourceFile.nameTable; + } + + function initializeNameTable(sourceFile: SourceFile): void { + var nameTable: Map = {}; + + walk(sourceFile); + sourceFile.nameTable = nameTable; + + function walk(node: Node) { + switch (node.kind) { + case SyntaxKind.Identifier: + nameTable[(node).text] = (node).text; + break; + case SyntaxKind.StringLiteral: + case SyntaxKind.NumericLiteral: + // We want to store any numbers/strings if they were a name that could be + // related to a declaration. So, if we have 'import x = require("something")' + // then we want 'something' to be in the name table. Similarly, if we have + // "a['propname']" then we want to store "propname" in the name table. + if (isDeclarationName(node) || + node.parent.kind === SyntaxKind.ExternalModuleReference || + isArgumentOfElementAccessExpression(node)) { + + nameTable[(node).text] = (node).text; + } + break; + default: + forEachChild(node, walk); + } + } + } + + function isArgumentOfElementAccessExpression(node: Node) { + return node && + node.parent && + node.parent.kind === SyntaxKind.ElementAccessExpression && + (node.parent).argumentExpression === node; + } + /// Classifier export function createClassifier(): Classifier { var scanner = createScanner(ScriptTarget.Latest, /*skipTrivia*/ false); diff --git a/tests/baselines/reference/APISample_compile.js b/tests/baselines/reference/APISample_compile.js index 1f2cb776526..ca2bb9511a7 100644 --- a/tests/baselines/reference/APISample_compile.js +++ b/tests/baselines/reference/APISample_compile.js @@ -1518,9 +1518,6 @@ declare module "typescript" { getDocumentationComment(): SymbolDisplayPart[]; } interface SourceFile { - version: string; - scriptSnapshot: IScriptSnapshot; - nameTable: Map; getNamedDeclarations(): Declaration[]; getLineAndCharacterOfPosition(pos: number): LineAndCharacter; getLineStarts(): number[]; diff --git a/tests/baselines/reference/APISample_compile.types b/tests/baselines/reference/APISample_compile.types index e2526712f12..896290f73c0 100644 --- a/tests/baselines/reference/APISample_compile.types +++ b/tests/baselines/reference/APISample_compile.types @@ -4896,17 +4896,6 @@ declare module "typescript" { interface SourceFile { >SourceFile : SourceFile - version: string; ->version : string - - scriptSnapshot: IScriptSnapshot; ->scriptSnapshot : IScriptSnapshot ->IScriptSnapshot : IScriptSnapshot - - nameTable: Map; ->nameTable : Map ->Map : Map - getNamedDeclarations(): Declaration[]; >getNamedDeclarations : () => Declaration[] >Declaration : Declaration diff --git a/tests/baselines/reference/APISample_linter.js b/tests/baselines/reference/APISample_linter.js index 4998afa4661..d6177798c76 100644 --- a/tests/baselines/reference/APISample_linter.js +++ b/tests/baselines/reference/APISample_linter.js @@ -1549,9 +1549,6 @@ declare module "typescript" { getDocumentationComment(): SymbolDisplayPart[]; } interface SourceFile { - version: string; - scriptSnapshot: IScriptSnapshot; - nameTable: Map; getNamedDeclarations(): Declaration[]; getLineAndCharacterOfPosition(pos: number): LineAndCharacter; getLineStarts(): number[]; diff --git a/tests/baselines/reference/APISample_linter.types b/tests/baselines/reference/APISample_linter.types index b8908315d6d..73c1c529627 100644 --- a/tests/baselines/reference/APISample_linter.types +++ b/tests/baselines/reference/APISample_linter.types @@ -5042,17 +5042,6 @@ declare module "typescript" { interface SourceFile { >SourceFile : SourceFile - version: string; ->version : string - - scriptSnapshot: IScriptSnapshot; ->scriptSnapshot : IScriptSnapshot ->IScriptSnapshot : IScriptSnapshot - - nameTable: Map; ->nameTable : Map ->Map : Map - getNamedDeclarations(): Declaration[]; >getNamedDeclarations : () => Declaration[] >Declaration : Declaration diff --git a/tests/baselines/reference/APISample_transform.js b/tests/baselines/reference/APISample_transform.js index 92d19b46e39..ba7e4a195d8 100644 --- a/tests/baselines/reference/APISample_transform.js +++ b/tests/baselines/reference/APISample_transform.js @@ -1550,9 +1550,6 @@ declare module "typescript" { getDocumentationComment(): SymbolDisplayPart[]; } interface SourceFile { - version: string; - scriptSnapshot: IScriptSnapshot; - nameTable: Map; getNamedDeclarations(): Declaration[]; getLineAndCharacterOfPosition(pos: number): LineAndCharacter; getLineStarts(): number[]; diff --git a/tests/baselines/reference/APISample_transform.types b/tests/baselines/reference/APISample_transform.types index a6c77fc7f05..6d28f9194ed 100644 --- a/tests/baselines/reference/APISample_transform.types +++ b/tests/baselines/reference/APISample_transform.types @@ -4992,17 +4992,6 @@ declare module "typescript" { interface SourceFile { >SourceFile : SourceFile - version: string; ->version : string - - scriptSnapshot: IScriptSnapshot; ->scriptSnapshot : IScriptSnapshot ->IScriptSnapshot : IScriptSnapshot - - nameTable: Map; ->nameTable : Map ->Map : Map - getNamedDeclarations(): Declaration[]; >getNamedDeclarations : () => Declaration[] >Declaration : Declaration diff --git a/tests/baselines/reference/APISample_watcher.js b/tests/baselines/reference/APISample_watcher.js index 1528b0331c1..3d4c3308b8c 100644 --- a/tests/baselines/reference/APISample_watcher.js +++ b/tests/baselines/reference/APISample_watcher.js @@ -1587,9 +1587,6 @@ declare module "typescript" { getDocumentationComment(): SymbolDisplayPart[]; } interface SourceFile { - version: string; - scriptSnapshot: IScriptSnapshot; - nameTable: Map; getNamedDeclarations(): Declaration[]; getLineAndCharacterOfPosition(pos: number): LineAndCharacter; getLineStarts(): number[]; diff --git a/tests/baselines/reference/APISample_watcher.types b/tests/baselines/reference/APISample_watcher.types index 9a245522156..f7d9c0e283e 100644 --- a/tests/baselines/reference/APISample_watcher.types +++ b/tests/baselines/reference/APISample_watcher.types @@ -5165,17 +5165,6 @@ declare module "typescript" { interface SourceFile { >SourceFile : SourceFile - version: string; ->version : string - - scriptSnapshot: IScriptSnapshot; ->scriptSnapshot : IScriptSnapshot ->IScriptSnapshot : IScriptSnapshot - - nameTable: Map; ->nameTable : Map ->Map : Map - getNamedDeclarations(): Declaration[]; >getNamedDeclarations : () => Declaration[] >Declaration : Declaration