-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
no-useless-constructor.ts
79 lines (73 loc) · 2.03 KB
/
no-useless-constructor.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import type {
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../util';
import { createRule } from '../util';
import { getESLintCoreRule } from '../util/getESLintCoreRule';
const baseRule = getESLintCoreRule('no-useless-constructor');
type Options = InferOptionsTypeFromRule<typeof baseRule>;
type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>;
/**
* Check if method with accessibility is not useless
*/
function checkAccessibility(node: TSESTree.MethodDefinition): boolean {
switch (node.accessibility) {
case 'protected':
case 'private':
return false;
case 'public':
if (
node.parent.type === AST_NODE_TYPES.ClassBody &&
(
node.parent.parent as
| TSESTree.ClassDeclaration
| TSESTree.ClassExpression
).superClass
) {
return false;
}
break;
}
return true;
}
/**
* Check if method is not useless due to typescript parameter properties and decorators
*/
function checkParams(node: TSESTree.MethodDefinition): boolean {
return !node.value.params.some(
param =>
param.type === AST_NODE_TYPES.TSParameterProperty ||
param.decorators.length,
);
}
export default createRule<Options, MessageIds>({
name: 'no-useless-constructor',
meta: {
type: 'problem',
docs: {
description: 'Disallow unnecessary constructors',
recommended: 'strict',
extendsBaseRule: true,
},
hasSuggestions: baseRule.meta.hasSuggestions,
schema: baseRule.meta.schema,
messages: baseRule.meta.messages,
},
defaultOptions: [],
create(context) {
const rules = baseRule.create(context);
return {
MethodDefinition(node): void {
if (
node.value.type === AST_NODE_TYPES.FunctionExpression &&
checkAccessibility(node) &&
checkParams(node)
) {
rules.MethodDefinition(node);
}
},
};
},
});