From 6b9b7669ea09980997595cbbf446784a1cb7c1ba Mon Sep 17 00:00:00 2001 From: Bronley Plumb Date: Fri, 10 May 2024 13:25:30 -0400 Subject: [PATCH 1/2] Fix crash when diagnostic is missing range --- src/ProgramBuilder.spec.ts | 13 +++++++++++++ src/ProgramBuilder.ts | 4 ++-- src/files/BrsFile.ts | 12 +++++++----- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/ProgramBuilder.spec.ts b/src/ProgramBuilder.spec.ts index cb085d3cb..1f435a54b 100644 --- a/src/ProgramBuilder.spec.ts +++ b/src/ProgramBuilder.spec.ts @@ -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; diff --git a/src/ProgramBuilder.ts b/src/ProgramBuilder.ts index c1d4aef1a..22fb60be2 100644 --- a/src/ProgramBuilder.ts +++ b/src/ProgramBuilder.ts @@ -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) ); }); diff --git a/src/files/BrsFile.ts b/src/files/BrsFile.ts index eb7b006bc..235796676 100644 --- a/src/files/BrsFile.ts +++ b/src/files/BrsFile.ts @@ -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[]; From 5ad683dbdf57ef312afa54e3cde1aae0af9473c2 Mon Sep 17 00:00:00 2001 From: Bronley Plumb Date: Fri, 10 May 2024 13:54:44 -0400 Subject: [PATCH 2/2] improve unit test --- src/ProgramBuilder.spec.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/ProgramBuilder.spec.ts b/src/ProgramBuilder.spec.ts index 1f435a54b..7492a3c56 100644 --- a/src/ProgramBuilder.spec.ts +++ b/src/ProgramBuilder.spec.ts @@ -250,14 +250,21 @@ describe('ProgramBuilder', () => { 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: 'message 1', + code: 'test1', + file: file }, { - message: 'the message', - code: 'test1' + 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', () => {