Skip to content

Commit

Permalink
Add 'severityOverride' option
Browse files Browse the repository at this point in the history
Fixes #724
  • Loading branch information
philippe-wm committed Oct 21, 2022
1 parent 0aa6537 commit 790e337
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bsconfig.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@
]
}
},
"severityOverride": {
"description": "A map of error codes with their severity level override (error|warn|info)",
"type": "object",
"patternProperties": {
".{1,}": {
"type": ["number", "string"],
"enum": ["error", "warn", "info", "hint"]
}
}
},
"emitFullPaths": {
"description": "Emit full paths to files when printing diagnostics to the console.",
"type": "boolean",
Expand Down
5 changes: 5 additions & 0 deletions src/BsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ export interface BsConfig {
*/
ignoreErrorCodes?: (number | string)[];

/**
* A map of error codes with their severity level override (error|warn|info)
*/
severityOverride?: Record<number | string, 'error' | 'warn' | 'info' | 'hint'>;

/**
* Emit full paths to files when printing diagnostics to the console. Defaults to false
*/
Expand Down
53 changes: 53 additions & 0 deletions src/DiagnosticSeverityAdjuster.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { DiagnosticSeverity } from 'vscode-languageserver-protocol';
import { expect } from './chai-config.spec';

import { DiagnosticSeverityAdjuster } from './DiagnosticSeverityAdjuster';
import type { BsDiagnostic } from './interfaces';

describe('DiagnosticSeverityAdjuster', () => {
const adjuster = new DiagnosticSeverityAdjuster();

it('supports empty map', () => {
const actual = adjuster.createSeverityMap({});
expect(Array.from(actual.keys()).length === 0);
});

it('maps strings to enums', () => {
const actual = adjuster.createSeverityMap({
'a': 'error',
'b': 'warn',
'c': 'info',
1001: 'hint',
// @ts-expect-error using invalid key
'e': 'foo',
// @ts-expect-error using invalid key
'f': 42
});
expect(actual.get('a')).to.equal(DiagnosticSeverity.Error);
expect(actual.get('b')).to.equal(DiagnosticSeverity.Warning);
expect(actual.get('c')).to.equal(DiagnosticSeverity.Information);
expect(actual.get('1001')).to.equal(DiagnosticSeverity.Hint);
expect(actual.get('e')).to.equal(undefined);
expect(actual.get('f')).to.equal(undefined);
});

it('adjusts severity', () => {
const diagnostics = [
{
code: 'BSLINT1001',
severity: DiagnosticSeverity.Error
} as BsDiagnostic, {
code: 1001,
severity: DiagnosticSeverity.Error
} as BsDiagnostic
];
adjuster.adjust({
severityOverride: {
'BSLINT1001': 'warn',
1001: 'info'
}
}, diagnostics);
expect(diagnostics[0].severity).to.equal(DiagnosticSeverity.Warning);
expect(diagnostics[1].severity).to.equal(DiagnosticSeverity.Information);
});
});
38 changes: 38 additions & 0 deletions src/DiagnosticSeverityAdjuster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { DiagnosticSeverity } from 'vscode-languageserver-protocol';
import type { BsConfig } from './BsConfig';
import type { BsDiagnostic } from './interfaces';

export class DiagnosticSeverityAdjuster {
public adjust(options: BsConfig, diagnostics: BsDiagnostic[]): void {
const map = this.createSeverityMap(options.severityOverride);

diagnostics.forEach(diagnostic => {
const code = String(diagnostic.code);
if (map.has(code)) {
diagnostic.severity = map.get(code);
}
});
}

public createSeverityMap(severityOverride: BsConfig['severityOverride']): Map<string, DiagnosticSeverity> {
const map = new Map<string, DiagnosticSeverity>();
Object.keys(severityOverride).forEach(key => {
const value = severityOverride[key];
switch (value) {
case 'error':
map.set(key, DiagnosticSeverity.Error);
break;
case 'warn':
map.set(key, DiagnosticSeverity.Warning);
break;
case 'info':
map.set(key, DiagnosticSeverity.Information);
break;
case 'hint':
map.set(key, DiagnosticSeverity.Hint);
break;
}
});
return map;
}
}
7 changes: 7 additions & 0 deletions src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { rokuDeploy } from 'roku-deploy';
import type { Statement } from './parser/AstNode';
import { CallExpressionInfo } from './bscPlugin/CallExpressionInfo';
import { SignatureHelpUtil } from './bscPlugin/SignatureHelpUtil';
import { DiagnosticSeverityAdjuster } from './DiagnosticSeverityAdjuster';

const startOfSourcePkgPath = `source${path.sep}`;
const bslibNonAliasedRokuModulesPkgPath = s`source/roku_modules/rokucommunity_bslib/bslib.brs`;
Expand Down Expand Up @@ -102,6 +103,8 @@ export class Program {

private diagnosticFilterer = new DiagnosticFilterer();

private diagnosticAdjuster = new DiagnosticSeverityAdjuster();

/**
* A scope that contains all built-in global functions.
* All scopes should directly or indirectly inherit from this scope
Expand Down Expand Up @@ -288,6 +291,10 @@ export class Program {
return finalDiagnostics;
});

this.logger.time(LogLevel.debug, ['adjust diagnostics severity'], () => {
this.diagnosticAdjuster.adjust(this.options, diagnostics);
});

this.logger.info(`diagnostic counts: total=${chalk.yellow(diagnostics.length.toString())}, after filter=${chalk.yellow(filteredDiagnostics.length.toString())}`);
return filteredDiagnostics;
});
Expand Down
1 change: 1 addition & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ export class Util {
config.retainStagingFolder = config.retainStagingDir;
config.copyToStaging = config.copyToStaging === false ? false : true;
config.ignoreErrorCodes = config.ignoreErrorCodes ?? [];
config.severityOverride = config.severityOverride ?? {};
config.diagnosticFilters = config.diagnosticFilters ?? [];
config.plugins = config.plugins ?? [];
config.autoImportComponentScript = config.autoImportComponentScript === true ? true : false;
Expand Down

0 comments on commit 790e337

Please sign in to comment.