forked from koalaman/shellcheck.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shellcheck.js
301 lines (266 loc) · 7.03 KB
/
shellcheck.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* I don't actually know Javascript :( */
var lastChange = 0;
var lastCheck = 0;
var lastCode = "";
var lastErrors = [];
function shellcheckCode(code, callback) {
lastChange = lastCheck;
if (lastChange == 0 && code == "") {
return;
}
if (window.jQuery) {
$.post("shellcheck.php",
{ "script": code },
function (data) {
callback(data);
}, 'json');
} else {
// JQuery is not loaded. Try again soon.
setTimeout(function() {
shellcheckCode(code, callback);
}, 1000);
}
}
function getLink(code) {
return $("<a />")
.attr("href", "https://github.com/koalaman/shellcheck/wiki/SC" + code)
.attr("target", "_blank")
.attr("class", "wikilink")
.text("SC" + code);
}
function getLineLink(line, column) {
return $("<a />")
.attr("href", "javascript:setPosition(" + line + ", " + column + ")")
.attr("class", "linelink")
.text("Line " + line + ":");
}
function formatError(err) {
var nodes = [];
var header="";
var i=1;
if (err.column >= 60) {
header += ">>";
i += header.length;
}
for(; i<err.column; i++) {
header += " "
}
header += "^-- ";
nodes.push($("<span />").html(header));
if (err.code > 0) {
nodes.push(getLink(err.code));
nodes.push(document.createTextNode(" (" + err.level + "): "));
}
nodes.push($("<span />").text(err.message));
return $("<span />")
.addClass(err.level)
.addClass("pointer")
.append(nodes);
}
// Get only unique elements from a list.
function nubList(list) {
var set = {};
for(var item of list) {
set[item] = true;
}
var result = [];
for(var key in set) {
result.push(key);
}
result.sort();
return result;
}
function formatFix(fixer, errors, applicableIndices) {
var result = $("<span />");
var codes = [];
for(var index of applicableIndices) {
codes.push(errors[index].code);
}
codes = nubList(codes);
result.append($("<span />")
.attr("class", "didyoumean")
.append($("<br />"))
.append($("<span />").text("Did you mean: ")));
var nodes = [];
nodes.push($("<span />").text("("));
nodes.push($("<a />")
.attr("href", "javascript:applyFixIndex(" + JSON.stringify(applicableIndices) + ")")
.attr("class", "linelink")
.text("apply this"));
for (var code of codes) {
nodes.push($("<span />").text(", apply "));
nodes.push($("<a />")
.attr("href", "javascript:applyFixCode(" + code + ")")
.attr("class", "linelink")
.text("all SC" + code));
}
nodes.push($("<span />").text(")"));
nodes.push($("<br />"));
result.append($("<span />")
.attr("class", "applyline")
.append(nodes));
nodes = []
for(var line of fixer.getSnippet().split("\n")) {
nodes.push($("<span />")
.addClass("sourceline")
.append(document.createTextNode(formatLine(line))));
}
nodes.push($("<br />"));
result.append(nodes);
return result;
}
function applyAllFixes() {
if(!applyFixes(lastErrors)) {
alert("Sorry! There are no relevant autofixes available at this time.");
}
}
function applyFixIndex(indices) {
var fixes = [];
for (var i of indices) {
fixes.push(lastErrors[i]);
}
applyFixes(fixes);
}
function applyFixCode(code) {
var fixes = [];
for (var err of lastErrors) {
if (err.code == code) {
fixes.push(err);
}
}
applyFixes(fixes);
}
function applyFixes(fixes) {
if (lastChange != lastCheck) {
return;
}
var zoomTo = undefined;
var fixer = new AutoFixer(lastCode);
for (var err of fixes) {
if (!zoomTo) {
zoomTo = err.line;
}
fixer.applyFix(err);
}
editor.setValue(fixer.getResult());
if (zoomTo) {
setPosition(zoomTo, 1);
}
return fixer.hasModifications();
}
function sortKey() {
for(var i in arguments) {
if (arguments[i] != 0)
return arguments[i];
}
return 0;
}
function formatLine(line) {
if (!line) {
line = "";
}
line = line.replace(/\t/g, " ");
line = line.replace(/^ /, "\u00A0");
line = line.replace(/ /g, " \u00A0");
return line;
}
function createTerminal(code, errors) {
var node = $("<div />");
errors.sort(function(a,b) {
return sortKey(a.line - b.line, a.column - b.column)
});
var lines = code.split("\n");
node.append(document.createTextNode("$ shellcheck myscript")).append("<br />");
if(errors.length == 0) {
node.append(document.createTextNode("No issues detected!")).append("<br />");
} else {
var fixer = new AutoFixer(code);
var currentFixes = [];
var last = -1;
for (var i=0; i < errors.length; i++) {
if (errors[i].line != last) {
// After a set of errors, show autofix suggestions
if (fixer.hasModifications()) {
node.append(formatFix(fixer, errors, currentFixes));
}
fixer.reset();
currentFixes = [];
// For the first message of the line, print the line number and line
last = errors[i].line;
node.append(" <br />");
node.append(getLineLink(errors[i].line, errors[i].column));
node.append("<br />");
var line = formatLine(lines[errors[i].line - 1]);
var lineElem = $("<span />")
.addClass("sourceline")
.append(document.createTextNode(line));
node.append(lineElem).append("<br />");
}
// Then 1+ errors
node.append(formatError(errors[i])).append("<br />");
if(errors[i].fix) {
fixer.applyFix(errors[i]);
currentFixes.push(i);
}
}
if (fixer.hasModifications()) {
node.append(formatFix(fixer, errors, currentFixes));
}
}
node
.append("<br />")
.append(document.createTextNode("$ ")).append("<br />");
return node;
}
function hasAutoFix(errors) {
for(var err of errors) {
if (err.fix && err.fix.replacements && err.fix.replacements.length > 0) {
return true;
}
}
return false;
}
function setTerminal(code, errors) {
$("#terminal").html("").append(createTerminal(code, errors));
if (hasAutoFix(errors)) {
$("#autofix").removeClass("disabledLink");
} else {
$("#autofix").addClass("disabledLink");
}
if (lastChange == lastCheck) {
lastCode = code;
lastErrors = errors;
$("#processingindicator").text("");
}
}
function setPosition(line, column) {
var min = 40;
editor.gotoLine(line, column - 1);
var position = editor.renderer.textToScreenCoordinates(line, column - 1).pageY;
if ( position < min) {
document.body.scrollTop += position - min;
}
editor.focus();
editor.clearSelection();
}
function maximize() {
var selector = "#editorwindow";
$(selector).css( { "max-width": "100%" });
$(selector).height($(selector).height() * 1.5);
$("#terminalwindow").css( { "max-width": "100%" });
editor.resize();
}
function minimize() {
var selector = "#editorwindow";
if ($(selector).height() > 50) {
$(selector).height($(selector).height() * 0.5);
}
editor.resize();
}
function editorChangeHandler(obj) {
lastChange++;
$("#processingindicator").text(" (updating)");
}
function reportBug() {
window.open(makeReportUrl(lastCode, lastErrors));
}