Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
malwaredllc
GitHub Repository: malwaredllc/byob
Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/lint/javascript-lint.js
1293 views
1
(function() {
2
"use strict";
3
// declare global: JSHINT
4
5
var bogus = [ "Dangerous comment" ];
6
7
var warnings = [ [ "Expected '{'",
8
"Statement body should be inside '{ }' braces." ] ];
9
10
var errors = [ "Missing semicolon", "Extra comma", "Missing property name",
11
"Unmatched ", " and instead saw", " is not defined",
12
"Unclosed string", "Stopping, unable to continue" ];
13
14
function validator(text, options) {
15
JSHINT(text, options);
16
var errors = JSHINT.data().errors, result = [];
17
if (errors) parseErrors(errors, result);
18
return result;
19
}
20
21
CodeMirror.registerHelper("lint", "javascript", validator);
22
CodeMirror.javascriptValidator = CodeMirror.lint.javascript; // deprecated
23
24
function cleanup(error) {
25
// All problems are warnings by default
26
fixWith(error, warnings, "warning", true);
27
fixWith(error, errors, "error");
28
29
return isBogus(error) ? null : error;
30
}
31
32
function fixWith(error, fixes, severity, force) {
33
var description, fix, find, replace, found;
34
35
description = error.description;
36
37
for ( var i = 0; i < fixes.length; i++) {
38
fix = fixes[i];
39
find = (typeof fix === "string" ? fix : fix[0]);
40
replace = (typeof fix === "string" ? null : fix[1]);
41
found = description.indexOf(find) !== -1;
42
43
if (force || found) {
44
error.severity = severity;
45
}
46
if (found && replace) {
47
error.description = replace;
48
}
49
}
50
}
51
52
function isBogus(error) {
53
var description = error.description;
54
for ( var i = 0; i < bogus.length; i++) {
55
if (description.indexOf(bogus[i]) !== -1) {
56
return true;
57
}
58
}
59
return false;
60
}
61
62
function parseErrors(errors, output) {
63
for ( var i = 0; i < errors.length; i++) {
64
var error = errors[i];
65
if (error) {
66
var linetabpositions, index;
67
68
linetabpositions = [];
69
70
// This next block is to fix a problem in jshint. Jshint
71
// replaces
72
// all tabs with spaces then performs some checks. The error
73
// positions (character/space) are then reported incorrectly,
74
// not taking the replacement step into account. Here we look
75
// at the evidence line and try to adjust the character position
76
// to the correct value.
77
if (error.evidence) {
78
// Tab positions are computed once per line and cached
79
var tabpositions = linetabpositions[error.line];
80
if (!tabpositions) {
81
var evidence = error.evidence;
82
tabpositions = [];
83
// ugggh phantomjs does not like this
84
// forEachChar(evidence, function(item, index) {
85
Array.prototype.forEach.call(evidence, function(item,
86
index) {
87
if (item === '\t') {
88
// First col is 1 (not 0) to match error
89
// positions
90
tabpositions.push(index + 1);
91
}
92
});
93
linetabpositions[error.line] = tabpositions;
94
}
95
if (tabpositions.length > 0) {
96
var pos = error.character;
97
tabpositions.forEach(function(tabposition) {
98
if (pos > tabposition) pos -= 1;
99
});
100
error.character = pos;
101
}
102
}
103
104
var start = error.character - 1, end = start + 1;
105
if (error.evidence) {
106
index = error.evidence.substring(start).search(/.\b/);
107
if (index > -1) {
108
end += index;
109
}
110
}
111
112
// Convert to format expected by validation service
113
error.description = error.reason;// + "(jshint)";
114
error.start = error.character;
115
error.end = end;
116
error = cleanup(error);
117
118
if (error)
119
output.push({message: error.description,
120
severity: error.severity,
121
from: CodeMirror.Pos(error.line - 1, start),
122
to: CodeMirror.Pos(error.line - 1, end)});
123
}
124
}
125
}
126
})();
127
128