Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3628,6 +3628,21 @@ describe('BrsFile', () => {
`, undefined, 'source/main.bs');
});

it('does not crash when annotation has undefined leadingTrivia', async () => {
//the lexer emits leadingTrivia as `undefined` (rather than `[]`) when a token has no preceding trivia,
//so an `@annotation` whose `at` token has no preceding whitespace/comments/newlines produces
//`at.leadingTrivia === undefined`. `AnnotationExpression.transpile` forwards that directly to
//`state.transpileComments`, which must tolerate a nullish input rather than crashing on `.filter`
//of undefined. Feed source that starts at column 0 with no leading newline to ensure no trivia
//is attached to the `@` token.
program.setFile('source/main.bs', `@annotation\nsub main()\n print "main"\nend sub`);
program.validate();
expectZeroDiagnostics(program);
//this should not throw
const result = await program.getTranspiledFileContents('source/main.bs');
expect(result.code).to.include('sub main()');
});

it('includes annotation comments for class', async () => {
await testTranspile(`
'comment1
Expand Down
3 changes: 3 additions & 0 deletions src/parser/TranspileState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ export class TranspileState {

public transpileComments(tokens: TranspileToken[], prepNextLine = false): Array<string | SourceNode> {
const leadingCommentsSourceNodes = [];
if (!tokens) {
return leadingCommentsSourceNodes;
}
const justComments = tokens.filter(t => t.kind === TokenKind.Comment || t.kind === TokenKind.Newline);
let newLinesSinceComment = 0;

Expand Down
Loading