Skip to content

Commit

Permalink
Fix crash when diagnostic is missing range
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed May 10, 2024
1 parent d785e86 commit 6b9b766
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/ProgramBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@ describe('ProgramBuilder', () => {

describe('printDiagnostics', () => {

it('does not crash when a diagnostic is missing range informtaion', () => {
const file = builder.program.setFile('source/main.brs', ``);
file.addDiagnostics([{
message: 'the message',
code: 'test1'
}, {
message: 'the message',
code: 'test1'
}] as any);
//if this doesn't crash, then the test passes
builder['printDiagnostics']();
});

it('prints no diagnostics when showDiagnosticsInConsole is false', () => {
builder.options.showDiagnosticsInConsole = false;

Expand Down
4 changes: 2 additions & 2 deletions src/ProgramBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ export class ProgramBuilder {
//sort the diagnostics in line and column order
let sortedDiagnostics = diagnosticsForFile.sort((a, b) => {
return (
a.range.start.line - b.range.start.line ||
a.range.start.character - b.range.start.character
(a.range?.start.line ?? -1) - (b.range?.start.line ?? -1) ||
(a.range?.start.character ?? -1) - (b.range?.start.character ?? -1)
);
});

Expand Down
12 changes: 7 additions & 5 deletions src/files/BrsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,16 @@ export class BrsFile {
}

public addDiagnostic(diagnostic: Diagnostic & { file?: BscFile }) {
if (!diagnostic.file) {
diagnostic.file = this;
}
this.diagnostics.push(diagnostic as any);
this.addDiagnostics([diagnostic as BsDiagnostic]);
}

public addDiagnostics(diagnostics: BsDiagnostic[]) {
this.diagnostics.push(...diagnostics);
for (const diagnostic of diagnostics) {
if (!diagnostic.file) {
diagnostic.file = this;
}
this.diagnostics.push(diagnostic as any);
}
}

public commentFlags = [] as CommentFlag[];
Expand Down

0 comments on commit 6b9b766

Please sign in to comment.