Skip to content

Commit

Permalink
Add setting "eslint.globalSeverity"
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashraf Hadden committed Oct 19, 2018
1 parent ebe1cb6 commit 2ae068a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
2 changes: 2 additions & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ interface TextDocumentSettings {
autoFix: boolean;
autoFixOnSave: boolean;
options: any | undefined;
globalSeverity: object;
run: RunValues;
nodePath: string | undefined;
workspaceFolder: WorkspaceFolder | undefined;
Expand Down Expand Up @@ -463,6 +464,7 @@ export function realActivate(context: ExtensionContext) {
autoFix: false,
autoFixOnSave: false,
options: config.get('options', {}),
globalSeverity: config.get('globalSeverity'),
run: config.get('run', 'onType'),
nodePath: config.get('nodePath', undefined),
workingDirectory: undefined,
Expand Down
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,52 @@
"default": {},
"description": "The eslint options object to provide args normally passed to eslint when executed from a command line (see http://eslint.org/docs/developer-guide/nodejs-api#cliengine)."
},
"eslint.globalSeverity": {
"examples": [
null,
0
],
"description": "Force a global rule severity. Overrides all ESLint rules.\n\"severity\": null = Deactivate globalSeverity.\nEx.\n\t\"eslint.globalSeverity\": {\n\t\t\"severity\": \"warn\",\n\t\t\"exclude\": [\"no-console\", \"no-debugger\"]\n\t}",
"markdownDescription": "Force a global rule severity. Overrides all ESLint rules.\n• `\"severity\": null` = Deactivate globalSeverity.\nEx.`\n\t\"eslint.globalSeverity\": {\n\t\t\"severity\": \"warn\",\n\t\t\"exclude\": [\"no-console\", \"no-debugger\"]\n\t}`",
"scope": "resource",
"type": "object",
"default": {
"severity": null,
"exclude": []
},
"properties": {
"severity": {
"markdownDescription": "Global rule severity.\nSee https://eslint.org/docs/user-guide/getting-started#configuration",
"type": [
"null",
"number",
"string"
],
"enum": [
null,
0,
1,
2,
"off",
"warn",
"error"
],
"enumDescriptions": [
"Deactivate globalSeverity",
"Turn off all ESLint rules",
"Set all ESLint rules to severity \"warn\"",
"Set all ESLint rules to severity \"error\"",
"Turn off all ESLint rules",
"Set all ESLint rules to severity \"warn\"",
"Set all ESLint rules to severity \"error\""
]
},
"exclude": {
"markdownDescription": "A string array of rule ID/names to exclude from this setting. Ex. `[\"no-console\", \"no-debugger\"]`",
"type": "array"
}
}
},
"eslint.trace.server": {
"scope": "window",
"anyOf": [
Expand Down
34 changes: 29 additions & 5 deletions server/src/eslintServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,18 @@ namespace DirectoryItem {
}
}

interface GlobalSeverity {
"severity": null | number | string,
"exclude": Array<String>,
}

interface TextDocumentSettings {
validate: boolean;
packageManager: 'npm' | 'yarn';
autoFix: boolean;
autoFixOnSave: boolean;
options: any | undefined;
globalSeverity: GlobalSeverity;
run: RunValues;
nodePath: string | undefined;
workspaceFolder: WorkspaceFolder | undefined;
Expand Down Expand Up @@ -157,15 +163,15 @@ interface ESLintModule {
CLIEngine: CLIEngineConstructor;
}

function makeDiagnostic(problem: ESLintProblem): Diagnostic {
function makeDiagnostic(problem: ESLintProblem, settings: TextDocumentSettings): Diagnostic {
let message = problem.message;
let startLine = Math.max(0, problem.line - 1);
let startChar = Math.max(0, problem.column - 1);
let endLine = problem.endLine != null ? Math.max(0, problem.endLine - 1) : startLine;
let endChar = problem.endColumn != null ? Math.max(0, problem.endColumn - 1) : startChar;
return {
message: message,
severity: convertSeverity(problem.severity),
severity: computeSeverity(problem, settings),
source: 'eslint',
range: {
start: { line: startLine, character: startChar },
Expand Down Expand Up @@ -201,18 +207,34 @@ function recordCodeAction(document: TextDocument, diagnostic: Diagnostic, proble
edits.set(computeKey(diagnostic), { label: `Fix this ${problem.ruleId} problem`, documentVersion: document.version, ruleId: problem.ruleId, edit: problem.fix });
}

function convertSeverity(severity: number): DiagnosticSeverity {
function convertSeverity(severity: null | number | string): null | DiagnosticSeverity {
switch (severity) {
// Eslint 1 is warning
case 0:
case "off":
return null;
case 1:
case "warn":
return DiagnosticSeverity.Warning;
case 2:
case "error":
return DiagnosticSeverity.Error;
default:
return DiagnosticSeverity.Error;
}
}

function computeSeverity(problem: ESLintProblem, settings: TextDocumentSettings) {
const severity = settings.globalSeverity.severity;
const isException = settings.globalSeverity.exclude.includes(problem.ruleId);
if ((severity === null) || (isException === true)) {
return convertSeverity(problem.severity);
}
else {
return convertSeverity(severity);
}
}

const enum CharCode {
/**
* The `\` character.
Expand Down Expand Up @@ -785,8 +807,10 @@ function validate(document: TextDocument, settings: TextDocumentSettings, publis
if (docReport.messages && Array.isArray(docReport.messages)) {
docReport.messages.forEach((problem) => {
if (problem) {
let diagnostic = makeDiagnostic(problem);
diagnostics.push(diagnostic);
let diagnostic = makeDiagnostic(problem, settings);
if (diagnostic.severity !== null) {
diagnostics.push(diagnostic);
}
if (settings.autoFix) {
recordCodeAction(document, diagnostic, problem);
}
Expand Down
2 changes: 1 addition & 1 deletion server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"sourceMap": true,
"outDir": "out",
"rootDir": "src",
"lib": [ "es6" ]
"lib": [ "es6", "es7" ]
},
"include": [
"src"
Expand Down

0 comments on commit 2ae068a

Please sign in to comment.