1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/LICENSE
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
11 })(function(CodeMirror) {
13 // declare global: JSHINT
15 function validator(text, options) {
18 window.console.error("Error: window.JSHINT not defined, CodeMirror JavaScript linting cannot run.");
22 if (!options.indent) // JSHint error.character actually is a column index, this fixes underlining on lines using tabs for indentation
23 options.indent = 1; // JSHint default value is 4
24 JSHINT(text, options, options.globals);
25 var errors = JSHINT.data().errors, result = [];
26 if (errors) parseErrors(errors, result);
30 CodeMirror.registerHelper("lint", "javascript", validator);
32 function parseErrors(errors, output) {
33 for ( var i = 0; i < errors.length; i++) {
34 var error = errors[i];
36 if (error.line <= 0) {
38 window.console.warn("Cannot display JSHint error (invalid line " + error.line + ")", error);
43 var start = error.character - 1, end = start + 1;
45 var index = error.evidence.substring(start).search(/.\b/);
51 // Convert to format expected by validation service
53 message: error.reason,
54 severity: error.code ? (error.code.startsWith('W') ? "warning" : "error") : "error",
55 from: CodeMirror.Pos(error.line - 1, start),
56 to: CodeMirror.Pos(error.line - 1, end)