Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
flattened.push(lintRule);
return { flat: flattened, tree: lintRule };
}
/**
* Settable severity
*/
export type SettableSeverity = 'error' | 'warning' | 'info' | 'hint' | 'off';
/**
* Map from settable severity to diagnostic severity
*/
export const severityMapper = new Map([
['error', DiagnosticSeverity.Error],
['warning', DiagnosticSeverity.Warning],
['info', DiagnosticSeverity.Information],
['hint', DiagnosticSeverity.Hint],
]);
/**
* Lint rules in data form
*/
const lintTemplates: LintRule = {
rule: 'all',
level: 'warning',
diagnostics: [],
owned: [
{
rule: 'scanning',
level: 'error',
diagnostics: [DIAGNOSTICS.SCANNER_ERROR],
owned: [],
function toDiagnosticSeverity(severity) {
switch (severity) {
case 0:
return DiagnosticSeverity.Information;
case 1:
return DiagnosticSeverity.Warning;
case 2:
return DiagnosticSeverity.Error;
}
return DiagnosticSeverity.Information;
}
import { DiagnosticLevel } from '@lwc/errors';
import templateCompiler from '@lwc/template-compiler';
import { TextDocument, Diagnostic, DiagnosticSeverity, Range } from 'vscode-languageserver';
import { DIAGNOSTIC_SOURCE } from '../constants';
const LEVEL_MAPPING: Map = new Map([
[DiagnosticLevel.Log, DiagnosticSeverity.Information],
[DiagnosticLevel.Warning, DiagnosticSeverity.Warning],
[DiagnosticLevel.Error, DiagnosticSeverity.Error],
[DiagnosticLevel.Fatal, DiagnosticSeverity.Error],
]);
export default function lint(document: TextDocument): Diagnostic[] {
const source = document.getText();
const { warnings } = templateCompiler(source, {});
return warnings.map(warning => {
const { start = 0, length = 0 } = warning.location || { start: 0, length: 0 };
return {
range: toRange(document, start, length),
message: warning.message,
severity: LEVEL_MAPPING.get(warning.level),
export function getDiagnosticSeverity(severity: string): DiagnosticSeverity {
switch (severity) {
case 'error':
return DiagnosticSeverity.Error;
case 'warning':
return DiagnosticSeverity.Warning;
case 'info':
return DiagnosticSeverity.Information;
default:
return DiagnosticSeverity.Error;
}
}
const webhintToDiagnosticServerity = (severity: Severity): DiagnosticSeverity => {
switch (severity) {
case 4:
return DiagnosticSeverity.Error;
case 3:
return DiagnosticSeverity.Warning;
case 2:
return DiagnosticSeverity.Hint;
case 1:
return DiagnosticSeverity.Information;
default:
return DiagnosticSeverity.Information;
}
};
private loadError(range: Range, path: string) {
return createDiagnostic(
range,
`Unable to load script at ${path}`,
DiagnosticSeverity.Information,
DIAGNOSTICS.LOAD_ERROR,
);
}
public getDiagnostics(request: TextDocument): Diagnostic[] {
let program: Program = this.getProgram(request.uri);
if (!program) {
return [];
}
let diagnostics: Diagnostic[] = [];
let errors: Error[] = program.getErrors();
for (let error of errors) {
let severity: DiagnosticSeverity = [
null,
DiagnosticSeverity.Error,
DiagnosticSeverity.Warning,
DiagnosticSeverity.Information,
DiagnosticSeverity.Hint
][error.severity];
let diagnostic: Diagnostic = {
range: error.range,
severity: severity,
message: error.message
};
diagnostics.push(diagnostic);
}
return diagnostics;
}
function makeDiagnostic(problem): Diagnostic {
const source = problem.usage.source;
const message: string = problem.message.replace(/<input>:\d*:\d*:\s/, '');
const severityLevel = {
Error: DiagnosticSeverity.Error,
Information: DiagnosticSeverity.Information,
Warning: DiagnosticSeverity.Warning
};
return {
severity: severityLevel[editorSettings.messageLevel],
message,
range: {
start: {
line: source.start.line - 1,
character: source.start.column - 1
},
end: {
line: source.end.line - 1,
character: source.end.column
}
},
convertSeverity(severity: Severity): DiagnosticSeverity {
switch (severity) {
case Severity.ERROR:
return DiagnosticSeverity.Error;
case Severity.WARNING:
return DiagnosticSeverity.Warning;
case Severity.INFO:
return DiagnosticSeverity.Information;
default:
throw new Error(
`This should never happen. Got a severity of ${severity}`);
}
}