Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,24 @@ module ts {
return 0;
}

export function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen: boolean = false): SourceFile {
function fixupParentReferences(sourceFile: SourceFile) {
// normally parent references are set during binding.
// however here SourceFile data is used only for syntactic features so running the whole binding process is an overhead.
// walk over the nodes and set parent references
var parent: Node = sourceFile;
function walk(n: Node): void {
n.parent = parent;

var saveParent = parent;
parent = n;
forEachChild(n, walk);
parent = saveParent;
}

forEachChild(sourceFile, walk);
}

export function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes = false): SourceFile {
var parsingContext: ParsingContext;
var identifiers: Map<string> = {};
var identifierCount = 0;
Expand Down Expand Up @@ -1131,10 +1148,13 @@ module ts {

sourceFile.nodeCount = nodeCount;
sourceFile.identifierCount = identifierCount;
sourceFile.version = version;
sourceFile.isOpen = isOpen;
sourceFile.languageVersion = languageVersion;
sourceFile.identifiers = identifiers;

if (setParentNodes) {
fixupParentReferences(sourceFile);
}

return sourceFile;

function setContextFlag(val: Boolean, flag: ParserContextFlags) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ module ts {
}
text = "";
}
return text !== undefined ? createSourceFile(filename, text, languageVersion, /*version:*/ "0") : undefined;
return text !== undefined ? createSourceFile(filename, text, languageVersion) : undefined;
}

function writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) {
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,6 @@ module ts {
nodeCount: number;
identifierCount: number;
symbolCount: number;
isOpen: boolean;
version: string;
languageVersion: ScriptTarget;
identifiers: Map<string>;
}
Expand Down
10 changes: 5 additions & 5 deletions src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,8 @@ module Harness {
}

export var defaultLibFileName = 'lib.d.ts';
export var defaultLibSourceFile = ts.createSourceFile(defaultLibFileName, IO.readFile(libFolder + 'lib.core.d.ts'), /*languageVersion*/ ts.ScriptTarget.Latest, /*version:*/ "0");
export var defaultES6LibSourceFile = ts.createSourceFile(defaultLibFileName, IO.readFile(libFolder + 'lib.core.es6.d.ts'), /*languageVersion*/ ts.ScriptTarget.Latest, /*version:*/ "0");
export var defaultLibSourceFile = ts.createSourceFile(defaultLibFileName, IO.readFile(libFolder + 'lib.core.d.ts'), /*languageVersion*/ ts.ScriptTarget.Latest);
export var defaultES6LibSourceFile = ts.createSourceFile(defaultLibFileName, IO.readFile(libFolder + 'lib.core.es6.d.ts'), /*languageVersion*/ ts.ScriptTarget.Latest);


// Cache these between executions so we don't have to re-parse them for every test
Expand All @@ -565,7 +565,7 @@ module Harness {
function register(file: { unitName: string; content: string; }) {
if (file.content !== undefined) {
var filename = ts.normalizeSlashes(file.unitName);
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, scriptTarget, /*version:*/ "0");
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, scriptTarget);
}
};
inputFiles.forEach(register);
Expand All @@ -579,7 +579,7 @@ module Harness {
}
else if (fn === fourslashFilename) {
var tsFn = 'tests/cases/fourslash/' + fourslashFilename;
fourslashSourceFile = fourslashSourceFile || ts.createSourceFile(tsFn, Harness.IO.readFile(tsFn), scriptTarget, /*version*/ "0", /*isOpen*/ false);
fourslashSourceFile = fourslashSourceFile || ts.createSourceFile(tsFn, Harness.IO.readFile(tsFn), scriptTarget);
return fourslashSourceFile;
}
else {
Expand Down Expand Up @@ -786,7 +786,7 @@ module Harness {
var register = (file: { unitName: string; content: string; }) => {
if (file.content !== undefined) {
var filename = ts.normalizeSlashes(file.unitName);
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, options.target, /*version:*/ "0");
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, options.target);
}
};
inputFiles.forEach(register);
Expand Down
10 changes: 8 additions & 2 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ module Harness.LanguageService {
scriptSnapshot: ts.IScriptSnapshot,
version: string,
isOpen: boolean): ts.SourceFile {
return ts.createSourceFile(fileName, scriptSnapshot.getText(0, scriptSnapshot.getLength()), compilationSettings.target, version, isOpen);
var sourceFile = ts.createSourceFile(fileName, scriptSnapshot.getText(0, scriptSnapshot.getLength()), compilationSettings.target);
sourceFile.version = version;
sourceFile.isOpen = isOpen;
return sourceFile;
}

public updateDocument(
Expand Down Expand Up @@ -264,7 +267,10 @@ module Harness.LanguageService {

/** Parse file given its source text */
public parseSourceText(fileName: string, sourceText: ts.IScriptSnapshot): ts.SourceFile {
return ts.createSourceFile(fileName, sourceText.getText(0, sourceText.getLength()), ts.ScriptTarget.Latest, "1", true);
var result = ts.createSourceFile(fileName, sourceText.getText(0, sourceText.getLength()), ts.ScriptTarget.Latest);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why set to true all the time. this method is used for both semantic and syntactic features. we only need to set hte parents in the syntactic ones.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielRosenwasser had a fix for this today..

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually based on a failed cherry pick of that commit ;)

result.version = "1";
result.isOpen = true;
return result;
}

/** Parse a file on disk given its fileName */
Expand Down
2 changes: 1 addition & 1 deletion src/harness/projectsRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class ProjectRunner extends RunnerBase {
else {
var text = getSourceFileText(filename);
if (text !== undefined) {
sourceFile = ts.createSourceFile(filename, text, languageVersion, /*version:*/ "0");
sourceFile = ts.createSourceFile(filename, text, languageVersion);
}
}

Expand Down
25 changes: 6 additions & 19 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ module ts {
}

export interface SourceFile {
isOpen: boolean;
version: string;

getScriptSnapshot(): IScriptSnapshot;
getNamedDeclarations(): Declaration[];
update(scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TextChangeRange): SourceFile;
Expand Down Expand Up @@ -866,7 +869,9 @@ module ts {
}

public static createSourceFileObject(filename: string, scriptSnapshot: IScriptSnapshot, languageVersion: ScriptTarget, version: string, isOpen: boolean) {
var newSourceFile = <SourceFileObject><any>createSourceFile(filename, scriptSnapshot.getText(0, scriptSnapshot.getLength()), languageVersion, version, isOpen);
var newSourceFile = <SourceFileObject><any>createSourceFile(filename, scriptSnapshot.getText(0, scriptSnapshot.getLength()), languageVersion, /*setParentNodes:*/ true);
newSourceFile.version = version;
newSourceFile.isOpen = isOpen;
newSourceFile.scriptSnapshot = scriptSnapshot;
return newSourceFile;
}
Expand Down Expand Up @@ -1678,7 +1683,6 @@ module ts {
this.host.log("SyntaxTreeCache.Initialize: createSourceFile: " + (new Date().getTime() - start));

var start = new Date().getTime();
fixupParentReferences(sourceFile);
this.host.log("SyntaxTreeCache.Initialize: fixupParentRefs : " + (new Date().getTime() - start));
}
else if (this.currentFileVersion !== version) {
Expand All @@ -1693,7 +1697,6 @@ module ts {
this.host.log("SyntaxTreeCache.Initialize: updateSourceFile: " + (new Date().getTime() - start));

var start = new Date().getTime();
fixupParentReferences(sourceFile);
this.host.log("SyntaxTreeCache.Initialize: fixupParentRefs : " + (new Date().getTime() - start));
}

Expand All @@ -1703,22 +1706,6 @@ module ts {
this.currentFilename = filename;
this.currentSourceFile = sourceFile;
}

function fixupParentReferences(sourceFile: SourceFile) {
// normally parent references are set during binding.
// however here SourceFile data is used only for syntactic features so running the whole binding process is an overhead.
// walk over the nodes and set parent references
var parent: Node = sourceFile;
function walk(n: Node): void {
n.parent = parent;

var saveParent = parent;
parent = n;
forEachChild(n, walk);
parent = saveParent;
}
forEachChild(sourceFile, walk);
}
}

public getCurrentSourceFile(filename: string): SourceFile {
Expand Down