/
no-var-requires.ts
55 lines (52 loc) · 1.33 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
50
51
52
53
54
55
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[callee.name="require"]'(
node: TSESTree.CallExpression,
): void {
const parent =
node.parent?.type === AST_NODE_TYPES.ChainExpression
? node.parent.parent
: node.parent;
if (
parent &&
[
AST_NODE_TYPES.CallExpression,
AST_NODE_TYPES.MemberExpression,
AST_NODE_TYPES.NewExpression,
AST_NODE_TYPES.TSAsExpression,
AST_NODE_TYPES.TSTypeAssertion,
AST_NODE_TYPES.VariableDeclarator,
].includes(parent.type)
) {
context.report({
node,
messageId: 'noVarReqs',
});
}
},
};
},
});