Skip to content

Commit 45cf87b

Browse files
refactor: Modernize codebase (#42)
1 parent 00c423b commit 45cf87b

16 files changed

+644
-755
lines changed

src/__tests__/__fixtures__/createBannedAttributeTestCases.js

+55-56
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,60 @@
11
/* eslint-disable max-lines-per-function */
22

3-
module.exports = ({ preferred, negatedPreferred, attribute }) => {
4-
let doubleNegativeCases = [];
5-
if (negatedPreferred.startsWith("toBe")) {
6-
doubleNegativeCases = [
7-
{
8-
code: `expect(element).not.${negatedPreferred}`,
9-
errors: [
10-
{
11-
message: `Use ${preferred} instead of not.${negatedPreferred}`,
12-
},
13-
],
14-
output: `expect(element).${preferred}`,
15-
},
16-
{
17-
code: `expect(element).not.${preferred}`,
18-
errors: [
19-
{
20-
message: `Use ${negatedPreferred} instead of not.${preferred}`,
21-
},
22-
],
23-
output: `expect(element).${negatedPreferred}`,
24-
},
25-
];
26-
}
27-
let directChecks = [];
28-
if (!/-/.test(attribute)) {
29-
directChecks = [
30-
{
31-
code: `expect(getByText('foo').${attribute}).toBeTruthy()`,
32-
errors: [
33-
{
34-
message: `Use ${preferred} instead of checking .${attribute} directly`,
35-
},
36-
],
37-
output: `expect(getByText('foo')).${[preferred]}`,
38-
},
39-
{
40-
code: `expect(getByText('foo').${attribute}).toBeFalsy()`,
41-
errors: [
42-
{
43-
message: `Use ${negatedPreferred} instead of checking .${attribute} directly`,
44-
},
45-
],
46-
output: `expect(getByText('foo')).${[negatedPreferred]}`,
47-
},
48-
{
49-
code: `expect(getByText('foo').${attribute}).toBe(true)`,
50-
errors: [
51-
{
52-
message: `Use ${preferred} instead of checking .${attribute} directly`,
53-
},
54-
],
55-
output: `expect(getByText('foo')).${[preferred]}`,
56-
},
57-
];
58-
}
3+
export default ({ preferred, negatedPreferred, attribute }) => {
4+
const doubleNegativeCases = negatedPreferred.startsWith("toBe")
5+
? [
6+
{
7+
code: `expect(element).not.${negatedPreferred}`,
8+
errors: [
9+
{
10+
message: `Use ${preferred} instead of not.${negatedPreferred}`,
11+
},
12+
],
13+
output: `expect(element).${preferred}`,
14+
},
15+
{
16+
code: `expect(element).not.${preferred}`,
17+
errors: [
18+
{
19+
message: `Use ${negatedPreferred} instead of not.${preferred}`,
20+
},
21+
],
22+
output: `expect(element).${negatedPreferred}`,
23+
},
24+
]
25+
: [];
26+
const directChecks = /-/.test(attribute)
27+
? []
28+
: [
29+
{
30+
code: `expect(getByText('foo').${attribute}).toBeTruthy()`,
31+
errors: [
32+
{
33+
message: `Use ${preferred} instead of checking .${attribute} directly`,
34+
},
35+
],
36+
output: `expect(getByText('foo')).${[preferred]}`,
37+
},
38+
{
39+
code: `expect(getByText('foo').${attribute}).toBeFalsy()`,
40+
errors: [
41+
{
42+
message: `Use ${negatedPreferred} instead of checking .${attribute} directly`,
43+
},
44+
],
45+
output: `expect(getByText('foo')).${[negatedPreferred]}`,
46+
},
47+
{
48+
code: `expect(getByText('foo').${attribute}).toBe(true)`,
49+
errors: [
50+
{
51+
message: `Use ${preferred} instead of checking .${attribute} directly`,
52+
},
53+
],
54+
output: `expect(getByText('foo')).${[preferred]}`,
55+
},
56+
];
57+
5958
return {
6059
valid: [
6160
`expect(element).not.toHaveProperty('value', 'foo')`,

src/__tests__/index.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { rules, generateRecommendedConfig } = require("../index");
1+
import { rules, generateRecommendedConfig } from "../index";
22

33
it("should have all the rules", () => {
44
expect(rules).toMatchSnapshot();

src/__tests__/lib/rules/no-attribute-checking.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* @author Ben Monro
44
*/
55

6-
const createBannedAttributeTestCases = require("../../__fixtures__/createBannedAttributeTestCases");
6+
import { RuleTester } from "eslint";
7+
import createBannedAttributeTestCases from "../../__fixtures__/createBannedAttributeTestCases";
78

89
const bannedAttributes = [
910
{
@@ -29,7 +30,6 @@ const bannedAttributes = [
2930
bannedAttributes.forEach(
3031
({ preferred, negatedPreferred, attributes, ruleName }) => {
3132
const rule = require(`../../../rules/${ruleName}`);
32-
const RuleTester = require("eslint").RuleTester;
3333

3434
// const preferred = 'toBeDisabled()';
3535
// const negatedPreferred = 'toBeEnabled()';

src/__tests__/lib/rules/prefer-empty.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// Requirements
88
//------------------------------------------------------------------------------
99

10-
const rule = require("../../../rules/prefer-empty");
11-
const RuleTester = require("eslint").RuleTester;
10+
import { RuleTester } from "eslint";
11+
import * as rule from "../../../rules/prefer-empty";
1212

1313
//------------------------------------------------------------------------------
1414
// Tests

src/__tests__/lib/rules/prefer-focus.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* @author Ben Monro
44
*/
55

6-
const rule = require("../../../rules/prefer-focus");
7-
const RuleTester = require("eslint").RuleTester;
6+
import { RuleTester } from "eslint";
7+
import * as rule from "../../../rules/prefer-focus";
88

99
const ruleTester = new RuleTester();
1010
ruleTester.run("prefer-focus", rule, {

src/__tests__/lib/rules/prefer-to-have-attribute.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// Requirements
88
//------------------------------------------------------------------------------
99

10-
const rule = require("../../../rules/prefer-to-have-attribute");
11-
const RuleTester = require("eslint").RuleTester;
10+
import { RuleTester } from "eslint";
11+
import * as rule from "../../../rules/prefer-to-have-attribute";
1212

1313
//------------------------------------------------------------------------------
1414
// Tests

src/__tests__/lib/rules/prefer-to-have-text-content.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// Requirements
88
//------------------------------------------------------------------------------
99

10-
const rule = require("../../../rules/prefer-to-have-text-content");
11-
const RuleTester = require("eslint").RuleTester;
10+
import { RuleTester } from "eslint";
11+
import * as rule from "../../../rules/prefer-to-have-text-content";
1212

1313
//------------------------------------------------------------------------------
1414
// Tests

src/createBannedAttributeRule.js

+78-79
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
module.exports = ({ preferred, negatedPreferred, attributes }) => (context) => {
2-
function getCorrectFunctionFor(node, negated = false) {
3-
return (node.arguments.length === 1 ||
1+
export default ({ preferred, negatedPreferred, attributes }) => (context) => {
2+
const getCorrectFunctionFor = (node, negated = false) =>
3+
(node.arguments.length === 1 ||
44
node.arguments[1].value === true ||
55
node.arguments[1].value === "") &&
6-
!negated
6+
!negated
77
? preferred
88
: negatedPreferred;
9-
}
109

1110
const isBannedArg = (node) =>
1211
attributes.some((attr) => attr === node.arguments[0].value);
@@ -15,100 +14,100 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => (context) => {
1514
[`CallExpression[callee.property.name=/${preferred}|${negatedPreferred}/][callee.object.property.name='not'][callee.object.object.callee.name='expect']`](
1615
node
1716
) {
18-
if (negatedPreferred.startsWith("toBe")) {
19-
const incorrectFunction = node.callee.property.name;
20-
21-
const correctFunction =
22-
incorrectFunction === preferred ? negatedPreferred : preferred;
23-
context.report({
24-
message: `Use ${correctFunction}() instead of not.${incorrectFunction}()`,
25-
node,
26-
fix(fixer) {
27-
return fixer.replaceTextRange(
28-
[node.callee.object.property.range[0], node.range[1]],
29-
`${correctFunction}()`
30-
);
31-
},
32-
});
17+
if (!negatedPreferred.startsWith("toBe")) {
18+
return;
3319
}
34-
},
3520

36-
"CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']"(
21+
const incorrectFunction = node.callee.property.name;
22+
23+
const correctFunction =
24+
incorrectFunction === preferred ? negatedPreferred : preferred;
25+
context.report({
26+
message: `Use ${correctFunction}() instead of not.${incorrectFunction}()`,
27+
node,
28+
fix: (fixer) =>
29+
fixer.replaceTextRange(
30+
[node.callee.object.property.range[0], node.range[1]],
31+
`${correctFunction}()`
32+
),
33+
});
34+
},
35+
[`CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']`](
3736
node
3837
) {
3938
const {
4039
arguments: [{ property, property: { name } = {} }],
4140
} = node.callee.object;
4241
const matcher = node.callee.property.name;
4342
const matcherArg = node.arguments.length && node.arguments[0].value;
44-
if (attributes.some((attr) => attr === name)) {
45-
const isNegated =
46-
matcher.endsWith("Falsy") ||
47-
((matcher === "toBe" || matcher === "toEqual") &&
48-
matcherArg !== true);
49-
const correctFunction = getCorrectFunctionFor(
50-
node.callee.object,
51-
isNegated
52-
);
53-
context.report({
54-
node,
55-
message: `Use ${correctFunction}() instead of checking .${name} directly`,
56-
fix(fixer) {
57-
return [
58-
fixer.removeRange([property.range[0] - 1, property.range[1]]),
59-
fixer.replaceTextRange(
60-
[node.callee.property.range[0], node.range[1]],
61-
`${correctFunction}()`
62-
),
63-
];
64-
},
65-
});
43+
if (!attributes.some((attr) => attr === name)) {
44+
return;
6645
}
46+
47+
const isNegated =
48+
matcher.endsWith("Falsy") ||
49+
((matcher === "toBe" || matcher === "toEqual") && matcherArg !== true);
50+
const correctFunction = getCorrectFunctionFor(
51+
node.callee.object,
52+
isNegated
53+
);
54+
context.report({
55+
node,
56+
message: `Use ${correctFunction}() instead of checking .${name} directly`,
57+
fix: (fixer) => [
58+
fixer.removeRange([property.range[0] - 1, property.range[1]]),
59+
fixer.replaceTextRange(
60+
[node.callee.property.range[0], node.range[1]],
61+
`${correctFunction}()`
62+
),
63+
],
64+
});
6765
},
68-
"CallExpression[callee.property.name=/toHaveProperty|toHaveAttribute/][callee.object.property.name='not'][callee.object.object.callee.name='expect']"(
66+
[`CallExpression[callee.property.name=/toHaveProperty|toHaveAttribute/][callee.object.property.name='not'][callee.object.object.callee.name='expect']`](
6967
node
7068
) {
7169
const arg = node.arguments[0].value;
72-
if (isBannedArg(node)) {
73-
const correctFunction = getCorrectFunctionFor(node, true);
74-
75-
const incorrectFunction = node.callee.property.name;
76-
context.report({
77-
message: `Use ${correctFunction}() instead of not.${incorrectFunction}('${arg}')`,
78-
node,
79-
fix(fixer) {
80-
return fixer.replaceTextRange(
81-
[node.callee.object.property.range[0], node.range[1]],
82-
`${correctFunction}()`
83-
);
84-
},
85-
});
70+
if (!isBannedArg(node)) {
71+
return;
8672
}
73+
74+
const correctFunction = getCorrectFunctionFor(node, true);
75+
76+
const incorrectFunction = node.callee.property.name;
77+
context.report({
78+
message: `Use ${correctFunction}() instead of not.${incorrectFunction}('${arg}')`,
79+
node,
80+
fix: (fixer) =>
81+
fixer.replaceTextRange(
82+
[node.callee.object.property.range[0], node.range[1]],
83+
`${correctFunction}()`
84+
),
85+
});
8786
},
88-
"CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]"(
87+
[`CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]`](
8988
node
9089
) {
91-
if (isBannedArg(node)) {
92-
const correctFunction = getCorrectFunctionFor(node);
90+
if (!isBannedArg(node)) {
91+
return;
92+
}
9393

94-
const incorrectFunction = node.callee.property.name;
94+
const correctFunction = getCorrectFunctionFor(node);
9595

96-
const message = `Use ${correctFunction}() instead of ${incorrectFunction}(${node.arguments
97-
.map(({ raw }) => raw)
98-
.join(", ")})`;
99-
context.report({
100-
node: node.callee.property,
101-
message,
102-
fix(fixer) {
103-
return [
104-
fixer.replaceTextRange(
105-
[node.callee.property.range[0], node.range[1]],
106-
`${correctFunction}()`
107-
),
108-
];
109-
},
110-
});
111-
}
96+
const incorrectFunction = node.callee.property.name;
97+
98+
const message = `Use ${correctFunction}() instead of ${incorrectFunction}(${node.arguments
99+
.map(({ raw }) => raw)
100+
.join(", ")})`;
101+
context.report({
102+
node: node.callee.property,
103+
message,
104+
fix: (fixer) => [
105+
fixer.replaceTextRange(
106+
[node.callee.property.range[0], node.range[1]],
107+
`${correctFunction}()`
108+
),
109+
],
110+
});
112111
},
113112
};
114113
};

0 commit comments

Comments
 (0)