Skip to content

Commit 0de61cb

Browse files
Merge pull request microsoft#2177 from Microsoft/intern
Don't intern all strings and numbers. Just the ones used as declaration names
2 parents bd447f7 + e452cff commit 0de61cb

9 files changed

+52
-86
lines changed

src/services/services.ts

+52-30
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ module ts {
5858
}
5959

6060
export interface SourceFile {
61-
version: string;
62-
scriptSnapshot: IScriptSnapshot;
63-
nameTable: Map<string>;
61+
/* @internal */ version: string;
62+
/* @internal */ scriptSnapshot: IScriptSnapshot;
63+
/* @internal */ nameTable: Map<string>;
64+
6465
getNamedDeclarations(): Declaration[];
6566
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
6667
getLineStarts(): number[];
@@ -4138,27 +4139,6 @@ module ts {
41384139
return getReferencesForNode(node, program.getSourceFiles(), /*searchOnlyInCurrentFile*/ false, findInStrings, findInComments);
41394140
}
41404141

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-
nameTable[(<LiteralExpression>node).text] = (<LiteralExpression>node).text;
4155-
break;
4156-
default:
4157-
forEachChild(node, walk);
4158-
}
4159-
}
4160-
}
4161-
41624142
function getReferencesForNode(node: Node, sourceFiles: SourceFile[], searchOnlyInCurrentFile: boolean, findInStrings: boolean, findInComments: boolean): ReferenceEntry[] {
41634143
// Labels
41644144
if (isLabelName(node)) {
@@ -4225,13 +4205,9 @@ module ts {
42254205
forEach(sourceFiles, sourceFile => {
42264206
cancellationToken.throwIfCancellationRequested();
42274207

4228-
if (!sourceFile.nameTable) {
4229-
initializeNameTable(sourceFile)
4230-
}
4208+
var nameTable = getNameTable(sourceFile);
42314209

4232-
Debug.assert(sourceFile.nameTable !== undefined);
4233-
4234-
if (lookUp(sourceFile.nameTable, internedName)) {
4210+
if (lookUp(nameTable, internedName)) {
42354211
result = result || [];
42364212
getReferencesInNode(sourceFile, symbol, declaredName, node, searchMeaning, findInStrings, findInComments, result);
42374213
}
@@ -5775,6 +5751,52 @@ module ts {
57755751
};
57765752
}
57775753

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

tests/baselines/reference/APISample_compile.js

-3
Original file line numberDiff line numberDiff line change
@@ -1518,9 +1518,6 @@ declare module "typescript" {
15181518
getDocumentationComment(): SymbolDisplayPart[];
15191519
}
15201520
interface SourceFile {
1521-
version: string;
1522-
scriptSnapshot: IScriptSnapshot;
1523-
nameTable: Map<string>;
15241521
getNamedDeclarations(): Declaration[];
15251522
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
15261523
getLineStarts(): number[];

tests/baselines/reference/APISample_compile.types

-11
Original file line numberDiff line numberDiff line change
@@ -4896,17 +4896,6 @@ declare module "typescript" {
48964896
interface SourceFile {
48974897
>SourceFile : SourceFile
48984898

4899-
version: string;
4900-
>version : string
4901-
4902-
scriptSnapshot: IScriptSnapshot;
4903-
>scriptSnapshot : IScriptSnapshot
4904-
>IScriptSnapshot : IScriptSnapshot
4905-
4906-
nameTable: Map<string>;
4907-
>nameTable : Map<string>
4908-
>Map : Map<T>
4909-
49104899
getNamedDeclarations(): Declaration[];
49114900
>getNamedDeclarations : () => Declaration[]
49124901
>Declaration : Declaration

tests/baselines/reference/APISample_linter.js

-3
Original file line numberDiff line numberDiff line change
@@ -1549,9 +1549,6 @@ declare module "typescript" {
15491549
getDocumentationComment(): SymbolDisplayPart[];
15501550
}
15511551
interface SourceFile {
1552-
version: string;
1553-
scriptSnapshot: IScriptSnapshot;
1554-
nameTable: Map<string>;
15551552
getNamedDeclarations(): Declaration[];
15561553
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
15571554
getLineStarts(): number[];

tests/baselines/reference/APISample_linter.types

-11
Original file line numberDiff line numberDiff line change
@@ -5042,17 +5042,6 @@ declare module "typescript" {
50425042
interface SourceFile {
50435043
>SourceFile : SourceFile
50445044

5045-
version: string;
5046-
>version : string
5047-
5048-
scriptSnapshot: IScriptSnapshot;
5049-
>scriptSnapshot : IScriptSnapshot
5050-
>IScriptSnapshot : IScriptSnapshot
5051-
5052-
nameTable: Map<string>;
5053-
>nameTable : Map<string>
5054-
>Map : Map<T>
5055-
50565045
getNamedDeclarations(): Declaration[];
50575046
>getNamedDeclarations : () => Declaration[]
50585047
>Declaration : Declaration

tests/baselines/reference/APISample_transform.js

-3
Original file line numberDiff line numberDiff line change
@@ -1550,9 +1550,6 @@ declare module "typescript" {
15501550
getDocumentationComment(): SymbolDisplayPart[];
15511551
}
15521552
interface SourceFile {
1553-
version: string;
1554-
scriptSnapshot: IScriptSnapshot;
1555-
nameTable: Map<string>;
15561553
getNamedDeclarations(): Declaration[];
15571554
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
15581555
getLineStarts(): number[];

tests/baselines/reference/APISample_transform.types

-11
Original file line numberDiff line numberDiff line change
@@ -4992,17 +4992,6 @@ declare module "typescript" {
49924992
interface SourceFile {
49934993
>SourceFile : SourceFile
49944994

4995-
version: string;
4996-
>version : string
4997-
4998-
scriptSnapshot: IScriptSnapshot;
4999-
>scriptSnapshot : IScriptSnapshot
5000-
>IScriptSnapshot : IScriptSnapshot
5001-
5002-
nameTable: Map<string>;
5003-
>nameTable : Map<string>
5004-
>Map : Map<T>
5005-
50064995
getNamedDeclarations(): Declaration[];
50074996
>getNamedDeclarations : () => Declaration[]
50084997
>Declaration : Declaration

tests/baselines/reference/APISample_watcher.js

-3
Original file line numberDiff line numberDiff line change
@@ -1587,9 +1587,6 @@ declare module "typescript" {
15871587
getDocumentationComment(): SymbolDisplayPart[];
15881588
}
15891589
interface SourceFile {
1590-
version: string;
1591-
scriptSnapshot: IScriptSnapshot;
1592-
nameTable: Map<string>;
15931590
getNamedDeclarations(): Declaration[];
15941591
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
15951592
getLineStarts(): number[];

tests/baselines/reference/APISample_watcher.types

-11
Original file line numberDiff line numberDiff line change
@@ -5165,17 +5165,6 @@ declare module "typescript" {
51655165
interface SourceFile {
51665166
>SourceFile : SourceFile
51675167

5168-
version: string;
5169-
>version : string
5170-
5171-
scriptSnapshot: IScriptSnapshot;
5172-
>scriptSnapshot : IScriptSnapshot
5173-
>IScriptSnapshot : IScriptSnapshot
5174-
5175-
nameTable: Map<string>;
5176-
>nameTable : Map<string>
5177-
>Map : Map<T>
5178-
51795168
getNamedDeclarations(): Declaration[];
51805169
>getNamedDeclarations : () => Declaration[]
51815170
>Declaration : Declaration

0 commit comments

Comments
 (0)