/
no-nodejs-modules.js
45 lines (41 loc) · 1.1 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
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'),
},
schema: [
{
type: 'object',
properties: {
allow: {
type: 'array',
},
},
additionalProperties: false,
},
],
},
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)
}
},
}
},
}