Skip to content

Commit 9c84508

Browse files
committed
move check into create
1 parent 6683935 commit 9c84508

File tree

1 file changed

+52
-49
lines changed

1 file changed

+52
-49
lines changed

src/rules/prefer-in-document.js

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,57 +27,60 @@ function isAntonymMatcher(matcherNode, matcherArguments) {
2727
);
2828
}
2929

30-
function check(
31-
context,
32-
{ queryNode, matcherNode, matcherArguments, negatedMatcher, expect }
33-
) {
34-
if (!queryNode || (!queryNode.name && !queryNode.property)) return;
35-
// toHaveLength() is only invalid with 0 or 1
36-
if (matcherNode.name === "toHaveLength" && matcherArguments[0].value > 1) {
37-
return;
38-
}
30+
export const create = (context) => {
31+
const alternativeMatchers = /(toHaveLength|toBeDefined|toBeNull)/;
32+
function check({
33+
queryNode,
34+
matcherNode,
35+
matcherArguments,
36+
negatedMatcher,
37+
expect,
38+
}) {
39+
if (!queryNode || (!queryNode.name && !queryNode.property)) return;
40+
// toHaveLength() is only invalid with 0 or 1
41+
if (matcherNode.name === "toHaveLength" && matcherArguments[0].value > 1) {
42+
return;
43+
}
44+
45+
const query = queryNode.name || queryNode.property.name;
46+
47+
if (queries.includes(query)) {
48+
context.report({
49+
node: matcherNode,
50+
messageId: "use-document",
51+
loc: matcherNode.loc,
52+
fix(fixer) {
53+
const operations = [];
3954

40-
const query = queryNode.name || queryNode.property.name;
41-
42-
if (queries.includes(query)) {
43-
context.report({
44-
node: matcherNode,
45-
messageId: "use-document",
46-
loc: matcherNode.loc,
47-
fix(fixer) {
48-
const operations = [];
49-
50-
// Remove any arguments in the matcher
51-
for (const argument of Array.from(matcherArguments)) {
52-
operations.push(fixer.remove(argument));
53-
}
54-
// Flip the .not if necessary
55-
if (isAntonymMatcher(matcherNode, matcherArguments)) {
56-
if (negatedMatcher) {
57-
operations.push(
58-
fixer.replaceTextRange(
59-
[expect.range[1], matcherNode.range[1]],
60-
".toBeInTheDocument"
61-
)
62-
);
63-
64-
return operations;
65-
} else {
66-
operations.push(fixer.insertTextBefore(matcherNode, "not."));
55+
// Remove any arguments in the matcher
56+
for (const argument of Array.from(matcherArguments)) {
57+
operations.push(fixer.remove(argument));
58+
}
59+
// Flip the .not if necessary
60+
if (isAntonymMatcher(matcherNode, matcherArguments)) {
61+
if (negatedMatcher) {
62+
operations.push(
63+
fixer.replaceTextRange(
64+
[expect.range[1], matcherNode.range[1]],
65+
".toBeInTheDocument"
66+
)
67+
);
68+
69+
return operations;
70+
} else {
71+
operations.push(fixer.insertTextBefore(matcherNode, "not."));
72+
}
6773
}
68-
}
6974

70-
// Replace the actual matcher
71-
operations.push(fixer.replaceText(matcherNode, "toBeInTheDocument"));
75+
// Replace the actual matcher
76+
operations.push(fixer.replaceText(matcherNode, "toBeInTheDocument"));
7277

73-
return operations;
74-
},
75-
});
78+
return operations;
79+
},
80+
});
81+
}
7682
}
77-
}
7883

79-
export const create = (context) => {
80-
const alternativeMatchers = /(toHaveLength|toBeDefined|toBeNull)/;
8184
function getQueryNodeFromAssignment(identifierName) {
8285
const variable = context.getScope().set.get(identifierName);
8386
const init = variable.defs[0].node.init;
@@ -119,7 +122,7 @@ export const create = (context) => {
119122
const matcherArguments = node.arguments;
120123

121124
const expect = node.callee.object.object;
122-
check(context, {
125+
check({
123126
negatedMatcher: true,
124127
queryNode,
125128
matcherNode,
@@ -141,7 +144,7 @@ export const create = (context) => {
141144

142145
const expect = node.object.object;
143146

144-
check(context, {
147+
check({
145148
negatedMatcher: true,
146149
queryNode,
147150
matcherNode,
@@ -160,7 +163,7 @@ export const create = (context) => {
160163

161164
const matcherArguments = node.parent.arguments;
162165

163-
check(context, {
166+
check({
164167
negatedMatcher: false,
165168
queryNode,
166169
matcherNode,
@@ -178,7 +181,7 @@ export const create = (context) => {
178181
const matcherNode = node.callee.property;
179182
const matcherArguments = node.arguments;
180183

181-
check(context, {
184+
check({
182185
negatedMatcher: false,
183186
queryNode,
184187
matcherNode,

0 commit comments

Comments
 (0)