coffeescript-lint.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // Depends on coffeelint.js from http://www.coffeelint.org/js/coffeelint.js
  4. // declare global: coffeelint
  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', 'coffeescript', function (text) {
  17. var found = []
  18. if (!window.coffeelint) {
  19. if (window.console) {
  20. window.console.error('Error: window.coffeelint not defined, CodeMirror CoffeeScript linting cannot run.')
  21. }
  22. return found
  23. }
  24. var parseError = function (err) {
  25. var loc = err.lineNumber
  26. found.push({ from: CodeMirror.Pos(loc - 1, 0), to: CodeMirror.Pos(loc, 0), severity: err.level, message: err.message })
  27. }
  28. try {
  29. var res = coffeelint.lint(text)
  30. for (var i = 0; i < res.length; i++) {
  31. parseError(res[i])
  32. }
  33. } catch (e) {
  34. found.push({ from: CodeMirror.Pos(e.location.first_line, 0), to: CodeMirror.Pos(e.location.last_line, e.location.last_column), severity: 'error', message: e.message })
  35. }
  36. return found
  37. })
  38. })