-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
no-nodejs-modules.js
34 lines (30 loc) · 934 Bytes
/
no-nodejs-modules.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
import importType from '../core/importType'
import isStaticRequire from '../core/staticRequire'
import docsUrl from '../docsUrl'
function reportIfMissing(context, node, allowed, name) {
if (allowed.indexOf(name) === -1 && importType(name, context) === 'builtin') {
context.report(node, 'Do not import Node.js builtin module "' + name + '"')
}
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
url: docsUrl('no-nodejs-modules'),
},
},
create: function (context) {
const options = context.options[0] || {}
const allowed = options.allow || []
return {
ImportDeclaration: function handleImports(node) {
reportIfMissing(context, node, allowed, node.source.value)
},
CallExpression: function handleRequires(node) {
if (isStaticRequire(node)) {
reportIfMissing(context, node, allowed, node.arguments[0].value)
}
},
}
},
}