Skip to content

Commit 08439b7

Browse files
Expose a way to get the name table from the LS.
1 parent 0cbe55b commit 08439b7

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

src/services/services.ts

+48-43
Original file line numberDiff line numberDiff line change
@@ -4138,43 +4138,6 @@ module ts {
41384138
return getReferencesForNode(node, program.getSourceFiles(), /*searchOnlyInCurrentFile*/ false, findInStrings, findInComments);
41394139
}
41404140

4141-
function initializeNameTable(sourceFile: SourceFile): void {
4142-
var nameTable: Map<string> = {};
4143-
4144-
walk(sourceFile);
4145-
sourceFile.nameTable = nameTable;
4146-
4147-
function walk(node: Node) {
4148-
switch (node.kind) {
4149-
case SyntaxKind.Identifier:
4150-
nameTable[(<Identifier>node).text] = (<Identifier>node).text;
4151-
break;
4152-
case SyntaxKind.StringLiteral:
4153-
case SyntaxKind.NumericLiteral:
4154-
// We want to store any numbers/strings if they were a name that could be
4155-
// related to a declaration. So, if we have 'import x = require("something")'
4156-
// then we want 'something' to be in the name table. Similarly, if we have
4157-
// "a['propname']" then we want to store "propname" in the name table.
4158-
if (isDeclarationName(node) ||
4159-
node.parent.kind === SyntaxKind.ExternalModuleReference ||
4160-
isArgumentOfElementAccessExpression(node)) {
4161-
4162-
nameTable[(<LiteralExpression>node).text] = (<LiteralExpression>node).text;
4163-
}
4164-
break;
4165-
default:
4166-
forEachChild(node, walk);
4167-
}
4168-
}
4169-
}
4170-
4171-
function isArgumentOfElementAccessExpression(node: Node) {
4172-
return node &&
4173-
node.parent &&
4174-
node.parent.kind === SyntaxKind.ElementAccessExpression &&
4175-
(<ElementAccessExpression>node.parent).argumentExpression === node;
4176-
}
4177-
41784141
function getReferencesForNode(node: Node, sourceFiles: SourceFile[], searchOnlyInCurrentFile: boolean, findInStrings: boolean, findInComments: boolean): ReferenceEntry[] {
41794142
// Labels
41804143
if (isLabelName(node)) {
@@ -4241,13 +4204,9 @@ module ts {
42414204
forEach(sourceFiles, sourceFile => {
42424205
cancellationToken.throwIfCancellationRequested();
42434206

4244-
if (!sourceFile.nameTable) {
4245-
initializeNameTable(sourceFile)
4246-
}
4207+
var nameTable = getNameTable(sourceFile);
42474208

4248-
Debug.assert(sourceFile.nameTable !== undefined);
4249-
4250-
if (lookUp(sourceFile.nameTable, internedName)) {
4209+
if (lookUp(nameTable, internedName)) {
42514210
result = result || [];
42524211
getReferencesInNode(sourceFile, symbol, declaredName, node, searchMeaning, findInStrings, findInComments, result);
42534212
}
@@ -5791,6 +5750,52 @@ module ts {
57915750
};
57925751
}
57935752

5753+
/* @internal */
5754+
export function getNameTable(sourceFile: SourceFile): Map<string> {
5755+
if (!sourceFile.nameTable) {
5756+
initializeNameTable(sourceFile)
5757+
}
5758+
5759+
return sourceFile.nameTable;
5760+
}
5761+
5762+
function initializeNameTable(sourceFile: SourceFile): void {
5763+
var nameTable: Map<string> = {};
5764+
5765+
walk(sourceFile);
5766+
sourceFile.nameTable = nameTable;
5767+
5768+
function walk(node: Node) {
5769+
switch (node.kind) {
5770+
case SyntaxKind.Identifier:
5771+
nameTable[(<Identifier>node).text] = (<Identifier>node).text;
5772+
break;
5773+
case SyntaxKind.StringLiteral:
5774+
case SyntaxKind.NumericLiteral:
5775+
// We want to store any numbers/strings if they were a name that could be
5776+
// related to a declaration. So, if we have 'import x = require("something")'
5777+
// then we want 'something' to be in the name table. Similarly, if we have
5778+
// "a['propname']" then we want to store "propname" in the name table.
5779+
if (isDeclarationName(node) ||
5780+
node.parent.kind === SyntaxKind.ExternalModuleReference ||
5781+
isArgumentOfElementAccessExpression(node)) {
5782+
5783+
nameTable[(<LiteralExpression>node).text] = (<LiteralExpression>node).text;
5784+
}
5785+
break;
5786+
default:
5787+
forEachChild(node, walk);
5788+
}
5789+
}
5790+
}
5791+
5792+
function isArgumentOfElementAccessExpression(node: Node) {
5793+
return node &&
5794+
node.parent &&
5795+
node.parent.kind === SyntaxKind.ElementAccessExpression &&
5796+
(<ElementAccessExpression>node.parent).argumentExpression === node;
5797+
}
5798+
57945799
/// Classifier
57955800
export function createClassifier(): Classifier {
57965801
var scanner = createScanner(ScriptTarget.Latest, /*skipTrivia*/ false);

0 commit comments

Comments
 (0)