Guard transpileComments against undefined trivia tokens#1725
Merged
Conversation
The lexer emits a token's `leadingTrivia` as `undefined` (rather than `[]`) when no whitespace, newlines, or comments precede it. An `@annotation` whose `at` token has no preceding trivia therefore has `leadingTrivia === undefined`, which `AnnotationExpression.transpile` forwards directly to `state.transpileComments(this.leadingTrivia)`, crashing with `TypeError: Cannot read properties of undefined (reading 'filter')`. Match the existing guard in `transpileLeadingComments` and the nullish-tolerant helpers in `util.ts` (`hasLeadingComments`, `getLeadingComments`) by returning early when `tokens` is nullish.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a crash during transpile when an
@annotationhas no leading trivia (whitespace, newline, or comment) before its@token — typical for annotations that appear at column 0 of a file.Repro
@annotation sub main() print "main" end subRoot cause
Lexer.scanToken(src/lexer/Lexer.ts:1082) initializes every token'sleadingTriviatoundefinedand only assigns an array when trivia accumulated before the token. So a token with no preceding whitespace/newlines/comments keepsleadingTrivia === undefined.AnnotationExpression.leadingTriviareturnsthis.tokens.at?.leadingTrivia—undefinedin that case — andAnnotationExpression.transpileforwards it directly tostate.transpileComments, which calls.filteron it without a guard.Fix
Add a nullish guard at the top of
transpileComments, matching the existing guard intranspileLeadingComments(same file, line 130) and the nullish-tolerant trivia helpers inutil.ts(hasLeadingComments,getLeadingComments).Test plan
BrsFile.spec.ts(does not crash when annotation has undefined leadingTrivia) reproduces the crash with the exact stack trace above onv1HEAD.4179 passing, 9 pending, 0 failing.🤖 Generated with Claude Code