diff --git a/src/util.spec.ts b/src/util.spec.ts index cef085b78..c3c55cb84 100644 --- a/src/util.spec.ts +++ b/src/util.spec.ts @@ -7,9 +7,10 @@ import * as fsExtra from 'fs-extra'; import { createSandbox } from 'sinon'; import { DiagnosticMessages } from './DiagnosticMessages'; import { tempDir, rootDir } from './testHelpers.spec'; +import { Program } from './Program'; import { TypeChainEntry } from './interfaces'; -import { ClassType } from './types/ClassType'; import { NamespaceType } from './types/NamespaceType'; +import { ClassType } from './types/ClassType'; import { ReferenceType } from './types/ReferenceType'; import { SymbolTypeFlag } from './SymbolTable'; @@ -43,6 +44,31 @@ describe('util', () => { }); }); + describe('diagnosticIsSuppressed', () => { + it('does not crash when diagnostic is missing location information', () => { + const program = new Program({}); + const file = program.setFile('source/main.brs', ''); + const diagnostic = { + file: file, + message: 'crash', + //important part of the test. range must be missing + range: undefined + }; + + file.commentFlags.push({ + affectedRange: util.createRange(1, 2, 3, 4), + codes: [1, 2, 3], + file: file, + range: util.createRange(1, 2, 3, 4) + }); + file.diagnostics.push(diagnostic); + + util.diagnosticIsSuppressed(diagnostic); + + //test passes if there's no crash + }); + }); + describe('getRokuPkgPath', () => { it('replaces more than one windows slash in a path', () => { expect(util.getRokuPkgPath('source\\folder1\\folder2\\file.brs')).to.eql('pkg:/source/folder1/folder2/file.brs'); diff --git a/src/util.ts b/src/util.ts index 318198015..bfd5c3074 100644 --- a/src/util.ts +++ b/src/util.ts @@ -708,7 +708,7 @@ export class Util { const diagnosticCode = typeof diagnostic.code === 'string' ? diagnostic.code.toLowerCase() : diagnostic.code; for (let flag of diagnostic.file?.commentFlags ?? []) { //this diagnostic is affected by this flag - if (this.rangeContains(flag.affectedRange, diagnostic.range.start)) { + if (diagnostic.range && this.rangeContains(flag.affectedRange, diagnostic.range.start)) { //if the flag acts upon this diagnostic's code if (flag.codes === null || flag.codes.includes(diagnosticCode)) { return true;