Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Add go-metalinter support #294

Merged
merged 2 commits into from
Jul 23, 2016
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,24 @@
"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",
"items": {
"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",
Expand Down Expand Up @@ -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",
Expand Down
18 changes: 12 additions & 6 deletions src/goCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.'
));
}

Expand Down
31 changes: 31 additions & 0 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});