yaml-lint.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. ;(function (mod) {
  4. if (typeof exports == 'object' && typeof module == 'object')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. // Depends on js-yaml.js from https://github.com/nodeca/js-yaml
  15. // declare global: jsyaml
  16. CodeMirror.registerHelper('lint', 'yaml', function (text) {
  17. var found = []
  18. if (!window.jsyaml) {
  19. if (window.console) {
  20. window.console.error('Error: window.jsyaml not defined, CodeMirror YAML linting cannot run.')
  21. }
  22. return found
  23. }
  24. try {
  25. jsyaml.loadAll(text)
  26. } catch (e) {
  27. var loc = e.mark,
  28. // js-yaml YAMLException doesn't always provide an accurate lineno
  29. // e.g., when there are multiple yaml docs
  30. // ---
  31. // ---
  32. // foo:bar
  33. from = loc ? CodeMirror.Pos(loc.line, loc.column) : CodeMirror.Pos(0, 0),
  34. to = from
  35. found.push({ from: from, to: to, message: e.message })
  36. }
  37. return found
  38. })
  39. })