Skip to content

Commit

Permalink
Fix crash when diagnostic is missing range (#1174)
Browse files Browse the repository at this point in the history
* Fix crash when diagnostic is missing range

* improve unit test
  • Loading branch information
TwitchBronBron authored May 10, 2024
1 parent d785e86 commit 2d77dc6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
20 changes: 20 additions & 0 deletions src/ProgramBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,26 @@ 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: 'message 1',
code: 'test1',
file: file
}, {
message: 'message 2',
code: 'test1',
file: file
}] as any);
const stub = sinon.stub(diagnosticUtils, 'printDiagnostic').callsFake(() => { });
//if this doesn't crash, then the test passes
builder['printDiagnostics']();
expect(stub.getCalls().map(x => x.args[4].message)).to.eql([
'message 1',
'message 2'
]);
});

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 2d77dc6

Please sign in to comment.