/
no-dynamic-require.js
61 lines (55 loc) · 1.31 KB
/
no-dynamic-require.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
51
52
53
54
55
56
57
58
59
60
61
import docsUrl from '../docsUrl'
function isRequire(node) {
return node &&
node.callee &&
node.callee.type === 'Identifier' &&
node.callee.name === 'require' &&
node.arguments.length >= 1
}
function isDynamicImport(node) {
return node &&
node.callee &&
node.callee.type === 'Import'
}
function isStaticValue(arg) {
return arg.type === 'Literal' ||
(arg.type === 'TemplateLiteral' && arg.expressions.length === 0)
}
module.exports = {
meta: {
docs: {
url: docsUrl('no-dynamic-require'),
},
schema: [
{
type: 'object',
properties: {
esmodule: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
},
create: function (context) {
const options = context.options[0] || {}
return {
CallExpression(node) {
if(!node.arguments[0] || isStaticValue(node.arguments[0])) return
if (isRequire(node)) {
return context.report({
node,
message: 'Calls to require() should use string literals',
})
}
else if(options.esmodule && isDynamicImport(node)) {
return context.report({
node,
message: 'Calls to import() should use string literals',
})
}
},
}
},
}