-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
50 lines (41 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var visit = require('unist-util-visit')
var rule = require('unified-lint-rule')
var path = require('path')
function lintCode (ast, file, linters) {
linters = linters || {}
// Make sure linter functions are loaded
Object.keys(linters).forEach(function (name) {
if (linters[name] instanceof Linter) return
linters[name] = Linter.from(linters[name])
})
// Visit code nodes and run corresponding linter, if available
visit(ast, 'code', function (node) {
var linter = linters[node.lang]
if (linter) linter.call(node, file)
})
}
function modOrString (mod) {
return typeof mod === 'string' ? require(path.resolve(mod)) : mod
}
function Linter (handler) {
this.handler = handler
}
Linter.prototype.call = function (node, file) {
return this.handler(node, file)
}
Linter.from = function (mod) {
var handler = mod
var opts = {}
if (typeof mod === 'object') {
handler = mod.module
if (mod.options) {
opts = mod.options
}
}
handler = modOrString(handler)
if ( ! handler) {
throw new Error('Unrecognized linter for ' + name)
}
return new Linter(handler(opts))
}
module.exports = rule('remark-lint:code', lintCode)