Skip to content

Commit 4b62ba9

Browse files
Keen Yee Liauatscott
authored andcommitted
fix(compiler-cli): Pass SourceFile to getLeadingTriviaWidth (#33588)
This commit fixes a crash in the Angular Kythe indexer caused by failure to retrieve `SourceFile` in a `Statement`. Crash logs: TypeError: Cannot read property 'text' of undefined at Object.getTokenPosOfNode (typescript/stable/lib/typescript.js:8957:72) at NodeObject.getStart (typescript/stable/lib/typescript.js:121419:23) at NodeObject.getLeadingTriviaWidth (typescript/stable/lib/typescript.js:121439:25) at FactoryGenerator.generate (angular2/rc/packages/compiler-cli/src/ngtsc/shims/src/factory_generator.ts:64:49) at GeneratedShimsHostWrapper.getSourceFile (angular2/rc/packages/compiler-cli/src/ngtsc/shims/src/host.ts:88:26) at findSourceFile (typescript/stable/lib/typescript.js:90654:29) at typescript/stable/lib/typescript.js:90553:85 at getSourceFileFromReferenceWorker (typescript/stable/lib/typescript.js:90520:34) at processSourceFile (typescript/stable/lib/typescript.js:90553:13) at processRootFile (typescript/stable/lib/typescript.js:90383:13) PR Close #33588
1 parent 0addaab commit 4b62ba9

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

packages/compiler-cli/src/ngtsc/shims/src/factory_generator.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ export class FactoryGenerator implements ShimGenerator {
5959
let comment: string = '';
6060
if (original.statements.length > 0) {
6161
const firstStatement = original.statements[0];
62-
if (firstStatement.getLeadingTriviaWidth() > 0) {
63-
comment = firstStatement.getFullText().substr(0, firstStatement.getLeadingTriviaWidth());
62+
// Must pass SourceFile to getLeadingTriviaWidth(), otherwise it'll try to
63+
// get SourceFile by recursively looking up the parent of the Node and fail,
64+
// because parent is undefined.
65+
const leadingTriviaWidth = firstStatement.getLeadingTriviaWidth(original);
66+
if (leadingTriviaWidth > 0) {
67+
comment = firstStatement.getFullText().substr(0, leadingTriviaWidth);
6468
}
6569
}
6670

0 commit comments

Comments
 (0)