/
no-empty-function.ts
131 lines (120 loc) · 3.31 KB
/
no-empty-function.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import baseRule from 'eslint/lib/rules/no-empty-function';
import * as util from '../util';
type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;
const schema = util.deepMerge(
Array.isArray(baseRule.meta.schema)
? baseRule.meta.schema[0]
: baseRule.meta.schema,
{
properties: {
allow: {
items: {
enum: [
'functions',
'arrowFunctions',
'generatorFunctions',
'methods',
'generatorMethods',
'getters',
'setters',
'constructors',
'private-constructors',
'protected-constructors',
],
},
},
},
},
);
export default util.createRule<Options, MessageIds>({
name: 'no-empty-function',
meta: {
type: 'suggestion',
docs: {
description: 'Disallow empty functions',
category: 'Best Practices',
recommended: 'error',
},
schema: [schema],
messages: baseRule.meta.messages,
},
defaultOptions: [
{
allow: [],
},
],
create(context, [{ allow = [] }]) {
const rules = baseRule.create(context);
const isAllowedProtectedConstructors = allow.includes(
'protected-constructors',
);
const isAllowedPrivateConstructors = allow.includes('private-constructors');
/**
* Check if the method body is empty
* @param node the node to be validated
* @returns true if the body is empty
* @private
*/
function isBodyEmpty(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
): boolean {
return !node.body || node.body.body.length === 0;
}
/**
* Check if method has parameter properties
* @param node the node to be validated
* @returns true if the body has parameter properties
* @private
*/
function hasParameterProperties(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
): boolean {
return (
node.params &&
node.params.some(
param => param.type === AST_NODE_TYPES.TSParameterProperty,
)
);
}
/**
* @param node the node to be validated
* @returns true if the constructor is allowed to be empty
* @private
*/
function isAllowedEmptyConstructor(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
): boolean {
const parent = node.parent;
if (
isBodyEmpty(node) &&
parent?.type === AST_NODE_TYPES.MethodDefinition &&
parent.kind === 'constructor'
) {
const { accessibility } = parent;
return (
// allow protected constructors
(accessibility === 'protected' && isAllowedProtectedConstructors) ||
// allow private constructors
(accessibility === 'private' && isAllowedPrivateConstructors) ||
// allow constructors which have parameter properties
hasParameterProperties(node)
);
}
return false;
}
return {
...rules,
FunctionExpression(node): void {
if (isAllowedEmptyConstructor(node)) {
return;
}
rules.FunctionExpression(node);
},
};
},
});