This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlint.js
135 lines (111 loc) · 4.74 KB
/
lint.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true*/
/*global define, brackets, $, window, CSSLint, Mustache */
define(function (require, exports, module) {
'use strict';
var CodeInspection = brackets.getModule("language/CodeInspection");
var NodeDomain = brackets.getModule("utils/NodeDomain");
var ExtensionUtils = brackets.getModule("utils/ExtensionUtils");
var PreferencesManager = brackets.getModule("preferences/PreferencesManager");
var ProjectManager = brackets.getModule("project/ProjectManager");
var rflintDomain = new NodeDomain("rflint", ExtensionUtils.getModulePath(module, "node/rflint-domain"));
var response = null;
var result = {errors: []};
function init() {
rflintDomain.on("stdout", function (e, data) {
var lines = data.data.split("\n");
var regex = /^(W|E):\s*(\d+),\s*(\d+):\s*(.*)/;
var type;
var match;
var error;
lines.forEach(function (line) {
match = regex.exec(line);
if (match) {
// N.B. brackets starts counting lines at zero, rflint
// starts at 1
var pos = {
line: parseInt(match[2], 10) - 1,
ch: parseInt(match[3], 10)
};
type = CodeInspection.Type.WARNING;
if (match[1] === "E") {
type = CodeInspection.Type.ERROR;
}
error = {
pos: pos,
message: match[4],
type: type
};
if (_isUniqueError(result.errors, error)) {
result.errors.push(error);
}
} else {
// some other sort of output we didn't expect.
if (line.length > 0) {
var error = {
pos: {},
message: "unexpected output from rflint: " + line,
type: CodeInspection.Type.META
};
result.errors.push(error);
}
}
});
});
rflintDomain.on("stderr", function (e, data) {
var error = {
pos: {},
message: data.data,
type: CodeInspection.Type.ERROR
};
if (_isUniqueError(result.errors, error)) {
result.errors.push(error);
}
});
rflintDomain.on("error", function (e, data) {
response.resolve(result);
});
rflintDomain.on("exit", function (e, data) {
response.resolve(result);
});
}
function _isUniqueError(errors, new_error) {
// return true if the given error is NOT in the given errors array
var i;
var this_error;
for (i = 0; i < errors.length; i++) {
this_error = result.errors[i];
if (this_error.pos.line === new_error.pos.line &&
this_error.pos.ch === new_error.pos.ch &&
this_error.message === new_error.message &&
this_error.type === new_error.type) {
return false;
}
}
return true;
}
function handleLintRequest(text, fullPath) {
var _prefs = PreferencesManager.getExtensionPrefs("robotframework");
var rflint_command = _prefs.get("rflint-command").trim();
// if the file is empty, don't do anything. (oddly, brackets seems
// to run lint when you first create a brand new file)
if (rflint_command.length === 0 || text.trim().length === 0) {
// is this the proper way to cancel the request?
response = new $.Deferred();
response.resolve();
return response.promise();
}
var cwd = ProjectManager.getProjectRoot().fullPath;
// N.B. the command must be a string; it will be parsed into arguments
// by the domain. No matter what other arguments the user may have
// specified, we need to insist on --no-filenames and --format.
var command = rflint_command +
" --no-filenames" +
" --format '{severity}: {linenumber}, {char}: {message} ({rulename})'";
result = {errors: []};
response = new $.Deferred();
rflintDomain.exec("start", cwd, command, fullPath);
return response.promise();
}
exports.handleLintRequest = handleLintRequest;
exports.init = init;
});