Mercurial
comparison .cms/lib/codemirror/addon/lint/javascript-lint.js @ 0:78edf6b517a0 draft
24.10
author | Coffee CMS <info@coffee-cms.ru> |
---|---|
date | Fri, 11 Oct 2024 22:40:23 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:78edf6b517a0 |
---|---|
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others | |
2 // Distributed under an MIT license: https://codemirror.net/5/LICENSE | |
3 | |
4 // Depends on jshint.js from https://github.com/jshint/jshint | |
5 | |
6 (function(mod) { | |
7 if (typeof exports == "object" && typeof module == "object") // CommonJS | |
8 mod(require("../../lib/codemirror")); | |
9 else if (typeof define == "function" && define.amd) // AMD | |
10 define(["../../lib/codemirror"], mod); | |
11 else // Plain browser env | |
12 mod(CodeMirror); | |
13 })(function(CodeMirror) { | |
14 "use strict"; | |
15 // declare global: JSHINT | |
16 | |
17 function validator(text, options) { | |
18 if (!window.JSHINT) { | |
19 if (window.console) { | |
20 window.console.error("Error: window.JSHINT not defined, CodeMirror JavaScript linting cannot run."); | |
21 } | |
22 return []; | |
23 } | |
24 if (!options.indent) // JSHint error.character actually is a column index, this fixes underlining on lines using tabs for indentation | |
25 options.indent = 1; // JSHint default value is 4 | |
26 JSHINT(text, options, options.globals); | |
27 var errors = JSHINT.data().errors, result = []; | |
28 if (errors) parseErrors(errors, result); | |
29 return result; | |
30 } | |
31 | |
32 CodeMirror.registerHelper("lint", "javascript", validator); | |
33 | |
34 function parseErrors(errors, output) { | |
35 for ( var i = 0; i < errors.length; i++) { | |
36 var error = errors[i]; | |
37 if (error) { | |
38 if (error.line <= 0) { | |
39 if (window.console) { | |
40 window.console.warn("Cannot display JSHint error (invalid line " + error.line + ")", error); | |
41 } | |
42 continue; | |
43 } | |
44 | |
45 var start = error.character - 1, end = start + 1; | |
46 if (error.evidence) { | |
47 var index = error.evidence.substring(start).search(/.\b/); | |
48 if (index > -1) { | |
49 end += index; | |
50 } | |
51 } | |
52 | |
53 // Convert to format expected by validation service | |
54 var hint = { | |
55 message: error.reason, | |
56 severity: error.code ? (error.code.startsWith('W') ? "warning" : "error") : "error", | |
57 from: CodeMirror.Pos(error.line - 1, start), | |
58 to: CodeMirror.Pos(error.line - 1, end) | |
59 }; | |
60 | |
61 output.push(hint); | |
62 } | |
63 } | |
64 } | |
65 }); |