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