Skip to content

Commit 7dba949

Browse files
Made 'createSourceFile' take a 'setParentPointers' parameter.
1 parent 95bf3a7 commit 7dba949

12 files changed

+131
-54
lines changed

src/compiler/helpers.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ module ts {
4242
return getSourceTextOfNodeFromSourceFile(getSourceFileOfNode(node), node);
4343
}
4444

45+
/**
46+
* Normally parent references are set during binding; however, running
47+
* the whole binding process is overkill when only using syntactic features.
48+
* Here we simply walk over the nodes and set parent references.
49+
*/
50+
export function initializeParentReferences(sourceFile: SourceFile) {
51+
var parent: Node = sourceFile;
52+
forEachChild(sourceFile, walk);
53+
54+
function walk(current: Node): void {
55+
current.parent = parent;
56+
57+
var saveParent = parent;
58+
parent = current;
59+
forEachChild(current, walk);
60+
parent = saveParent;
61+
}
62+
}
63+
4564
// Add an extra underscore to identifiers that start with two underscores to avoid issues with magic names like '__proto__'
4665
export function escapeIdentifier(identifier: string): string {
4766
return identifier.length >= 2 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ ? "_" + identifier : identifier;

src/compiler/parser.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ module ts {
8484
}
8585
text = "";
8686
}
87-
return text !== undefined ? createSourceFile(filename, text, languageVersion, /*version:*/ "0") : undefined;
87+
return text !== undefined ? createSourceFile(filename, text, languageVersion, /*setNodeParents*/ false, /*version:*/ "0") : undefined;
8888
}
8989

9090
function writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) {
@@ -396,7 +396,7 @@ module ts {
396396
return 0;
397397
}
398398

399-
export function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen: boolean = false): SourceFile {
399+
export function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, setNodeParents: boolean, version: string = "0", isOpen: boolean = false): SourceFile {
400400
var file: SourceFile;
401401
var scanner: Scanner;
402402
var token: SyntaxKind;
@@ -3678,6 +3678,11 @@ module ts {
36783678
file.isOpen = isOpen;
36793679
file.languageVersion = languageVersion;
36803680
file.identifiers = identifiers;
3681+
3682+
if (setNodeParents) {
3683+
initializeParentReferences(file);
3684+
}
3685+
36813686
return file;
36823687
}
36833688

src/harness/harness.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ module Harness {
540540
}
541541

542542
export var defaultLibFileName = 'lib.d.ts';
543-
export var defaultLibSourceFile = ts.createSourceFile(defaultLibFileName, IO.readFile(libFolder + 'lib.core.d.ts'), /*languageVersion*/ ts.ScriptTarget.Latest, /*version:*/ "0");
543+
export var defaultLibSourceFile = ts.createSourceFile(defaultLibFileName, IO.readFile(libFolder + 'lib.core.d.ts'), /*languageVersion*/ ts.ScriptTarget.Latest, /*setNodeParents*/ false);
544544

545545
// Cache these between executions so we don't have to re-parse them for every test
546546
export var fourslashFilename = 'fourslash.ts';
@@ -565,7 +565,7 @@ module Harness {
565565
function register(file: { unitName: string; content: string; }) {
566566
if (file.content !== undefined) {
567567
var filename = ts.normalizeSlashes(file.unitName);
568-
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, scriptTarget, /*version:*/ "0");
568+
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, scriptTarget, /*setNodeParents*/ false);
569569
}
570570
};
571571
inputFiles.forEach(register);
@@ -578,7 +578,7 @@ module Harness {
578578
}
579579
else if (fn === fourslashFilename) {
580580
var tsFn = 'tests/cases/fourslash/' + fourslashFilename;
581-
fourslashSourceFile = fourslashSourceFile || ts.createSourceFile(tsFn, Harness.IO.readFile(tsFn), scriptTarget, /*version*/ "0", /*isOpen*/ false);
581+
fourslashSourceFile = fourslashSourceFile || ts.createSourceFile(tsFn, Harness.IO.readFile(tsFn), scriptTarget, /*setNodeParents*/ false, /*version*/ "0", /*isOpen*/ false);
582582
return fourslashSourceFile;
583583
}
584584
else {
@@ -792,7 +792,7 @@ module Harness {
792792
var register = (file: { unitName: string; content: string; }) => {
793793
if (file.content !== undefined) {
794794
var filename = ts.normalizeSlashes(file.unitName);
795-
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, options.target, /*version:*/ "0");
795+
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, options.target, /*setNodeParents*/ false);
796796
}
797797
};
798798
inputFiles.forEach(register);

src/harness/harnessLanguageService.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,23 @@ module Harness.LanguageService {
109109
fileName: string,
110110
compilationSettings: ts.CompilerOptions,
111111
scriptSnapshot: ts.IScriptSnapshot,
112+
setNodeParents: boolean,
112113
version: string,
113114
isOpen: boolean): ts.SourceFile {
114-
return ts.createSourceFile(fileName, scriptSnapshot.getText(0, scriptSnapshot.getLength()), compilationSettings.target, version, isOpen);
115+
return ts.createSourceFile(fileName, scriptSnapshot.getText(0, scriptSnapshot.getLength()), compilationSettings.target, setNodeParents, version, isOpen);
115116
}
116117

117118
public updateDocument(
118119
document: ts.SourceFile,
119120
fileName: string,
120121
compilationSettings: ts.CompilerOptions,
121122
scriptSnapshot: ts.IScriptSnapshot,
123+
setNodeParents: boolean,
122124
version: string,
123125
isOpen: boolean,
124126
textChangeRange: ts.TextChangeRange
125127
): ts.SourceFile {
126-
return document.update(scriptSnapshot, version, isOpen, textChangeRange);
128+
return document.update(scriptSnapshot, version, setNodeParents, isOpen, textChangeRange);
127129
}
128130

129131
public releaseDocument(fileName: string, compilationSettings: ts.CompilerOptions): void {
@@ -264,7 +266,7 @@ module Harness.LanguageService {
264266

265267
/** Parse file given its source text */
266268
public parseSourceText(fileName: string, sourceText: ts.IScriptSnapshot): ts.SourceFile {
267-
return ts.createSourceFile(fileName, sourceText.getText(0, sourceText.getLength()), ts.ScriptTarget.Latest, "1", true);
269+
return ts.createSourceFile(fileName, sourceText.getText(0, sourceText.getLength()), ts.ScriptTarget.Latest, /*setNodeParents*/ false, "1", /*isOpen*/ true);
268270
}
269271

270272
/** Parse a file on disk given its fileName */

src/harness/projectsRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class ProjectRunner extends RunnerBase {
176176
else {
177177
var text = getSourceFileText(filename);
178178
if (text !== undefined) {
179-
sourceFile = ts.createSourceFile(filename, text, languageVersion, /*version:*/ "0");
179+
sourceFile = ts.createSourceFile(filename, text, languageVersion, /*setNodeParents*/ false, /*version:*/ "0");
180180
}
181181
}
182182

tests/baselines/reference/APISample_node_compile.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import ts = require("typescript");
66

7-
var sourceFile = ts.createSourceFile("file1.ts", "var x = 0;", ts.ScriptTarget.Latest, "0.0");
7+
var sourceFile = ts.createSourceFile("file1.ts", "var x = 0;", ts.ScriptTarget.Latest, /*setNodeParents*/ false, "0.0");
88

99
var program = ts.createProgram(["file1.ts"], {}, undefined);
1010
//// [typescript.d.ts]
@@ -1316,8 +1316,13 @@ declare module ts {
13161316
isNoDefaultLib?: boolean;
13171317
}
13181318
function getNodeConstructor(kind: SyntaxKind): new () => Node;
1319+
/**
1320+
* Creates a basic compiler host
1321+
* TODO (drosen): document this!
1322+
*/
1323+
function createCompilerHost(options: CompilerOptions): CompilerHost;
13191324
function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodes?: (nodes: Node[]) => T): T;
1320-
function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen?: boolean): SourceFile;
1325+
function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, setNodeParents: boolean, version?: string, isOpen?: boolean): SourceFile;
13211326
function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program;
13221327
}
13231328
declare module ts {
@@ -1367,7 +1372,7 @@ declare module ts {
13671372
interface SourceFile {
13681373
getScriptSnapshot(): IScriptSnapshot;
13691374
getNamedDeclarations(): Declaration[];
1370-
update(scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
1375+
update(scriptSnapshot: IScriptSnapshot, version: string, setParentPointers: boolean, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
13711376
}
13721377
/**
13731378
* Represents an immutable snapshot of a script at a specified time.Once acquired, the
@@ -1754,8 +1759,8 @@ declare module ts {
17541759
getClassificationsForLine(text: string, lexState: EndOfLineState, classifyKeywordsInGenerics?: boolean): ClassificationResult;
17551760
}
17561761
interface DocumentRegistry {
1757-
acquireDocument(filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean): SourceFile;
1758-
updateDocument(sourceFile: SourceFile, filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
1762+
acquireDocument(filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, setNodeParents: boolean, version: string, isOpen: boolean): SourceFile;
1763+
updateDocument(sourceFile: SourceFile, filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, setNodeParents: boolean, version: string, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
17591764
releaseDocument(filename: string, compilationSettings: CompilerOptions): void;
17601765
}
17611766
class ScriptElementKind {
@@ -1836,5 +1841,5 @@ export = ts;
18361841

18371842
//// [APISample_node_compile.js]
18381843
var ts = require("typescript");
1839-
var sourceFile = ts.createSourceFile("file1.ts", "var x = 0;", 2 /* Latest */, "0.0");
1844+
var sourceFile = ts.createSourceFile("file1.ts", "var x = 0;", 2 /* Latest */, false, "0.0");
18401845
var program = ts.createProgram(["file1.ts"], {}, undefined);

tests/baselines/reference/APISample_node_compile.types

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import ts = require("typescript");
44
>ts : typeof ts
55

6-
var sourceFile = ts.createSourceFile("file1.ts", "var x = 0;", ts.ScriptTarget.Latest, "0.0");
6+
var sourceFile = ts.createSourceFile("file1.ts", "var x = 0;", ts.ScriptTarget.Latest, /*setNodeParents*/ false, "0.0");
77
>sourceFile : ts.SourceFile
8-
>ts.createSourceFile("file1.ts", "var x = 0;", ts.ScriptTarget.Latest, "0.0") : ts.SourceFile
9-
>ts.createSourceFile : (filename: string, sourceText: string, languageVersion: ts.ScriptTarget, version: string, isOpen?: boolean) => ts.SourceFile
8+
>ts.createSourceFile("file1.ts", "var x = 0;", ts.ScriptTarget.Latest, /*setNodeParents*/ false, "0.0") : ts.SourceFile
9+
>ts.createSourceFile : (filename: string, sourceText: string, languageVersion: ts.ScriptTarget, setNodeParents: boolean, version?: string, isOpen?: boolean) => ts.SourceFile
1010
>ts : typeof ts
11-
>createSourceFile : (filename: string, sourceText: string, languageVersion: ts.ScriptTarget, version: string, isOpen?: boolean) => ts.SourceFile
11+
>createSourceFile : (filename: string, sourceText: string, languageVersion: ts.ScriptTarget, setNodeParents: boolean, version?: string, isOpen?: boolean) => ts.SourceFile
1212
>ts.ScriptTarget.Latest : ts.ScriptTarget
1313
>ts.ScriptTarget : typeof ts.ScriptTarget
1414
>ts : typeof ts
@@ -4151,6 +4151,16 @@ declare module ts {
41514151
>SyntaxKind : SyntaxKind
41524152
>Node : Node
41534153

4154+
/**
4155+
* Creates a basic compiler host
4156+
* TODO (drosen): document this!
4157+
*/
4158+
function createCompilerHost(options: CompilerOptions): CompilerHost;
4159+
>createCompilerHost : (options: CompilerOptions) => CompilerHost
4160+
>options : CompilerOptions
4161+
>CompilerOptions : CompilerOptions
4162+
>CompilerHost : CompilerHost
4163+
41544164
function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodes?: (nodes: Node[]) => T): T;
41554165
>forEachChild : <T>(node: Node, cbNode: (node: Node) => T, cbNodes?: (nodes: Node[]) => T) => T
41564166
>T : T
@@ -4166,12 +4176,13 @@ declare module ts {
41664176
>T : T
41674177
>T : T
41684178

4169-
function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen?: boolean): SourceFile;
4170-
>createSourceFile : (filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen?: boolean) => SourceFile
4179+
function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, setNodeParents: boolean, version?: string, isOpen?: boolean): SourceFile;
4180+
>createSourceFile : (filename: string, sourceText: string, languageVersion: ScriptTarget, setNodeParents: boolean, version?: string, isOpen?: boolean) => SourceFile
41714181
>filename : string
41724182
>sourceText : string
41734183
>languageVersion : ScriptTarget
41744184
>ScriptTarget : ScriptTarget
4185+
>setNodeParents : boolean
41754186
>version : string
41764187
>isOpen : boolean
41774188
>SourceFile : SourceFile
@@ -4361,11 +4372,12 @@ declare module ts {
43614372
>getNamedDeclarations : () => Declaration[]
43624373
>Declaration : Declaration
43634374

4364-
update(scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
4365-
>update : (scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TextChangeRange) => SourceFile
4375+
update(scriptSnapshot: IScriptSnapshot, version: string, setParentPointers: boolean, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
4376+
>update : (scriptSnapshot: IScriptSnapshot, version: string, setParentPointers: boolean, isOpen: boolean, textChangeRange: TextChangeRange) => SourceFile
43664377
>scriptSnapshot : IScriptSnapshot
43674378
>IScriptSnapshot : IScriptSnapshot
43684379
>version : string
4380+
>setParentPointers : boolean
43694381
>isOpen : boolean
43704382
>textChangeRange : TextChangeRange
43714383
>TextChangeRange : TextChangeRange
@@ -5391,26 +5403,28 @@ declare module ts {
53915403
interface DocumentRegistry {
53925404
>DocumentRegistry : DocumentRegistry
53935405

5394-
acquireDocument(filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean): SourceFile;
5395-
>acquireDocument : (filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean) => SourceFile
5406+
acquireDocument(filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, setNodeParents: boolean, version: string, isOpen: boolean): SourceFile;
5407+
>acquireDocument : (filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, setNodeParents: boolean, version: string, isOpen: boolean) => SourceFile
53965408
>filename : string
53975409
>compilationSettings : CompilerOptions
53985410
>CompilerOptions : CompilerOptions
53995411
>scriptSnapshot : IScriptSnapshot
54005412
>IScriptSnapshot : IScriptSnapshot
5413+
>setNodeParents : boolean
54015414
>version : string
54025415
>isOpen : boolean
54035416
>SourceFile : SourceFile
54045417

5405-
updateDocument(sourceFile: SourceFile, filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
5406-
>updateDocument : (sourceFile: SourceFile, filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TextChangeRange) => SourceFile
5418+
updateDocument(sourceFile: SourceFile, filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, setNodeParents: boolean, version: string, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
5419+
>updateDocument : (sourceFile: SourceFile, filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, setNodeParents: boolean, version: string, isOpen: boolean, textChangeRange: TextChangeRange) => SourceFile
54075420
>sourceFile : SourceFile
54085421
>SourceFile : SourceFile
54095422
>filename : string
54105423
>compilationSettings : CompilerOptions
54115424
>CompilerOptions : CompilerOptions
54125425
>scriptSnapshot : IScriptSnapshot
54135426
>IScriptSnapshot : IScriptSnapshot
5427+
>setNodeParents : boolean
54145428
>version : string
54155429
>isOpen : boolean
54165430
>textChangeRange : TextChangeRange

tests/baselines/reference/APISample_standalone_compile.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//// [APISample_standalone_compile.ts]
44

5-
var sourceFile = ts.createSourceFile("file1.ts", "var x = 0;", ts.ScriptTarget.Latest, "0.0");
5+
var sourceFile = ts.createSourceFile("file1.ts", "var x = 0;", ts.ScriptTarget.Latest, /*setNodeParents*/ false, "0.0");
66

77
var program = ts.createProgram(["file1.ts"], {}, undefined);
88
//// [typescriptServices.d.ts]
@@ -1314,8 +1314,13 @@ declare module ts {
13141314
isNoDefaultLib?: boolean;
13151315
}
13161316
function getNodeConstructor(kind: SyntaxKind): new () => Node;
1317+
/**
1318+
* Creates a basic compiler host
1319+
* TODO (drosen): document this!
1320+
*/
1321+
function createCompilerHost(options: CompilerOptions): CompilerHost;
13171322
function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodes?: (nodes: Node[]) => T): T;
1318-
function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen?: boolean): SourceFile;
1323+
function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, setNodeParents: boolean, version?: string, isOpen?: boolean): SourceFile;
13191324
function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program;
13201325
}
13211326
declare module ts {
@@ -1365,7 +1370,7 @@ declare module ts {
13651370
interface SourceFile {
13661371
getScriptSnapshot(): IScriptSnapshot;
13671372
getNamedDeclarations(): Declaration[];
1368-
update(scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
1373+
update(scriptSnapshot: IScriptSnapshot, version: string, setParentPointers: boolean, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
13691374
}
13701375
/**
13711376
* Represents an immutable snapshot of a script at a specified time.Once acquired, the
@@ -1752,8 +1757,8 @@ declare module ts {
17521757
getClassificationsForLine(text: string, lexState: EndOfLineState, classifyKeywordsInGenerics?: boolean): ClassificationResult;
17531758
}
17541759
interface DocumentRegistry {
1755-
acquireDocument(filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean): SourceFile;
1756-
updateDocument(sourceFile: SourceFile, filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
1760+
acquireDocument(filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, setNodeParents: boolean, version: string, isOpen: boolean): SourceFile;
1761+
updateDocument(sourceFile: SourceFile, filename: string, compilationSettings: CompilerOptions, scriptSnapshot: IScriptSnapshot, setNodeParents: boolean, version: string, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
17571762
releaseDocument(filename: string, compilationSettings: CompilerOptions): void;
17581763
}
17591764
class ScriptElementKind {
@@ -1832,5 +1837,5 @@ declare module ts {
18321837

18331838

18341839
//// [APISample_standalone_compile.js]
1835-
var sourceFile = ts.createSourceFile("file1.ts", "var x = 0;", 2 /* Latest */, "0.0");
1840+
var sourceFile = ts.createSourceFile("file1.ts", "var x = 0;", 2 /* Latest */, false, "0.0");
18361841
var program = ts.createProgram(["file1.ts"], {}, undefined);

0 commit comments

Comments
 (0)