This repository was archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathgoCheck.ts
107 lines (99 loc) · 3.64 KB
/
goCheck.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
import vscode = require('vscode');
import cp = require('child_process');
import path = require('path');
import os = require('os');
import fs = require('fs');
import { getBinPath, getGoBin } from './goPath'
//TODO: Less hacky?
var go = getGoBin();
if (!go) {
vscode.window.showInformationMessage("No 'go' binary could be found on PATH or in GOROOT. Set location manual in 'go.goroot' setting.");
}
export interface ICheckResult {
file: string;
line: number;
msg: string;
severity: string;
}
export function check(filename: string, buildOnSave = true, lintOnSave = true, vetOnSave = true): Promise<ICheckResult[]> {
var gobuild = !buildOnSave ? Promise.resolve([]) : new Promise((resolve, reject) => {
var tmppath = path.normalize(path.join(os.tmpdir(), "go-code-check"))
var cwd = path.dirname(filename)
var args = ["build", "-o", tmppath, "."];
if (filename.match(/_test.go$/i)) {
args = ['test', '-copybinary', '-o', tmppath, '-c', '.']
}
cp.execFile(go, args, { cwd: cwd }, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.window.showInformationMessage("The 'go' compiler is not available. Install Go from http://golang.org/dl/.");
return resolve([]);
}
var lines = stderr.toString().split('\n');
var ret: ICheckResult[] = [];
for (var i = 1; i < lines.length; i++) {
var match = /([^:]*):(\d+)(:\d+)?: (.*)/.exec(lines[i]);
if (!match) continue;
var [_, file, lineStr, charStr, msg] = match;
var line = +lineStr;
ret.push({ file: path.resolve(cwd, file), line, msg, severity: "error" });
}
resolve(ret);
} catch (e) {
reject(e);
}
});
});
var golint = !lintOnSave ? Promise.resolve([]) : new Promise((resolve, reject) => {
var cwd = path.dirname(filename)
var golint = getBinPath("golint");
cp.execFile(golint, [filename], { cwd: cwd }, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.window.showInformationMessage("The 'golint' command is not available. Use 'go get -u github.com/golang/lint/golint' to install.");
return resolve([]);
}
var lines = stdout.toString().split('\n');
var ret: ICheckResult[] = [];
for (var i = 0; i < lines.length; i++) {
var match = /(.*):(\d+):(\d+): (.*)/.exec(lines[i]);
if (!match) continue;
var [_, file, lineStr, colStr, msg] = match;
var line = +lineStr;
ret.push({ file: path.resolve(cwd, file), line, msg, severity: "warning" });
}
resolve(ret);
} catch (e) {
reject(e);
}
});
});
var govet = !vetOnSave ? Promise.resolve([]) : new Promise((resolve, reject) => {
var cwd = path.dirname(filename)
cp.execFile(go, ["tool", "vet", filename], { cwd: cwd }, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.window.showInformationMessage("The 'go tool vet' compiler is not available. Install Go from http://golang.org/dl/.");
return resolve([]);
}
var lines = stdout.toString().split('\n');
var ret: ICheckResult[] = [];
for (var i = 0; i < lines.length; i++) {
var match = /(.*):(\d+): (.*)/.exec(lines[i]);
if (!match) continue;
var [_, file, lineStr, msg] = match;
var line = +lineStr;
ret.push({ file: path.resolve(cwd, file), line, msg, severity: "warning" });
}
resolve(ret);
} catch (e) {
reject(e);
}
});
});
return Promise.all([gobuild, golint, govet]).then(resultSets => [].concat.apply([], resultSets));
}