lint.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. var GUTTER_ID = 'CodeMirror-lint-markers'
  15. var LINT_LINE_ID = 'CodeMirror-lint-line-'
  16. function showTooltip(cm, e, content) {
  17. var tt = document.createElement('div')
  18. tt.className = 'CodeMirror-lint-tooltip cm-s-' + cm.options.theme
  19. tt.appendChild(content.cloneNode(true))
  20. if (cm.state.lint.options.selfContain) cm.getWrapperElement().appendChild(tt)
  21. else document.body.appendChild(tt)
  22. function position(e) {
  23. if (!tt.parentNode) return CodeMirror.off(document, 'mousemove', position)
  24. tt.style.top = Math.max(0, e.clientY - tt.offsetHeight - 5) + 'px'
  25. tt.style.left = e.clientX + 5 + 'px'
  26. }
  27. CodeMirror.on(document, 'mousemove', position)
  28. position(e)
  29. if (tt.style.opacity != null) tt.style.opacity = 1
  30. return tt
  31. }
  32. function rm(elt) {
  33. if (elt.parentNode) elt.parentNode.removeChild(elt)
  34. }
  35. function hideTooltip(tt) {
  36. if (!tt.parentNode) return
  37. if (tt.style.opacity == null) rm(tt)
  38. tt.style.opacity = 0
  39. setTimeout(function () {
  40. rm(tt)
  41. }, 600)
  42. }
  43. function showTooltipFor(cm, e, content, node) {
  44. var tooltip = showTooltip(cm, e, content)
  45. function hide() {
  46. CodeMirror.off(node, 'mouseout', hide)
  47. if (tooltip) {
  48. hideTooltip(tooltip)
  49. tooltip = null
  50. }
  51. }
  52. var poll = setInterval(function () {
  53. if (tooltip)
  54. for (var n = node; ; n = n.parentNode) {
  55. if (n && n.nodeType == 11) n = n.host
  56. if (n == document.body) return
  57. if (!n) {
  58. hide()
  59. break
  60. }
  61. }
  62. if (!tooltip) return clearInterval(poll)
  63. }, 400)
  64. CodeMirror.on(node, 'mouseout', hide)
  65. }
  66. function LintState(cm, conf, hasGutter) {
  67. this.marked = []
  68. if (conf instanceof Function) conf = { getAnnotations: conf }
  69. if (!conf || conf === true) conf = {}
  70. this.options = {}
  71. this.linterOptions = conf.options || {}
  72. for (var prop in defaults) this.options[prop] = defaults[prop]
  73. for (var prop in conf) {
  74. if (defaults.hasOwnProperty(prop)) {
  75. if (conf[prop] != null) this.options[prop] = conf[prop]
  76. } else if (!conf.options) {
  77. this.linterOptions[prop] = conf[prop]
  78. }
  79. }
  80. this.timeout = null
  81. this.hasGutter = hasGutter
  82. this.onMouseOver = function (e) {
  83. onMouseOver(cm, e)
  84. }
  85. this.waitingFor = 0
  86. }
  87. var defaults = {
  88. highlightLines: false,
  89. tooltips: true,
  90. delay: 500,
  91. lintOnChange: true,
  92. getAnnotations: null,
  93. async: false,
  94. selfContain: null,
  95. formatAnnotation: null,
  96. onUpdateLinting: null,
  97. }
  98. function clearMarks(cm) {
  99. var state = cm.state.lint
  100. if (state.hasGutter) cm.clearGutter(GUTTER_ID)
  101. if (state.options.highlightLines) clearErrorLines(cm)
  102. for (var i = 0; i < state.marked.length; ++i) state.marked[i].clear()
  103. state.marked.length = 0
  104. }
  105. function clearErrorLines(cm) {
  106. cm.eachLine(function (line) {
  107. var has = line.wrapClass && /\bCodeMirror-lint-line-\w+\b/.exec(line.wrapClass)
  108. if (has) cm.removeLineClass(line, 'wrap', has[0])
  109. })
  110. }
  111. function makeMarker(cm, labels, severity, multiple, tooltips) {
  112. var marker = document.createElement('div'),
  113. inner = marker
  114. marker.className = 'CodeMirror-lint-marker CodeMirror-lint-marker-' + severity
  115. if (multiple) {
  116. inner = marker.appendChild(document.createElement('div'))
  117. inner.className = 'CodeMirror-lint-marker CodeMirror-lint-marker-multiple'
  118. }
  119. if (tooltips != false)
  120. CodeMirror.on(inner, 'mouseover', function (e) {
  121. showTooltipFor(cm, e, labels, inner)
  122. })
  123. return marker
  124. }
  125. function getMaxSeverity(a, b) {
  126. if (a == 'error') return a
  127. else return b
  128. }
  129. function groupByLine(annotations) {
  130. var lines = []
  131. for (var i = 0; i < annotations.length; ++i) {
  132. var ann = annotations[i],
  133. line = ann.from.line
  134. ;(lines[line] || (lines[line] = [])).push(ann)
  135. }
  136. return lines
  137. }
  138. function annotationTooltip(ann) {
  139. var severity = ann.severity
  140. if (!severity) severity = 'error'
  141. var tip = document.createElement('div')
  142. tip.className = 'CodeMirror-lint-message CodeMirror-lint-message-' + severity
  143. if (typeof ann.messageHTML != 'undefined') {
  144. tip.innerHTML = ann.messageHTML
  145. } else {
  146. tip.appendChild(document.createTextNode(ann.message))
  147. }
  148. return tip
  149. }
  150. function lintAsync(cm, getAnnotations) {
  151. var state = cm.state.lint
  152. var id = ++state.waitingFor
  153. function abort() {
  154. id = -1
  155. cm.off('change', abort)
  156. }
  157. cm.on('change', abort)
  158. getAnnotations(
  159. cm.getValue(),
  160. function (annotations, arg2) {
  161. cm.off('change', abort)
  162. if (state.waitingFor != id) return
  163. if (arg2 && annotations instanceof CodeMirror) annotations = arg2
  164. cm.operation(function () {
  165. updateLinting(cm, annotations)
  166. })
  167. },
  168. state.linterOptions,
  169. cm
  170. )
  171. }
  172. function startLinting(cm) {
  173. var state = cm.state.lint
  174. if (!state) return
  175. var options = state.options
  176. /*
  177. * Passing rules in `options` property prevents JSHint (and other linters) from complaining
  178. * about unrecognized rules like `onUpdateLinting`, `delay`, `lintOnChange`, etc.
  179. */
  180. var getAnnotations = options.getAnnotations || cm.getHelper(CodeMirror.Pos(0, 0), 'lint')
  181. if (!getAnnotations) return
  182. if (options.async || getAnnotations.async) {
  183. lintAsync(cm, getAnnotations)
  184. } else {
  185. var annotations = getAnnotations(cm.getValue(), state.linterOptions, cm)
  186. if (!annotations) return
  187. if (annotations.then)
  188. annotations.then(function (issues) {
  189. cm.operation(function () {
  190. updateLinting(cm, issues)
  191. })
  192. })
  193. else
  194. cm.operation(function () {
  195. updateLinting(cm, annotations)
  196. })
  197. }
  198. }
  199. function updateLinting(cm, annotationsNotSorted) {
  200. var state = cm.state.lint
  201. if (!state) return
  202. var options = state.options
  203. clearMarks(cm)
  204. var annotations = groupByLine(annotationsNotSorted)
  205. for (var line = 0; line < annotations.length; ++line) {
  206. var anns = annotations[line]
  207. if (!anns) continue
  208. // filter out duplicate messages
  209. var message = []
  210. anns = anns.filter(function (item) {
  211. return message.indexOf(item.message) > -1 ? false : message.push(item.message)
  212. })
  213. var maxSeverity = null
  214. var tipLabel = state.hasGutter && document.createDocumentFragment()
  215. for (var i = 0; i < anns.length; ++i) {
  216. var ann = anns[i]
  217. var severity = ann.severity
  218. if (!severity) severity = 'error'
  219. maxSeverity = getMaxSeverity(maxSeverity, severity)
  220. if (options.formatAnnotation) ann = options.formatAnnotation(ann)
  221. if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann))
  222. if (ann.to)
  223. state.marked.push(
  224. cm.markText(ann.from, ann.to, {
  225. className: 'CodeMirror-lint-mark CodeMirror-lint-mark-' + severity,
  226. __annotation: ann,
  227. })
  228. )
  229. }
  230. // use original annotations[line] to show multiple messages
  231. if (state.hasGutter) cm.setGutterMarker(line, GUTTER_ID, makeMarker(cm, tipLabel, maxSeverity, annotations[line].length > 1, options.tooltips))
  232. if (options.highlightLines) cm.addLineClass(line, 'wrap', LINT_LINE_ID + maxSeverity)
  233. }
  234. if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm)
  235. }
  236. function onChange(cm) {
  237. var state = cm.state.lint
  238. if (!state) return
  239. clearTimeout(state.timeout)
  240. state.timeout = setTimeout(function () {
  241. startLinting(cm)
  242. }, state.options.delay)
  243. }
  244. function popupTooltips(cm, annotations, e) {
  245. var target = e.target || e.srcElement
  246. var tooltip = document.createDocumentFragment()
  247. for (var i = 0; i < annotations.length; i++) {
  248. var ann = annotations[i]
  249. tooltip.appendChild(annotationTooltip(ann))
  250. }
  251. showTooltipFor(cm, e, tooltip, target)
  252. }
  253. function onMouseOver(cm, e) {
  254. var target = e.target || e.srcElement
  255. if (!/\bCodeMirror-lint-mark-/.test(target.className)) return
  256. var box = target.getBoundingClientRect(),
  257. x = (box.left + box.right) / 2,
  258. y = (box.top + box.bottom) / 2
  259. var spans = cm.findMarksAt(cm.coordsChar({ left: x, top: y }, 'client'))
  260. var annotations = []
  261. for (var i = 0; i < spans.length; ++i) {
  262. var ann = spans[i].__annotation
  263. if (ann) annotations.push(ann)
  264. }
  265. if (annotations.length) popupTooltips(cm, annotations, e)
  266. }
  267. CodeMirror.defineOption('lint', false, function (cm, val, old) {
  268. if (old && old != CodeMirror.Init) {
  269. clearMarks(cm)
  270. if (cm.state.lint.options.lintOnChange !== false) cm.off('change', onChange)
  271. CodeMirror.off(cm.getWrapperElement(), 'mouseover', cm.state.lint.onMouseOver)
  272. clearTimeout(cm.state.lint.timeout)
  273. delete cm.state.lint
  274. }
  275. if (val) {
  276. var gutters = cm.getOption('gutters'),
  277. hasLintGutter = false
  278. for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true
  279. var state = (cm.state.lint = new LintState(cm, val, hasLintGutter))
  280. if (state.options.lintOnChange) cm.on('change', onChange)
  281. if (state.options.tooltips != false && state.options.tooltips != 'gutter') CodeMirror.on(cm.getWrapperElement(), 'mouseover', state.onMouseOver)
  282. startLinting(cm)
  283. }
  284. })
  285. CodeMirror.defineExtension('performLint', function () {
  286. startLinting(this)
  287. })
  288. })