From 4b62ba9017408a2a8e703e00e7bae0991ac813dc Mon Sep 17 00:00:00 2001 From: Keen Yee Liau Date: Mon, 4 Nov 2019 17:15:07 -0800 Subject: [PATCH] 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 --- .../compiler-cli/src/ngtsc/shims/src/factory_generator.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/shims/src/factory_generator.ts b/packages/compiler-cli/src/ngtsc/shims/src/factory_generator.ts index 945229f37827a..42377926ed620 100644 --- a/packages/compiler-cli/src/ngtsc/shims/src/factory_generator.ts +++ b/packages/compiler-cli/src/ngtsc/shims/src/factory_generator.ts @@ -59,8 +59,12 @@ export class FactoryGenerator implements ShimGenerator { let comment: string = ''; if (original.statements.length > 0) { const firstStatement = original.statements[0]; - if (firstStatement.getLeadingTriviaWidth() > 0) { - comment = firstStatement.getFullText().substr(0, firstStatement.getLeadingTriviaWidth()); + // Must pass SourceFile to getLeadingTriviaWidth(), otherwise it'll try to + // get SourceFile by recursively looking up the parent of the Node and fail, + // because parent is undefined. + const leadingTriviaWidth = firstStatement.getLeadingTriviaWidth(original); + if (leadingTriviaWidth > 0) { + comment = firstStatement.getFullText().substr(0, leadingTriviaWidth); } }