Skip to content

Commit 564178b

Browse files
authored
fix(prefer-checked): don't auto-fix when 2nd argument is a non literal. (#62)
* fix:(prefer-checked): don't fix when 2nd arg is non literal) * turned off babel/quotes, that' sprettiers job * added docs
1 parent 8497c48 commit 564178b

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ Thanks goes to these people ([emoji key][emojis]):
151151

152152
<!-- markdownlint-enable -->
153153
<!-- prettier-ignore-end -->
154+
154155
<!-- ALL-CONTRIBUTORS-LIST:END -->
155156

156157
This project follows the [all-contributors][all-contributors] specification.

docs/rules/prefer-checked.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Examples of **incorrect** code for this rule:
4747
```js
4848
expect(element).toHaveProperty("checked", true);
4949
expect(element).toHaveAttribute("checked", false);
50+
expect(element).toHaveProperty("checked", something);
5051

5152
expect(element).toHaveAttribute("checked");
5253
expect(element).not.toHaveProperty("checked");

src/__tests__/__fixtures__/createBannedAttributeTestCases.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ export default ({ preferred, negatedPreferred, attribute }) => {
5757

5858
return {
5959
valid: [
60-
`expect(element).not.toHaveProperty('value', 'foo')`,
60+
"expect(element).not.toHaveProperty('value', 'foo')",
6161
`expect(element).${preferred}`,
6262
`expect(element).${negatedPreferred}`,
63-
`expect(element).toHaveProperty('value', 'bar')`,
63+
"expect(element).toHaveProperty('value', 'bar')",
6464
],
6565
invalid: [
6666
...doubleNegativeCases,
@@ -173,6 +173,14 @@ export default ({ preferred, negatedPreferred, attribute }) => {
173173
],
174174
output: `expect(getByText("foo")).${negatedPreferred}`,
175175
},
176+
{
177+
code: `expect(element).toHaveProperty('${attribute}', foo)`,
178+
errors: [
179+
{
180+
message: `Use ${preferred} instead of toHaveProperty('${attribute}', foo)`,
181+
},
182+
],
183+
},
176184
],
177185
};
178186
};

src/createBannedAttributeRule.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
22
const getCorrectFunctionFor = (node, negated = false) =>
33
(node.arguments.length === 1 ||
44
node.arguments[1].value === true ||
5+
node.arguments[1].type !== "Literal" ||
56
(typeof node.arguments[1].value === "string" &&
67
node.arguments[1].value.toLowerCase() === "true") ||
78
node.arguments[1].value === "") &&
@@ -34,7 +35,7 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
3435
),
3536
});
3637
},
37-
[`CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']`](
38+
"CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']"(
3839
node
3940
) {
4041
const {
@@ -65,7 +66,7 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
6566
],
6667
});
6768
},
68-
[`CallExpression[callee.property.name=/toHaveProperty|toHaveAttribute/][callee.object.property.name='not'][callee.object.object.callee.name='expect']`](
69+
"CallExpression[callee.property.name=/toHaveProperty|toHaveAttribute/][callee.object.property.name='not'][callee.object.object.callee.name='expect']"(
6970
node
7071
) {
7172
const arg = node.arguments[0].value;
@@ -86,7 +87,7 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
8687
),
8788
});
8889
},
89-
[`CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]`](
90+
"CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]"(
9091
node
9192
) {
9293
if (!isBannedArg(node)) {
@@ -98,17 +99,27 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
9899
const incorrectFunction = node.callee.property.name;
99100

100101
const message = `Use ${correctFunction}() instead of ${incorrectFunction}(${node.arguments
101-
.map(({ raw }) => raw)
102+
.map(({ raw, name }) => raw || name)
102103
.join(", ")})`;
104+
105+
const secondArgIsLiteral =
106+
node.arguments.length === 2 && node.arguments[1].type === "Literal";
107+
103108
context.report({
104109
node: node.callee.property,
105110
message,
106-
fix: (fixer) => [
107-
fixer.replaceTextRange(
108-
[node.callee.property.range[0], node.range[1]],
109-
`${correctFunction}()`
110-
),
111-
],
111+
fix: (fixer) => {
112+
if (node.arguments.length === 1 || secondArgIsLiteral) {
113+
return [
114+
fixer.replaceTextRange(
115+
[node.callee.property.range[0], node.range[1]],
116+
`${correctFunction}()`
117+
),
118+
];
119+
}
120+
121+
return null;
122+
},
112123
});
113124
},
114125
};

0 commit comments

Comments
 (0)