Skip to content

Commit 06dd3f2

Browse files
authored
Fail fast on synthetic nodes in services (microsoft#19894)
1 parent 9d56f7b commit 06dd3f2

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/services/services.ts

+18
Original file line numberDiff line numberDiff line change
@@ -62,39 +62,52 @@ namespace ts {
6262
this.kind = kind;
6363
}
6464

65+
private assertHasRealPosition(message?: string) {
66+
// tslint:disable-next-line:debug-assert
67+
Debug.assert(!positionIsSynthesized(this.pos) && !positionIsSynthesized(this.end), message || "Node must have a real position for this operation");
68+
}
69+
6570
public getSourceFile(): SourceFile {
6671
return getSourceFileOfNode(this);
6772
}
6873

6974
public getStart(sourceFile?: SourceFileLike, includeJsDocComment?: boolean): number {
75+
this.assertHasRealPosition();
7076
return getTokenPosOfNode(this, sourceFile, includeJsDocComment);
7177
}
7278

7379
public getFullStart(): number {
80+
this.assertHasRealPosition();
7481
return this.pos;
7582
}
7683

7784
public getEnd(): number {
85+
this.assertHasRealPosition();
7886
return this.end;
7987
}
8088

8189
public getWidth(sourceFile?: SourceFile): number {
90+
this.assertHasRealPosition();
8291
return this.getEnd() - this.getStart(sourceFile);
8392
}
8493

8594
public getFullWidth(): number {
95+
this.assertHasRealPosition();
8696
return this.end - this.pos;
8797
}
8898

8999
public getLeadingTriviaWidth(sourceFile?: SourceFile): number {
100+
this.assertHasRealPosition();
90101
return this.getStart(sourceFile) - this.pos;
91102
}
92103

93104
public getFullText(sourceFile?: SourceFile): string {
105+
this.assertHasRealPosition();
94106
return (sourceFile || this.getSourceFile()).text.substring(this.pos, this.end);
95107
}
96108

97109
public getText(sourceFile?: SourceFile): string {
110+
this.assertHasRealPosition();
98111
if (!sourceFile) {
99112
sourceFile = this.getSourceFile();
100113
}
@@ -183,21 +196,25 @@ namespace ts {
183196
}
184197

185198
public getChildCount(sourceFile?: SourceFile): number {
199+
this.assertHasRealPosition();
186200
if (!this._children) this.createChildren(sourceFile);
187201
return this._children.length;
188202
}
189203

190204
public getChildAt(index: number, sourceFile?: SourceFile): Node {
205+
this.assertHasRealPosition();
191206
if (!this._children) this.createChildren(sourceFile);
192207
return this._children[index];
193208
}
194209

195210
public getChildren(sourceFile?: SourceFileLike): Node[] {
211+
this.assertHasRealPosition("Node without a real position cannot be scanned and thus has no token nodes - use forEachChild and collect the result if that's fine");
196212
if (!this._children) this.createChildren(sourceFile);
197213
return this._children;
198214
}
199215

200216
public getFirstToken(sourceFile?: SourceFile): Node {
217+
this.assertHasRealPosition();
201218
const children = this.getChildren(sourceFile);
202219
if (!children.length) {
203220
return undefined;
@@ -210,6 +227,7 @@ namespace ts {
210227
}
211228

212229
public getLastToken(sourceFile?: SourceFile): Node {
230+
this.assertHasRealPosition();
213231
const children = this.getChildren(sourceFile);
214232

215233
const child = lastOrUndefined(children);

0 commit comments

Comments
 (0)