Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when diagnostic is missing range #1174

Merged
merged 2 commits into from
May 10, 2024
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
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