3 var bogus = [ "Dangerous comment" ];
5 var warnings = [ [ "Expected '{'",
6 "Statement body should be inside '{ }' braces." ] ];
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" ];
12 function validator(text, options) {
13 JSHINT(text, options);
14 var errors = JSHINT.data().errors, result = [];
15 if (errors) parseErrors(errors, result);
19 CodeMirror.registerHelper("lint", "javascript", validator);
20 CodeMirror.javascriptValidator = CodeMirror.lint.javascript; // deprecated
22 function cleanup(error) {
23 // All problems are warnings by default
24 fixWith(error, warnings, "warning", true);
25 fixWith(error, errors, "error");
27 return isBogus(error) ? null : error;
30 function fixWith(error, fixes, severity, force) {
31 var description, fix, find, replace, found;
33 description = error.description;
35 for ( var i = 0; i < fixes.length; i++) {
37 find = (typeof fix === "string" ? fix : fix[0]);
38 replace = (typeof fix === "string" ? null : fix[1]);
39 found = description.indexOf(find) !== -1;
42 error.severity = severity;
44 if (found && replace) {
45 error.description = replace;
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) {
60 function parseErrors(errors, output) {
61 for ( var i = 0; i < errors.length; i++) {
62 var error = errors[i];
64 var linetabpositions, index;
66 linetabpositions = [];
68 // This next block is to fix a problem in jshint. Jshint
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.
76 // Tab positions are computed once per line and cached
77 var tabpositions = linetabpositions[error.line];
79 var evidence = error.evidence;
81 // ugggh phantomjs does not like this
82 // forEachChar(evidence, function(item, index) {
83 Array.prototype.forEach.call(evidence, function(item,
86 // First col is 1 (not 0) to match error
88 tabpositions.push(index + 1);
91 linetabpositions[error.line] = tabpositions;
93 if (tabpositions.length > 0) {
94 var pos = error.character;
95 tabpositions.forEach(function(tabposition) {
96 if (pos > tabposition) pos -= 1;
98 error.character = pos;
102 var start = error.character - 1, end = start + 1;
103 if (error.evidence) {
104 index = error.evidence.substring(start).search(/.\b/);
110 // Convert to format expected by validation service
111 error.description = error.reason;// + "(jshint)";
112 error.start = error.character;
114 error = cleanup(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)});