/
init-declarations.ts
53 lines (49 loc) · 1.39 KB
/
init-declarations.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
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import baseRule from 'eslint/lib/rules/init-declarations';
import {
InferOptionsTypeFromRule,
InferMessageIdsTypeFromRule,
createRule,
} from '../util';
export type Options = InferOptionsTypeFromRule<typeof baseRule>;
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>;
export default createRule<Options, MessageIds>({
name: 'init-declarations',
meta: {
type: 'suggestion',
docs: {
description:
'require or disallow initialization in variable declarations',
category: 'Variables',
recommended: false,
extendsBaseRule: true,
},
schema: baseRule.meta.schema,
messages: baseRule.meta.messages,
},
defaultOptions: ['always'],
create(context) {
const rules = baseRule.create(context);
const mode = context.options[0] || 'always';
return {
'VariableDeclaration:exit'(node: TSESTree.VariableDeclaration): void {
if (mode === 'always') {
if (node.declare) {
return;
}
if (
node.parent?.type === AST_NODE_TYPES.TSModuleBlock &&
node.parent.parent?.type === AST_NODE_TYPES.TSModuleDeclaration &&
node.parent.parent?.declare
) {
return;
}
}
rules['VariableDeclaration:exit'](node);
},
};
},
});