@@ -16,65 +16,61 @@ export default class Linter {
1616 }
1717
1818 public async lint ( ) : Promise < LinterError [ ] > {
19- const errors = await this . runProtoLint ( ) ;
20- if ( ! errors ) {
19+ const result = await this . runProtoLint ( ) ;
20+ if ( ! result ) {
2121 return [ ] ;
2222 }
2323
24- // protolint returns "not found config file by searching" when it is
25- // executed and a configuration file cannot be found.
26- if ( errors . includes ( "not found config" ) ) {
27- vscode . window . showErrorMessage ( errors ) ;
24+ const lintingErrors : LinterError [ ] = this . parseErrors ( result ) ;
25+
26+ // When errors exist, but no linting errors were returned show the error window
27+ // in VSCode as it is most likely an issue with the binary itself such as not being
28+ // able to find a configuration or a file to lint.
29+ if ( lintingErrors . length === 0 ) {
30+ vscode . window . showErrorMessage ( "protolint: " + result ) ;
2831 return [ ] ;
2932 }
3033
31- const lintingErrors : LinterError [ ] = this . parseErrors ( errors ) ;
32- return lintingErrors ;
33- }
3434
35- private parseErrors ( errorStr : string ) : LinterError [ ] {
36- let errors = errorStr . split ( '\n' ) || [ ] ;
37-
38- var result = errors . reduce ( ( errors : LinterError [ ] , currentError : string ) => {
39- const parsedError = parseProtoError ( currentError ) ;
40- if ( ! parsedError . reason ) {
41- return errors ;
42- }
43-
44- const linterError : LinterError = this . createLinterError ( parsedError ) ;
45- return errors . concat ( linterError ) ;
46- } , [ ] ) ;
47-
48- return result ;
35+ return lintingErrors ;
4936 }
5037
5138 private async runProtoLint ( ) : Promise < string > {
5239 if ( ! vscode . workspace . workspaceFolders ) {
5340 return "" ;
5441 }
5542
56- const currentFile = this . codeDocument . uri . fsPath ;
5743 let workspaceFolder : vscode . WorkspaceFolder = vscode . workspace . getWorkspaceFolder ( this . codeDocument . uri ) || vscode . workspace . workspaceFolders [ 0 ] ;
58- const cmd = `protolint lint -config_dir_path="${ workspaceFolder . uri . fsPath } " "${ currentFile } "` ;
59-
60- const exec = util . promisify ( cp . exec ) ;
44+ const cmd = `protolint lint -config_dir_path="${ workspaceFolder . uri . fsPath } " "${ this . codeDocument . uri . fsPath } "` ;
6145
6246 let lintResults : string = "" ;
47+
48+ // Execute the protolint binary and store the output from standard error.
49+ // The output could either be an error from using the binary improperly, such as unable to find
50+ // a configuration, or linting errors.
51+ const exec = util . promisify ( cp . exec ) ;
6352 await exec ( cmd ) . catch ( ( error : any ) => lintResults = error . stderr ) ;
6453
6554 return lintResults ;
6655 }
6756
68- private createLinterError ( error : ProtoError ) : LinterError {
69- const linterError : LinterError = {
70- proto : error ,
71- range : this . getErrorRange ( error )
72- } ;
57+ private parseErrors ( errorStr : string ) : LinterError [ ] {
58+ let errors = errorStr . split ( '\n' ) || [ ] ;
7359
74- return linterError ;
75- }
60+ var result = errors . reduce ( ( errors : LinterError [ ] , currentError : string ) => {
61+ const parsedError = parseProtoError ( currentError ) ;
62+ if ( ! parsedError . reason ) {
63+ return errors ;
64+ }
65+
66+ const linterError : LinterError = {
67+ proto : parsedError ,
68+ range : this . codeDocument . lineAt ( parsedError . line - 1 ) . range
69+ } ;
7670
77- private getErrorRange ( error : ProtoError ) : vscode . Range {
78- return this . codeDocument . lineAt ( error . line - 1 ) . range ;
71+ return errors . concat ( linterError ) ;
72+ } , [ ] ) ;
73+
74+ return result ;
7975 }
8076}
0 commit comments