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