-
Notifications
You must be signed in to change notification settings - Fork 13.3k
AST: getText, getStart and getEnd crash when using createProgram #14464
Description
TypeScript Version: 2.2.1
When walking the AST, it is possible to call getText() in scenario 1, but not in scenario 2
Scenario 1
sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES2016, /*setParentNodes */ true);
processFile(sourceFile);
Scenario 2
program = ts.createProgram(fileName, {});
sourceFile = program.getSourceFile(fileName);
processFile(sourceFile);
Expected behavior:
That node.getText() returns the text of an identifier both in scenario 1 and scenario 2.
Actual behavior:
Exception in TokenOrIdentifierObject.prototype.getText, most likely because this.parent is undefined, in scenario 2. Scenario 1 works as expected.
Additional comments
It also seems that some AST nodes have a text field containing the text, but this field is missing from the type declaration (the declared type is Node, but the internal type is sometimes a NodeObject and other times a TokenObject, both of which aren't declared and can't be used in a cast). Documentation would be most welcome.