Skip to content

Commit 24c40bb

Browse files
committed
added optional 'sourceFile' argument to methods of Node to avoid unnecesary tree walk
1 parent 7439ee3 commit 24c40bb

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

src/compiler/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ module ts {
5050
return node.pos;
5151
}
5252

53-
export function getTokenPosOfNode(node: Node): number {
54-
return skipTrivia(getSourceFileOfNode(node).text, node.pos);
53+
export function getTokenPosOfNode(node: Node, sourceFile?: SourceFile): number {
54+
return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos);
5555
}
5656

5757
export function getSourceTextOfNodeFromSourceText(sourceText: string, node: Node): string {

src/services/services.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@
2727
module ts {
2828
export interface Node {
2929
getSourceFile(): SourceFile;
30-
getChildCount(): number;
31-
getChildAt(index: number): Node;
32-
getChildren(): Node[];
33-
getStart(): number;
30+
getChildCount(sourceFile?: SourceFile): number;
31+
getChildAt(index: number, sourceFile?: SourceFile): Node;
32+
getChildren(sourceFile?: SourceFile): Node[];
33+
getStart(sourceFile?: SourceFile): number;
3434
getFullStart(): number;
3535
getEnd(): number;
36-
getWidth(): number;
36+
getWidth(sourceFile?: SourceFile): number;
3737
getFullWidth(): number;
38-
getLeadingTriviaWidth(): number;
39-
getFullText(): string;
40-
getFirstToken(): Node;
41-
getLastToken(): Node;
38+
getLeadingTriviaWidth(sourceFile?: SourceFile): number;
39+
getFullText(sourceFile?: SourceFile): string;
40+
getFirstToken(sourceFile?: SourceFile): Node;
41+
getLastToken(sourceFile?: SourceFile): Node;
4242
}
4343

4444
export interface Symbol {
@@ -100,8 +100,8 @@ module ts {
100100
return <SourceFile>node;
101101
}
102102

103-
public getStart(): number {
104-
return getTokenPosOfNode(this);
103+
public getStart(sourceFile?: SourceFile): number {
104+
return getTokenPosOfNode(this, sourceFile);
105105
}
106106

107107
public getFullStart(): number {
@@ -112,20 +112,20 @@ module ts {
112112
return this.end;
113113
}
114114

115-
public getWidth(): number {
116-
return this.getEnd() - this.getStart();
115+
public getWidth(sourceFile?: SourceFile): number {
116+
return this.getEnd() - this.getStart(sourceFile);
117117
}
118118

119119
public getFullWidth(): number {
120120
return this.end - this.getFullStart();
121121
}
122122

123-
public getLeadingTriviaWidth(): number {
124-
return this.getStart() - this.pos;
123+
public getLeadingTriviaWidth(sourceFile?: SourceFile): number {
124+
return this.getStart(sourceFile) - this.pos;
125125
}
126126

127-
public getFullText(): string {
128-
return this.getSourceFile().text.substring(this.pos, this.end);
127+
public getFullText(sourceFile?: SourceFile): string {
128+
return (sourceFile || this.getSourceFile()).text.substring(this.pos, this.end);
129129
}
130130

131131
private addSyntheticNodes(nodes: Node[], pos: number, end: number): number {
@@ -157,9 +157,9 @@ module ts {
157157
return list;
158158
}
159159

160-
private createChildren() {
160+
private createChildren(sourceFile?: SourceFile) {
161161
if (this.kind > SyntaxKind.Missing) {
162-
scanner.setText(this.getSourceFile().text);
162+
scanner.setText((sourceFile || this.getSourceFile()).text);
163163
var children: Node[] = [];
164164
var pos = this.pos;
165165
var processNode = (node: Node) => {
@@ -185,36 +185,36 @@ module ts {
185185
this._children = children || emptyArray;
186186
}
187187

188-
public getChildCount(): number {
189-
if (!this._children) this.createChildren();
188+
public getChildCount(sourceFile?: SourceFile): number {
189+
if (!this._children) this.createChildren(sourceFile);
190190
return this._children.length;
191191
}
192192

193-
public getChildAt(index: number): Node {
194-
if (!this._children) this.createChildren();
193+
public getChildAt(index: number, sourceFile?: SourceFile): Node {
194+
if (!this._children) this.createChildren(sourceFile);
195195
return this._children[index];
196196
}
197197

198-
public getChildren(): Node[] {
199-
if (!this._children) this.createChildren();
198+
public getChildren(sourceFile?: SourceFile): Node[] {
199+
if (!this._children) this.createChildren(sourceFile);
200200
return this._children;
201201
}
202202

203-
public getFirstToken(): Node {
204-
var children = this.getChildren();
203+
public getFirstToken(sourceFile?: SourceFile): Node {
204+
var children = this.getChildren(sourceFile);
205205
for (var i = 0; i < children.length; i++) {
206206
var child = children[i];
207207
if (child.kind < SyntaxKind.Missing) return child;
208-
if (child.kind > SyntaxKind.Missing) return child.getFirstToken();
208+
if (child.kind > SyntaxKind.Missing) return child.getFirstToken(sourceFile);
209209
}
210210
}
211211

212-
public getLastToken(): Node {
213-
var children = this.getChildren();
212+
public getLastToken(sourceFile?: SourceFile): Node {
213+
var children = this.getChildren(sourceFile);
214214
for (var i = children.length - 1; i >= 0; i--) {
215215
var child = children[i];
216216
if (child.kind < SyntaxKind.Missing) return child;
217-
if (child.kind > SyntaxKind.Missing) return child.getLastToken();
217+
if (child.kind > SyntaxKind.Missing) return child.getLastToken(sourceFile);
218218
}
219219
}
220220
}

0 commit comments

Comments
 (0)