Skip to content

Guard transpileComments against undefined trivia tokens#1725

Merged
TwitchBronBron merged 2 commits into
v1from
fix/transpile-comments-undefined-tokens
May 29, 2026
Merged

Guard transpileComments against undefined trivia tokens#1725
TwitchBronBron merged 2 commits into
v1from
fix/transpile-comments-undefined-tokens

Conversation

@TwitchBronBron
Copy link
Copy Markdown
Member

Summary

Fixes a crash during transpile when an @annotation has 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 sub
TypeError: Cannot read properties of undefined (reading 'filter')
    at BrsTranspileState.transpileComments (src/parser/TranspileState.ts:144)
    at AnnotationExpression.transpile (src/parser/Expression.ts:2266)
    at BrsTranspileState.transpileAnnotations (src/parser/BrsTranspileState.ts:54)
    at FunctionStatement.transpile (src/parser/Statement.ts:631)
    ...
    at BscPlugin.afterSerializeFile (src/bscPlugin/BscPlugin.ts:119)

Root cause

Lexer.scanToken (src/lexer/Lexer.ts:1082) initializes every token's leadingTrivia to undefined and only assigns an array when trivia accumulated before the token. So a token with no preceding whitespace/newlines/comments keeps leadingTrivia === undefined.

AnnotationExpression.leadingTrivia returns this.tokens.at?.leadingTriviaundefined in that case — and AnnotationExpression.transpile forwards it directly to state.transpileComments, which calls .filter on it without a guard.

Fix

Add a nullish guard at the top of transpileComments, matching the existing guard in transpileLeadingComments (same file, line 130) and the nullish-tolerant trivia helpers in util.ts (hasLeadingComments, getLeadingComments).

Test plan

  • New failing test in BrsFile.spec.ts (does not crash when annotation has undefined leadingTrivia) reproduces the crash with the exact stack trace above on v1 HEAD.
  • Test passes after the fix.
  • Full suite green: 4179 passing, 9 pending, 0 failing.

🤖 Generated with Claude Code

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.
@TwitchBronBron TwitchBronBron merged commit 19da57b into v1 May 29, 2026
11 checks passed
@TwitchBronBron TwitchBronBron deleted the fix/transpile-comments-undefined-tokens branch May 29, 2026 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant