forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmust-call-assert.js
More file actions
225 lines (206 loc) · 8.54 KB
/
Copy pathmust-call-assert.js
File metadata and controls
225 lines (206 loc) · 8.54 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict';
const message =
'Assertions must be wrapped into `common.mustSucceed`, `common.mustCall` or `common.mustCallAtLeast`';
const requireCall = 'CallExpression[callee.name="require"]';
const assertModuleSpecifier = '/^(node:)?assert(.strict)?$/';
const isObjectKeysOrEntries = (node) =>
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'Object' &&
node.callee.property.type === 'Identifier' &&
['keys', 'entries'].includes(node.callee.property.name);
const isPromiseAllCallArg = (node) =>
node.parent?.type === 'CallExpression' &&
node.parent.callee.type === 'MemberExpression' &&
node.parent.callee.object.type === 'Identifier' && node.parent.callee.object.name === 'Promise' &&
node.parent.callee.property.type === 'Identifier' && node.parent.callee.property.name === 'all' &&
node.parent.arguments.length === 1 && node.parent.arguments[0] === node;
function findEnclosingFunction(node) {
while (true) {
node = node.parent;
if (!node) break;
if (node.type !== 'ArrowFunctionExpression' && node.type !== 'FunctionExpression') continue;
if (node.parent?.type === 'CallExpression') {
if (node.parent.callee === node) continue; // IIFE
if (
node.parent.callee.type === 'MemberExpression' &&
(
node.parent.callee.object.type === 'ArrayExpression' ||
node.parent.callee.object.type === 'Identifier' ||
isObjectKeysOrEntries(node.parent.callee.object)
) &&
(
node.parent.callee.property.name === 'forEach' ||
(node.parent.callee.property.name === 'map' && isPromiseAllCallArg(node.parent))
)
) continue; // `[].forEach()` call
} else if (node.parent?.type === 'NewExpression') {
if (node.parent.callee.type === 'Identifier' && node.parent.callee.name === 'Promise') continue;
} else if (node.parent?.type === 'Property') {
const ancestor = node.parent.parent?.parent;
if (ancestor?.type === 'CallExpression' &&
ancestor.callee.type === 'Identifier' &&
/^spawnSyncAnd(Exit(WithoutError)?|Assert)$/.test(ancestor.callee.name)) {
continue;
}
}
break;
}
return node;
}
function isMustCallOrMustCallAtLeast(str) {
return str === 'mustCall' || str === 'mustCallAtLeast' || str === 'mustSucceed';
}
function isMustCallOrTest(str) {
return str === 'test' || str === 'it' || str === 'describe' || str === 'suite' ||
str === 'before' || str === 'after' || str === 'beforeEach' || str === 'afterEach' ||
isMustCallOrMustCallAtLeast(str);
}
module.exports = {
meta: {
fixable: 'code',
},
create: function(context) {
return {
[`:function CallExpression:matches(${[
'[callee.type="Identifier"][callee.name=/^mustCall(AtLeast)?$/]',
'[callee.type="Identifier"][callee.name="assert"]',
'[callee.object.name="assert"][callee.property.name!="fail"]',
'[callee.object.name="common"][callee.property.name=/^mustCall(AtLeast)?$/]',
].join(',')})`]: (node) => {
const enclosingFn = findEnclosingFunction(node);
const parent = enclosingFn?.parent;
if (!parent) return; // Top-level
if (parent.type === 'BinaryExpression' && parent.operator === '+') {
// Function is stringified, e.g. to be passed to a child process or a worker.
return;
}
if (parent.type === 'CallExpression') {
switch (parent.callee.type) {
case 'MemberExpression':
if (
parent.callee.property.name === 'then' ||
{
assert: (name) => name === 'rejects' || name === 'throws', // assert.throws or assert.rejects
common: isMustCallOrMustCallAtLeast, // common.mustCall or common.mustCallAtLeast
process: (name) =>
(name === 'nextTick' && enclosingFn === parent.arguments[0]) || // process.nextTick
( // process.on('exit', …)
(name === 'on' || name === 'once') &&
enclosingFn === parent.arguments[1] &&
parent.arguments[0].type === 'Literal' &&
parent.arguments[0].value === 'exit'
),
t: (name) => name === 'test', // t.test
}[parent.callee.object.name]?.(parent.callee.property.name)
) {
return;
}
break;
case 'Identifier':
if (isMustCallOrTest(parent.callee.name)) return;
break;
}
}
context.report({
node,
message,
});
},
[[
`ImportDeclaration[source.value=${assertModuleSpecifier}]:not(${[
'length=1',
'0.type=/^Import(Default|Namespace)Specifier$/',
'0.local.name="assert"',
].map((selector) => `[specifiers.${selector}]`).join('')})`,
`:not(VariableDeclarator[id.name="assert"])>${requireCall}[arguments.0.value=${assertModuleSpecifier}]`,
].join(',')]: (node) => {
context.report({
node,
message: 'Only assign `node:assert` to `assert`',
});
},
[`CallExpression[callee.property.name="then"][arguments.length=1]>CallExpression:matches(${[
'[callee.name="mustCall"]',
'[callee.object.name="common"][callee.property.name="mustCall"]',
].join(',')})[arguments.length=1]>:has(ReturnStatement)`]: (node) => {
context.report({
node,
message: 'Cannot mix `common.mustCall` and return statement inside a `.then` chain',
});
},
'ExpressionStatement>CallExpression[callee.property.name="then"]': (node) => {
const { arguments: { length, 0: arg }, callee: { object } } = node;
if (object.type === 'CallExpression' &&
object.callee.type === 'Identifier' &&
object.callee.name === 'checkSupportReusePort') {
return;
}
const defaultMessage = 'Last item of `.then` list must use `mustCall()` ' +
'or `mustNotCall("expected never settling promise")`';
if (length !== 1) {
context.report({
node,
message: 'Expected exactly one argument for `.then`, consider using ' +
'`assert.rejects` or remove the second argument',
});
} else if (arg.type !== 'CallExpression') {
context.report({
node: arg,
message: defaultMessage,
});
} else {
const message = {
mustCall(args) {
switch (args.length) {
case 0: return false;
case 1: {
const [arg] = args;
if (arg.async || arg.body.type !== 'BlockStatement')
return 'Add an additional `.then(common.mustCall())` to detect if resulting promise settles';
return false;
}
default: return 'do not use more than one argument';
}
},
mustNotCall(args) {
if (args.length === 1 && args[0].type === 'Literal' && args[0].value.includes('never settling')) {
return false;
}
return 'Unexpected `common.mustNotCall`, either add a label mention ' +
'"never settling promise" or use `common.mustCall`';
},
}[arg.callee.type === 'MemberExpression' && arg.callee.object.name === 'common' ?
arg.callee.property.name :
arg.callee.name]?.(arg.arguments) ?? defaultMessage;
if (message) {
context.report({ node: arg, message });
}
}
node = node.callee.object;
// Walk down .then chain
while (
node?.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.property.name === 'then'
) {
const { arguments: { 0: arg } } = node;
if (arg &&
arg.type === 'CallExpression' &&
(arg.callee.type === 'MemberExpression' && arg.callee.object.name === 'common' ?
arg.callee.property.name :
arg.callee.name
) === 'mustCall') {
context.report({
node,
message: 'in a `.then` chain, only use mustCall as the last node',
});
}
// Go deeper: find next "left" call in chain
node = node.callee.object;
}
},
};
},
};