Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/src/rules/no-dynamic-require.js
829 views
1
import docsUrl from '../docsUrl';
2
3
function isRequire(node) {
4
return node &&
5
node.callee &&
6
node.callee.type === 'Identifier' &&
7
node.callee.name === 'require' &&
8
node.arguments.length >= 1;
9
}
10
11
function isDynamicImport(node) {
12
return node &&
13
node.callee &&
14
node.callee.type === 'Import';
15
}
16
17
function isStaticValue(arg) {
18
return arg.type === 'Literal' ||
19
(arg.type === 'TemplateLiteral' && arg.expressions.length === 0);
20
}
21
22
module.exports = {
23
meta: {
24
type: 'suggestion',
25
docs: {
26
url: docsUrl('no-dynamic-require'),
27
},
28
schema: [
29
{
30
type: 'object',
31
properties: {
32
esmodule: {
33
type: 'boolean',
34
},
35
},
36
additionalProperties: false,
37
},
38
],
39
},
40
41
create(context) {
42
const options = context.options[0] || {};
43
44
return {
45
CallExpression(node) {
46
if (!node.arguments[0] || isStaticValue(node.arguments[0])) {
47
return;
48
}
49
if (isRequire(node)) {
50
return context.report({
51
node,
52
message: 'Calls to require() should use string literals',
53
});
54
}
55
if (options.esmodule && isDynamicImport(node)) {
56
return context.report({
57
node,
58
message: 'Calls to import() should use string literals',
59
});
60
}
61
},
62
};
63
},
64
};
65
66