forked from typescript-eslint/typescript-eslint
/
no-var-requires.ts
49 lines (47 loc) · 1.36 KB
/
no-var-requires.ts
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
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';
type Options = [];
type MessageIds = 'noVarReqs';
export default util.createRule<Options, MessageIds>({
name: 'no-var-requires',
meta: {
type: 'problem',
docs: {
description:
'Disallows the use of require statements except in import statements',
category: 'Best Practices',
recommended: 'error',
},
messages: {
noVarReqs: 'Require statement not part of import statement.',
},
schema: [],
},
defaultOptions: [],
create(context) {
return {
'CallExpression, OptionalCallExpression'(
node: TSESTree.CallExpression | TSESTree.OptionalCallExpression,
): void {
if (
node.callee.type === AST_NODE_TYPES.Identifier &&
node.callee.name === 'require' &&
node.parent &&
(node.parent.type === AST_NODE_TYPES.VariableDeclarator ||
node.parent.type === AST_NODE_TYPES.CallExpression ||
node.parent.type === AST_NODE_TYPES.OptionalCallExpression ||
node.parent.type === AST_NODE_TYPES.TSAsExpression ||
node.parent.type === AST_NODE_TYPES.MemberExpression)
) {
context.report({
node,
messageId: 'noVarReqs',
});
}
},
};
},
});