json-lint.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // Depends on jsonlint.js from https://github.com/zaach/jsonlint
  4. // declare global: jsonlint
  5. (function(mod) {
  6. if (typeof exports == "object" && typeof module == "object") // CommonJS
  7. mod(require("../../lib/codemirror"));
  8. else if (typeof define == "function" && define.amd) // AMD
  9. define(["../../lib/codemirror"], mod);
  10. else // Plain browser env
  11. mod(CodeMirror);
  12. })(function(CodeMirror) {
  13. "use strict";
  14. CodeMirror.registerHelper("lint", "json", function(text) {
  15. var found = [];
  16. if (!window.jsonlint) {
  17. if (window.console) {
  18. window.console.error("Error: window.jsonlint not defined, CodeMirror JSON linting cannot run.");
  19. }
  20. return found;
  21. }
  22. // for jsonlint's web dist jsonlint is exported as an object with a single property parser, of which parseError
  23. // is a subproperty
  24. var jsonlint = window.jsonlint.parser || window.jsonlint
  25. jsonlint.parseError = function(str, hash) {
  26. var loc = hash.loc;
  27. found.push({from: CodeMirror.Pos(loc.first_line - 1, loc.first_column),
  28. to: CodeMirror.Pos(loc.last_line - 1, loc.last_column),
  29. message: str});
  30. };
  31. try { jsonlint.parse(text); }
  32. catch(e) {}
  33. return found;
  34. });
  35. });