From 467ab49cdd16f6839f288789613c36f948863687 Mon Sep 17 00:00:00 2001 From: Sajjad Hasehmian Date: Fri, 15 Apr 2016 17:58:18 +0430 Subject: [PATCH 1/2] Add go-metalinter support --- package.json | 15 ++++++++++++--- src/goCheck.ts | 18 ++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index bc708362b..eede32cc7 100644 --- a/package.json +++ b/package.json @@ -219,7 +219,16 @@ "go.lintOnSave": { "type": "boolean", "default": true, - "description": "Run 'golint' on save." + "description": "Run Lint tool on save." + }, + "go.lintTool": { + "type": "string", + "default": "golint", + "description": "Specifies Lint tool name.", + "enum": [ + "golint", + "gometalinter" + ] }, "go.lintFlags": { "type": "array", @@ -227,7 +236,7 @@ "type": "string" }, "default": [], - "description": "Flags to pass to `golint` (e.g. ['-min_confidenc=.8'])" + "description": "Flags to pass to Lint tool (e.g. ['-min_confidenc=.8'])" }, "go.vetOnSave": { "type": "boolean", @@ -276,7 +285,7 @@ "go.coverOnSave": { "type": "boolean", "default": false, - "description": "Run 'go test -coverprofile' on save" + "description": "Run 'go test -coverprofile' on save" }, "go.testTimeout": { "type": "string", diff --git a/src/goCheck.ts b/src/goCheck.ts index 2b4962bc0..9e7570c8c 100644 --- a/src/goCheck.ts +++ b/src/goCheck.ts @@ -39,7 +39,7 @@ function runTool(cmd: string, args: string[], cwd: string, severity: string, use ret[ret.length - 1].msg += '\n' + lines[i]; continue; } - let match = /^([^:]*: )?((.:)?[^:]*):(\d+)(:(\d+))?: (.*)$/.exec(lines[i]); + let match = /^([^:]*: )?((.:)?[^:]*):(\d+)(:(\d+))?:(?:\w+:)? (.*)$/.exec(lines[i]); if (!match) continue; let [_, __, file, ___, lineStr, ____, charStr, msg] = match; let line = +lineStr; @@ -79,15 +79,21 @@ export function check(filename: string, goConfig: vscode.WorkspaceConfiguration) )); } if (!!goConfig['lintOnSave']) { - let golint = getBinPath('golint'); + let lintTool = getBinPath(goConfig['lintTool'] || 'golint'); let lintFlags = goConfig['lintFlags'] || []; + let args = [...lintFlags]; + + if (lintTool === 'golint') { + args.push(filename); + } + runningToolsPromises.push(runTool( - golint, - [...lintFlags, filename], + lintTool, + args, cwd, 'warning', - false, - 'The "golint" command is not available. Use "go get -u github.com/golang/lint/golint" to install.' + lintTool === 'golint', + 'The "' + lintTool + '" command is not available. Make sure it is installed.' )); } From 32c4e3e464f9024564f9435b294ea0edae629807 Mon Sep 17 00:00:00 2001 From: Sjjad Hashemian Date: Mon, 9 May 2016 19:16:40 +0430 Subject: [PATCH 2/2] Add gometalinter test case --- .travis.yml | 2 ++ test/go.test.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/.travis.yml b/.travis.yml index 87b5d1892..e916f3a51 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,6 +26,8 @@ install: - go get -u -v github.com/lukehoban/go-outline - go get -u -v sourcegraph.com/sqs/goreturns - go get -u -v golang.org/x/tools/cmd/gorename + - go get -u -v github.com/alecthomas/gometalinter + - gometalinter --install --update script: - npm run lint diff --git a/test/go.test.ts b/test/go.test.ts index 928edacf0..c05c97a98 100644 --- a/test/go.test.ts +++ b/test/go.test.ts @@ -116,4 +116,35 @@ suite('Go Extension Tests', () => { assert.equal(sortedDiagnostics.length, expected.length, `too many errors ${JSON.stringify(sortedDiagnostics)}`); }).then(() => done(), done); }); + + test('Gometalinter error checking', (done) => { + let config = vscode.workspace.getConfiguration('go'); + config['lintTool'] = 'gometalinter'; + let expected = [ + { line: 7, severity: 'warning', msg: 'Print2 is unused (deadcode)' }, + { line: 7, severity: 'warning', msg: 'exported function Print2 should have comment or be unexported (golint)' }, + { line: 10, severity: 'warning', msg: 'main2 is unused (deadcode)' }, + { line: 11, severity: 'warning', msg: 'undeclared name: prin (aligncheck)' }, + { line: 11, severity: 'warning', msg: 'undeclared name: prin (gotype)' }, + { line: 11, severity: 'warning', msg: 'undeclared name: prin (interfacer)' }, + { line: 11, severity: 'warning', msg: 'undeclared name: prin (unconvert)' }, + { line: 11, severity: 'error', msg: 'undefined: prin' }, + { line: 11, severity: 'warning', msg: 'unused struct field undeclared name: prin (structcheck)' }, + ]; + check(path.join(fixturePath, 'errors.go'), config).then(diagnostics => { + let sortedDiagnostics = diagnostics.sort((a, b) => { + if ( a.msg < b.msg ) + return -1; + if ( a.msg > b.msg ) + return 1; + return 0; + }); + for (let i in expected) { + assert.equal(sortedDiagnostics[i].line, expected[i].line); + assert.equal(sortedDiagnostics[i].severity, expected[i].severity); + assert.equal(sortedDiagnostics[i].msg, expected[i].msg); + } + assert.equal(sortedDiagnostics.length, expected.length, `too many errors ${JSON.stringify(sortedDiagnostics)}`); + }).then(() => done(), done); + }); });