json-lint.js 1.4 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')
  7. // CommonJS
  8. mod(require('../../lib/codemirror'))
  9. else if (typeof define == 'function' && define.amd)
  10. // AMD
  11. define(['../../lib/codemirror'], mod)
  12. // Plain browser env
  13. else mod(CodeMirror)
  14. })(function (CodeMirror) {
  15. 'use strict'
  16. CodeMirror.registerHelper('lint', 'json', function (text) {
  17. var found = []
  18. if (!window.jsonlint) {
  19. if (window.console) {
  20. window.console.error('Error: window.jsonlint not defined, CodeMirror JSON linting cannot run.')
  21. }
  22. return found
  23. }
  24. // for jsonlint's web dist jsonlint is exported as an object with a single property parser, of which parseError
  25. // is a subproperty
  26. var jsonlint = window.jsonlint.parser || window.jsonlint
  27. jsonlint.parseError = function (str, hash) {
  28. var loc = hash.loc
  29. found.push({ from: CodeMirror.Pos(loc.first_line - 1, loc.first_column), to: CodeMirror.Pos(loc.last_line - 1, loc.last_column), message: str })
  30. }
  31. try {
  32. jsonlint.parse(text)
  33. } catch (e) {}
  34. return found
  35. })
  36. })