You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.1 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/LICENSE
  3. // Depends on jshint.js from https://github.com/jshint/jshint
  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. function validator(text, options) {
  15. if (!window.JSHINT) {
  16. if (window.console) {
  17. window.console.error("Error: window.JSHINT not defined, CodeMirror JavaScript linting cannot run.");
  18. }
  19. return [];
  20. }
  21. if (!options.indent) // JSHint error.character actually is a column index, this fixes underlining on lines using tabs for indentation
  22. options.indent = 1; // JSHint default value is 4
  23. JSHINT(text, options, options.globals);
  24. var errors = JSHINT.data().errors, result = [];
  25. if (errors) parseErrors(errors, result);
  26. return result;
  27. }
  28. CodeMirror.registerHelper("lint", "javascript", validator);
  29. function parseErrors(errors, output) {
  30. for ( var i = 0; i < errors.length; i++) {
  31. var error = errors[i];
  32. if (error) {
  33. if (error.line <= 0) {
  34. if (window.console) {
  35. window.console.warn("Cannot display JSHint error (invalid line " + error.line + ")", error);
  36. }
  37. continue;
  38. }
  39. var start = error.character - 1, end = start + 1;
  40. if (error.evidence) {
  41. var index = error.evidence.substring(start).search(/.\b/);
  42. if (index > -1) {
  43. end += index;
  44. }
  45. }
  46. // Convert to format expected by validation service
  47. var hint = {
  48. message: error.reason,
  49. severity: error.code ? (error.code.startsWith('W') ? "warning" : "error") : "error",
  50. from: CodeMirror.Pos(error.line - 1, start),
  51. to: CodeMirror.Pos(error.line - 1, end)
  52. };
  53. output.push(hint);
  54. }
  55. }
  56. }
  57. });