Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make linter's severity levels configurable #941

Merged
merged 1 commit into from
Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,83 @@
"Warning"
]
},
"python.linting.pep8CategorySeverity.W": {
"type": "string",
"default": "Warning",
"description": "Severity of Pep8 message type 'W'.",
"enum": [
"Hint",
"Error",
"Information",
"Warning"
]
},
"python.linting.pep8CategorySeverity.E": {
"type": "string",
"default": "Error",
"description": "Severity of Pep8 message type 'E'.",
"enum": [
"Hint",
"Error",
"Information",
"Warning"
]
},
"python.linting.flake8CategorySeverity.F": {
"type": "string",
"default": "Error",
"description": "Severity of Flake8 message type 'F'.",
"enum": [
"Hint",
"Error",
"Information",
"Warning"
]
},
"python.linting.flake8CategorySeverity.E": {
"type": "string",
"default": "Error",
"description": "Severity of Flake8 message type 'E'.",
"enum": [
"Hint",
"Error",
"Information",
"Warning"
]
},
"python.linting.flake8CategorySeverity.W": {
"type": "string",
"default": "Warning",
"description": "Severity of Flake8 message type 'W'.",
"enum": [
"Hint",
"Error",
"Information",
"Warning"
]
},
"python.linting.mypyCategorySeverity.error": {
"type": "string",
"default": "Error",
"description": "Severity of Mypy message type 'Error'.",
"enum": [
"Hint",
"Error",
"Information",
"Warning"
]
},
"python.linting.mypyCategorySeverity.note": {
"type": "string",
"default": "Information",
"description": "Severity of Mypy message type 'Note'.",
"enum": [
"Hint",
"Error",
"Information",
"Warning"
]
},
"python.linting.prospectorPath": {
"type": "string",
"default": "prospector",
Expand Down
29 changes: 29 additions & 0 deletions src/client/common/configSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ export interface IPylintCategorySeverity {
error: vscode.DiagnosticSeverity;
fatal: vscode.DiagnosticSeverity;
}
export interface IPep8CategorySeverity {
W: vscode.DiagnosticSeverity;
E: vscode.DiagnosticSeverity;
}
export interface Flake8CategorySeverity {
F: vscode.DiagnosticSeverity;
E: vscode.DiagnosticSeverity;
W: vscode.DiagnosticSeverity;
}
export interface IMypyCategorySeverity {
error: vscode.DiagnosticSeverity;
note: vscode.DiagnosticSeverity;
}
export interface ILintingSettings {
enabled: boolean;
enabledWithoutWorkspace: boolean;
Expand All @@ -69,6 +82,9 @@ export interface ILintingSettings {
lintOnSave: boolean;
maxNumberOfProblems: number;
pylintCategorySeverity: IPylintCategorySeverity;
pep8CategorySeverity: IPep8CategorySeverity;
flake8CategorySeverity: Flake8CategorySeverity;
mypyCategorySeverity: IMypyCategorySeverity;
prospectorPath: string;
pylintPath: string;
pep8Path: string;
Expand Down Expand Up @@ -183,6 +199,19 @@ export class PythonSettings extends EventEmitter implements IPythonSettings {
fatal: vscode.DiagnosticSeverity.Error,
refactor: vscode.DiagnosticSeverity.Hint,
warning: vscode.DiagnosticSeverity.Warning
},
pep8CategorySeverity: {
E: vscode.DiagnosticSeverity.Error,
W: vscode.DiagnosticSeverity.Warning
},
flake8CategorySeverity: {
F: vscode.DiagnosticSeverity.Error,
E: vscode.DiagnosticSeverity.Error,
W: vscode.DiagnosticSeverity.Warning
},
mypyCategorySeverity: {
error: vscode.DiagnosticSeverity.Error,
note: vscode.DiagnosticSeverity.Hint
}
};
this.linting.pylintPath = getAbsolutePath(systemVariables.resolveAny(this.linting.pylintPath), workspaceRoot);
Expand Down
23 changes: 23 additions & 0 deletions src/client/linters/baseLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ export abstract class BaseLinter {
public abstract isEnabled(): Boolean;
public abstract runLinter(document: vscode.TextDocument, cancellation: vscode.CancellationToken): Promise<ILintMessage[]>;

protected parseMessagesSeverity(error: string, categorySeverity: any): LintMessageSeverity {
if (categorySeverity[error]) {
let severityName = categorySeverity[error];
switch (severityName) {
case 'Error':
return LintMessageSeverity.Error;
case 'Hint':
return LintMessageSeverity.Hint;
case 'Information':
return LintMessageSeverity.Information;
case 'Warning':
return LintMessageSeverity.Warning;
default: {
if (LintMessageSeverity[severityName]) {
return <LintMessageSeverity><any>LintMessageSeverity[severityName];
}
}
}
}

return LintMessageSeverity.Information;
}

protected run(command: string, args: string[], document: vscode.TextDocument, cwd: string, cancellation: vscode.CancellationToken, regEx: string = REGEX): Promise<ILintMessage[]> {
let outputChannel = this.outputChannel;

Expand Down
18 changes: 2 additions & 16 deletions src/client/linters/flake8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ export class Linter extends baseLinter.BaseLinter {
super('flake8', Product.flake8, outputChannel, workspaceRootPath);
}

private parseMessagesCodeSeverity(error: string): baseLinter.LintMessageSeverity {

let category_letter = error[0];
switch (category_letter) {
case 'F':
case 'E':
return baseLinter.LintMessageSeverity.Error;
case 'W':
return baseLinter.LintMessageSeverity.Warning;
default:
return baseLinter.LintMessageSeverity.Information;
}
}

public isEnabled(): Boolean {
return this.pythonSettings.linting.flake8Enabled;
}
Expand All @@ -35,9 +21,9 @@ export class Linter extends baseLinter.BaseLinter {
let flake8Path = this.pythonSettings.linting.flake8Path;
let flake8Args = Array.isArray(this.pythonSettings.linting.flake8Args) ? this.pythonSettings.linting.flake8Args : [];
return new Promise<baseLinter.ILintMessage[]>((resolve, reject) => {
this.run(flake8Path, flake8Args.concat(['--format=%(row)d,%(col)d,%(code)s,%(code)s:%(text)s', document.uri.fsPath]), document, this.workspaceRootPath, cancellation).then(messages => {
this.run(flake8Path, flake8Args.concat(['--format=%(row)d,%(col)d,%(code).1s,%(code)s:%(text)s', document.uri.fsPath]), document, this.workspaceRootPath, cancellation).then(messages => {
messages.forEach(msg => {
msg.severity = this.parseMessagesCodeSeverity(msg.type);
msg.severity = this.parseMessagesSeverity(msg.type, this.pythonSettings.linting.flake8CategorySeverity);
});

resolve(messages);
Expand Down
15 changes: 1 addition & 14 deletions src/client/linters/mypy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ export class Linter extends baseLinter.BaseLinter {
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
super('mypy', Product.mypy, outputChannel, workspaceRootPath);
}
private parseMessagesSeverity(category: string): baseLinter.LintMessageSeverity {
switch (category) {
case 'error': {
return baseLinter.LintMessageSeverity.Error;
}
case 'note': {
return baseLinter.LintMessageSeverity.Hint;
}
default: {
return baseLinter.LintMessageSeverity.Information;
}
}
}

public isEnabled(): Boolean {
return this.pythonSettings.linting.mypyEnabled;
Expand All @@ -38,7 +25,7 @@ export class Linter extends baseLinter.BaseLinter {
return new Promise<baseLinter.ILintMessage[]>((resolve, reject) => {
this.run(mypyPath, mypyArgs.concat([document.uri.fsPath]), document, this.workspaceRootPath, cancellation, REGEX).then(messages => {
messages.forEach(msg => {
msg.severity = this.parseMessagesSeverity(msg.type);
msg.severity = this.parseMessagesSeverity(msg.type, this.pythonSettings.linting.mypyCategorySeverity);
msg.code = msg.type;
});

Expand Down
17 changes: 2 additions & 15 deletions src/client/linters/pep8Linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@ export class Linter extends baseLinter.BaseLinter {
super('pep8', Product.pep8, outputChannel, workspaceRootPath);
}

private parseMessagesCodeSeverity(error: string): baseLinter.LintMessageSeverity {

let category_letter = error[0];
switch (category_letter) {
case 'E':
return baseLinter.LintMessageSeverity.Error;
case 'W':
return baseLinter.LintMessageSeverity.Warning;
default:
return baseLinter.LintMessageSeverity.Information;
}
}

public isEnabled(): Boolean {
return this.pythonSettings.linting.pep8Enabled;
}
Expand All @@ -34,9 +21,9 @@ export class Linter extends baseLinter.BaseLinter {
let pep8Path = this.pythonSettings.linting.pep8Path;
let pep8Args = Array.isArray(this.pythonSettings.linting.pep8Args) ? this.pythonSettings.linting.pep8Args : [];
return new Promise<baseLinter.ILintMessage[]>(resolve => {
this.run(pep8Path, pep8Args.concat(['--format=%(row)d,%(col)d,%(code)s,%(code)s:%(text)s', document.uri.fsPath]), document, this.workspaceRootPath, cancellation).then(messages => {
this.run(pep8Path, pep8Args.concat(['--format=%(row)d,%(col)d,%(code).1s,%(code)s:%(text)s', document.uri.fsPath]), document, this.workspaceRootPath, cancellation).then(messages => {
messages.forEach(msg => {
msg.severity = this.parseMessagesCodeSeverity(msg.type);
msg.severity = this.parseMessagesSeverity(msg.type, this.pythonSettings.linting.pep8CategorySeverity);
});

resolve(messages);
Expand Down
25 changes: 1 addition & 24 deletions src/client/linters/pylint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,6 @@ export class Linter extends baseLinter.BaseLinter {
super('pylint', Product.pylint, outputChannel, workspaceRootPath);
}

private parseMessagesSeverity(category: string): baseLinter.LintMessageSeverity {
if (this.pythonSettings.linting.pylintCategorySeverity[category]) {
let severityName = this.pythonSettings.linting.pylintCategorySeverity[category];
switch (severityName) {
case 'Error':
return baseLinter.LintMessageSeverity.Error;
case 'Hint':
return baseLinter.LintMessageSeverity.Hint;
case 'Information':
return baseLinter.LintMessageSeverity.Information;
case 'Warning':
return baseLinter.LintMessageSeverity.Warning;
default: {
if (baseLinter.LintMessageSeverity[severityName]) {
return <baseLinter.LintMessageSeverity><any>baseLinter.LintMessageSeverity[severityName];
}
}
}
}

return baseLinter.LintMessageSeverity.Information;
}

public isEnabled(): Boolean {
return this.pythonSettings.linting.pylintEnabled;
}
Expand All @@ -46,7 +23,7 @@ export class Linter extends baseLinter.BaseLinter {
return new Promise<baseLinter.ILintMessage[]>((resolve, reject) => {
this.run(pylintPath, pylintArgs.concat(['--msg-template=\'{line},{column},{category},{msg_id}:{msg}\'', '--reports=n', '--output-format=text', document.uri.fsPath]), document, this.workspaceRootPath, cancellation).then(messages => {
messages.forEach(msg => {
msg.severity = this.parseMessagesSeverity(msg.type);
msg.severity = this.parseMessagesSeverity(msg.type, this.pythonSettings.linting.pylintCategorySeverity);
});

resolve(messages);
Expand Down
13 changes: 3 additions & 10 deletions src/test/pythonFiles/sorting/withconfig/before.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
from third_party import lib0
from third_party import lib1
from third_party import lib2
from third_party import lib3
from third_party import lib4
from third_party import lib5
from third_party import lib6
from third_party import lib7
from third_party import lib8
from third_party import lib9
from third_party import (lib1, lib2, lib3,
lib4, lib5, lib6,
lib7, lib8, lib9)