1+ 'use strict' ;
2+
3+ import * as path from 'path' ;
4+ import * as baseLinter from './baseLinter' ;
5+ import { OutputChannel , workspace } from 'vscode' ;
6+
7+ const REGEX = '(?<file>.py):(?<line>\\d+): (?<type>\\w+): (?<message>.*)\\r?(\\n|$)' ;
8+
9+ export class Linter extends baseLinter . BaseLinter {
10+ constructor ( outputChannel : OutputChannel , workspaceRootPath : string ) {
11+ super ( 'mypy' , outputChannel , workspaceRootPath ) ;
12+ }
13+ private parseMessagesSeverity ( category : string ) : baseLinter . LintMessageSeverity {
14+ switch ( category ) {
15+ case 'error' : {
16+ return baseLinter . LintMessageSeverity . Error ;
17+ }
18+ case 'note' : {
19+ return baseLinter . LintMessageSeverity . Hint ;
20+ }
21+ default : {
22+ return baseLinter . LintMessageSeverity . Information ;
23+ }
24+ }
25+ }
26+
27+ public isEnabled ( ) : Boolean {
28+ return this . pythonSettings . linting . mypyEnabled ;
29+ }
30+ public runLinter ( filePath : string , txtDocumentLines : string [ ] ) : Promise < baseLinter . ILintMessage [ ] > {
31+ if ( ! this . pythonSettings . linting . mypyEnabled ) {
32+ return Promise . resolve ( [ ] ) ;
33+ }
34+
35+ let mypyPath = this . pythonSettings . linting . mypyPath ;
36+ let mypyArgs = Array . isArray ( this . pythonSettings . linting . mypyArgs ) ? this . pythonSettings . linting . mypyArgs : [ ] ;
37+ return new Promise < baseLinter . ILintMessage [ ] > ( ( resolve , reject ) => {
38+ this . run ( mypyPath , mypyArgs . concat ( [ filePath ] ) , filePath , txtDocumentLines , this . workspaceRootPath , REGEX ) . then ( messages => {
39+ messages . forEach ( msg => {
40+ msg . severity = this . parseMessagesSeverity ( msg . type ) ;
41+ msg . code = msg . type ;
42+ } ) ;
43+
44+ resolve ( messages ) ;
45+ } , reject ) ;
46+ } ) ;
47+ }
48+ }
0 commit comments