Skip to content

Commit 6f83c29

Browse files
committed
Refactor to get rid of outdated enhance-visitors dependency
1 parent d27e542 commit 6f83c29

38 files changed

Lines changed: 396 additions & 342 deletions

create-ava-rule.js

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import enhance from 'enhance-visitors';
21
import {getTestModifiers, unwrapTypeExpression} from './util.js';
32

4-
export default () => {
3+
export default context => {
54
let isTestFile = false;
6-
let currentTestNode;
75
const testIdentifiers = new Set();
86

97
function isTestFunctionCall(node) {
@@ -18,11 +16,8 @@ export default () => {
1816
return false;
1917
}
2018

21-
function getTestModifierNames(node) {
22-
return getTestModifiers(node).map(property => property.name);
23-
}
19+
const getModifierNames = node => getTestModifiers(node).map(property => property.name);
2420

25-
/* eslint quote-props: [2, "as-needed"] */
2621
const predefinedRules = {
2722
ImportDeclaration(node) {
2823
if (node.source.value !== 'ava' || node.importKind === 'type') {
@@ -51,37 +46,57 @@ export default () => {
5146
testIdentifiers.add(node.id.name);
5247
}
5348
},
54-
CallExpression(node) {
55-
if (isTestFunctionCall(node.callee)) {
56-
// Entering test function
57-
currentTestNode = node;
58-
}
59-
},
60-
'CallExpression:exit'(node) {
61-
if (currentTestNode === node) {
62-
// Leaving test function
63-
currentTestNode = undefined;
64-
}
65-
},
6649
'Program:exit'() {
6750
isTestFile = false;
6851
testIdentifiers.clear();
6952
},
7053
};
7154

7255
return {
73-
hasTestModifier: module_ => getTestModifierNames(currentTestNode).includes(module_),
74-
hasNoUtilityModifier() {
75-
const modifiers = getTestModifierNames(currentTestNode);
56+
isInTestFile: () => isTestFile,
57+
isTestNode: node => node.type === 'CallExpression' && isTestFunctionCall(node.callee),
58+
isInTestNode(node) {
59+
if (node.type === 'CallExpression' && isTestFunctionCall(node.callee)) {
60+
return node;
61+
}
62+
63+
const ancestors = context.sourceCode.getAncestors(node);
64+
for (let index = ancestors.length - 1; index >= 0; index--) {
65+
const ancestor = ancestors[index];
66+
if (ancestor.type === 'CallExpression' && isTestFunctionCall(ancestor.callee)) {
67+
return ancestor;
68+
}
69+
}
70+
71+
return undefined;
72+
},
73+
hasTestModifier: (node, modifier) => getModifierNames(node).includes(modifier),
74+
hasNoUtilityModifier(node) {
75+
const modifiers = getModifierNames(node);
7676
return !modifiers.includes('before')
7777
&& !modifiers.includes('beforeEach')
7878
&& !modifiers.includes('after')
7979
&& !modifiers.includes('afterEach')
8080
&& !modifiers.includes('macro');
8181
},
82-
isInTestFile: () => isTestFile,
83-
isInTestNode: () => currentTestNode,
84-
isTestNode: node => currentTestNode === node,
85-
merge: customHandlers => enhance.mergeVisitors([predefinedRules, customHandlers]),
82+
merge: customHandlers => ({
83+
...predefinedRules,
84+
...Object.fromEntries(Object.entries(customHandlers).map(([key, custom]) => {
85+
const predefined = predefinedRules[key];
86+
if (!predefined) {
87+
return [key, custom];
88+
}
89+
90+
return [key, key.endsWith(':exit')
91+
? node => {
92+
custom(node);
93+
predefined(node);
94+
}
95+
: node => {
96+
predefined(node);
97+
custom(node);
98+
}];
99+
})),
100+
}),
86101
};
87102
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"dependencies": {
4545
"@eslint-community/eslint-utils": "^4.9.1",
4646
"@eslint/json": "^1.0.0",
47-
"enhance-visitors": "^1.0.0",
4847
"espree": "^11.1.0",
4948
"espurify": "^3.2.0",
5049
"micro-spelling-correcter": "^1.1.1",

rules/assertion-arguments.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import {visitIf} from 'enhance-visitors';
21
import {
32
getStaticValue, isOpeningParenToken, isCommaToken, findVariable,
43
} from '@eslint-community/eslint-utils';
5-
import util from '../util.js';
64
import createAvaRule from '../create-ava-rule.js';
5+
import util from '../util.js';
76

87
const MESSAGE_ID_TOO_FEW = 'too-few-arguments';
98
const MESSAGE_ID_TOO_MANY = 'too-many-arguments';
@@ -227,16 +226,17 @@ function isString(node) {
227226
}
228227

229228
const create = context => {
230-
const ava = createAvaRule();
229+
const ava = createAvaRule(context);
231230
const options = context.options[0];
232231
const enforcesMessage = Boolean(options.message);
233232
const shouldHaveMessage = options.message !== 'never';
234233

235234
return ava.merge({
236-
CallExpression: visitIf([
237-
ava.isInTestFile,
238-
ava.isInTestNode,
239-
])(node => {
235+
CallExpression(node) {
236+
if (!ava.isInTestFile() || !ava.isInTestNode(node)) {
237+
return;
238+
}
239+
240240
const {callee} = node;
241241

242242
if (
@@ -352,7 +352,7 @@ const create = context => {
352352
context.report({node, messageId: MESSAGE_ID_NOT_STRING});
353353
}
354354
}
355-
}),
355+
},
356356
});
357357
};
358358

rules/failing-test-url.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {visitIf} from 'enhance-visitors';
21
import createAvaRule from '../create-ava-rule.js';
32
import util from '../util.js';
43

@@ -7,13 +6,14 @@ const MESSAGE_ID = 'failing-test-url';
76
const urlPattern = /https?:\/\/\S+/;
87

98
const create = context => {
10-
const ava = createAvaRule();
9+
const ava = createAvaRule(context);
1110

1211
return ava.merge({
13-
CallExpression: visitIf([
14-
ava.isInTestFile,
15-
ava.isTestNode,
16-
])(node => {
12+
CallExpression(node) {
13+
if (!ava.isInTestFile() || !ava.isTestNode(node)) {
14+
return;
15+
}
16+
1717
const propertyNode = util.getTestModifier(node, 'failing');
1818
if (!propertyNode) {
1919
return;
@@ -28,7 +28,7 @@ const create = context => {
2828
messageId: MESSAGE_ID,
2929
});
3030
}
31-
}),
31+
},
3232
});
3333
};
3434

rules/hooks-order.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {visitIf} from 'enhance-visitors';
21
import createAvaRule from '../create-ava-rule.js';
32
import util from '../util.js';
43

@@ -40,7 +39,7 @@ const buildMessage = (name, orders, visited) => {
4039
};
4140

4241
const create = context => {
43-
const ava = createAvaRule();
42+
const ava = createAvaRule(context);
4443

4544
const orders = buildOrders([
4645
'before',
@@ -89,10 +88,11 @@ const create = context => {
8988

9089
const selectors = {};
9190
for (const check of checks) {
92-
selectors[check.selector] = visitIf([
93-
ava.isInTestFile,
94-
ava.isTestNode,
95-
])(node => {
91+
selectors[check.selector] = node => {
92+
if (!ava.isInTestFile() || !ava.isTestNode(node)) {
93+
return;
94+
}
95+
9696
visited[check.name] = node;
9797

9898
const message = buildMessage(check.name, orders, visited);
@@ -136,7 +136,7 @@ const create = context => {
136136
},
137137
});
138138
}
139-
});
139+
};
140140
}
141141

142142
return ava.merge(selectors);

rules/max-asserts.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import {visitIf} from 'enhance-visitors';
2-
import util from '../util.js';
31
import createAvaRule from '../create-ava-rule.js';
2+
import util from '../util.js';
43

54
const MESSAGE_ID = 'max-asserts';
65

76
const notAssertionMethods = new Set(['plan', 'end']);
87

98
const create = context => {
10-
const ava = createAvaRule();
9+
const ava = createAvaRule(context);
1110
const {max: maxAssertions} = context.options[0];
1211
let assertionCount = 0;
1312
let nodeToReport;
1413

1514
return ava.merge({
16-
CallExpression: visitIf([
17-
ava.isInTestFile,
18-
ava.isInTestNode,
19-
])(node => {
15+
CallExpression(node) {
16+
if (!ava.isInTestFile() || !ava.isInTestNode(node)) {
17+
return;
18+
}
19+
2020
const {callee} = node;
2121

2222
if (callee.type !== 'MemberExpression') {
@@ -40,8 +40,12 @@ const create = context => {
4040
nodeToReport = node;
4141
}
4242
}
43-
}),
44-
'CallExpression:exit': visitIf([ava.isTestNode])(() => {
43+
},
44+
'CallExpression:exit'(node) {
45+
if (!ava.isTestNode(node)) {
46+
return;
47+
}
48+
4549
// Leaving test function
4650
if (assertionCount > maxAssertions) {
4751
context.report({
@@ -53,7 +57,7 @@ const create = context => {
5357

5458
assertionCount = 0;
5559
nodeToReport = undefined;
56-
}),
60+
},
5761
});
5862
};
5963

rules/no-async-fn-without-await.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import {visitIf} from 'enhance-visitors';
21
import createAvaRule from '../create-ava-rule.js';
32
import util from '../util.js';
43

54
const MESSAGE_ID = 'no-async-fn-without-await';
65
const MESSAGE_ID_SUGGESTION = 'no-async-fn-without-await-suggestion';
76

87
const create = context => {
9-
const ava = createAvaRule();
8+
const ava = createAvaRule(context);
109
let testUsed = false;
1110
let asyncTest;
1211
let nestedFunctionDepth = 0;
@@ -32,22 +31,24 @@ const create = context => {
3231
const isAsync = node => Boolean(node?.async);
3332

3433
return ava.merge({
35-
CallExpression: visitIf([
36-
ava.isInTestFile,
37-
ava.isTestNode,
38-
])(node => {
34+
CallExpression(node) {
35+
if (!ava.isInTestFile() || !ava.isTestNode(node)) {
36+
return;
37+
}
38+
3939
asyncTest = (isAsync(node.arguments[0]) && node.arguments[0])
4040
|| (isAsync(node.arguments[1]) && node.arguments[1]);
41-
}),
41+
},
4242
':function': enterFunction,
4343
':function:exit': exitFunction,
4444
AwaitExpression: registerUseOfAwait,
4545
YieldExpression: registerUseOfAwait,
4646
'ForOfStatement[await=true]': registerUseOfAwait,
47-
'CallExpression:exit': visitIf([
48-
ava.isInTestFile,
49-
ava.isTestNode,
50-
])(() => {
47+
'CallExpression:exit'(node) {
48+
if (!ava.isInTestFile() || !ava.isTestNode(node)) {
49+
return;
50+
}
51+
5152
if (asyncTest && !testUsed) {
5253
const {sourceCode} = context;
5354
const asyncToken = sourceCode.getFirstToken(asyncTest, token => token.value === 'async');
@@ -68,7 +69,7 @@ const create = context => {
6869
asyncTest = undefined;
6970
testUsed = false;
7071
nestedFunctionDepth = 0;
71-
}),
72+
},
7273
});
7374
};
7475

rules/no-commented-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const MESSAGE_ID = 'no-commented-tests';
66
const commentedTestPattern = /^\s*\*?\s*(?:test|serial)\s*(?:\.\s*\w+\s*)*\(/;
77

88
const create = context => {
9-
const ava = createAvaRule();
9+
const ava = createAvaRule(context);
1010

1111
return ava.merge({
1212
'Program:exit'() {

rules/no-conditional-assertion.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {visitIf} from 'enhance-visitors';
21
import createAvaRule from '../create-ava-rule.js';
32
import util from '../util.js';
43

@@ -269,21 +268,30 @@ function shouldTrackConditionalAncestor(node, child) {
269268
}
270269

271270
const create = context => {
272-
const ava = createAvaRule();
271+
const ava = createAvaRule(context);
273272

274273
return ava.merge({
275-
CallExpression: visitIf([ava.isInTestFile, ava.isInTestNode])(node => {
274+
CallExpression(node) {
275+
if (!ava.isInTestFile()) {
276+
return;
277+
}
278+
279+
const testNode = ava.isInTestNode(node);
280+
if (!testNode) {
281+
return;
282+
}
283+
276284
if (!isAssertionCall(node)) {
277285
return;
278286
}
279287

280-
for (const conditional of conditionalAncestors(node, ava.isInTestNode())) {
288+
for (const conditional of conditionalAncestors(node, testNode)) {
281289
if (!isBalanced(conditional)) {
282290
context.report({node, messageId: MESSAGE_ID});
283291
break;
284292
}
285293
}
286-
}),
294+
},
287295
});
288296
};
289297

0 commit comments

Comments
 (0)