Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(prefer-checked): don't auto-fix when 2nd argument is a non literal. #62

Merged
merged 5 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Thanks goes to these people ([emoji key][emojis]):

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down
1 change: 1 addition & 0 deletions docs/rules/prefer-checked.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Examples of **incorrect** code for this rule:
```js
expect(element).toHaveProperty("checked", true);
expect(element).toHaveAttribute("checked", false);
expect(element).toHaveProperty("checked", something);

expect(element).toHaveAttribute("checked");
expect(element).not.toHaveProperty("checked");
Expand Down
12 changes: 10 additions & 2 deletions src/__tests__/__fixtures__/createBannedAttributeTestCases.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ export default ({ preferred, negatedPreferred, attribute }) => {

return {
valid: [
`expect(element).not.toHaveProperty('value', 'foo')`,
"expect(element).not.toHaveProperty('value', 'foo')",
`expect(element).${preferred}`,
`expect(element).${negatedPreferred}`,
`expect(element).toHaveProperty('value', 'bar')`,
"expect(element).toHaveProperty('value', 'bar')",
],
invalid: [
...doubleNegativeCases,
Expand Down Expand Up @@ -173,6 +173,14 @@ export default ({ preferred, negatedPreferred, attribute }) => {
],
output: `expect(getByText("foo")).${negatedPreferred}`,
},
{
code: `expect(element).toHaveProperty('${attribute}', foo)`,
errors: [
{
message: `Use ${preferred} instead of toHaveProperty('${attribute}', foo)`,
},
],
},
],
};
};
31 changes: 21 additions & 10 deletions src/createBannedAttributeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
const getCorrectFunctionFor = (node, negated = false) =>
(node.arguments.length === 1 ||
node.arguments[1].value === true ||
node.arguments[1].type !== "Literal" ||
(typeof node.arguments[1].value === "string" &&
node.arguments[1].value.toLowerCase() === "true") ||
node.arguments[1].value === "") &&
Expand Down Expand Up @@ -34,7 +35,7 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
),
});
},
[`CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']`](
"CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']"(
node
) {
const {
Expand Down Expand Up @@ -65,7 +66,7 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
],
});
},
[`CallExpression[callee.property.name=/toHaveProperty|toHaveAttribute/][callee.object.property.name='not'][callee.object.object.callee.name='expect']`](
"CallExpression[callee.property.name=/toHaveProperty|toHaveAttribute/][callee.object.property.name='not'][callee.object.object.callee.name='expect']"(
node
) {
const arg = node.arguments[0].value;
Expand All @@ -86,7 +87,7 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
),
});
},
[`CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]`](
"CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]"(
node
) {
if (!isBannedArg(node)) {
Expand All @@ -98,17 +99,27 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
const incorrectFunction = node.callee.property.name;

const message = `Use ${correctFunction}() instead of ${incorrectFunction}(${node.arguments
.map(({ raw }) => raw)
.map(({ raw, name }) => raw || name)
.join(", ")})`;

const secondArgIsLiteral =
node.arguments.length === 2 && node.arguments[1].type === "Literal";

context.report({
node: node.callee.property,
message,
fix: (fixer) => [
fixer.replaceTextRange(
[node.callee.property.range[0], node.range[1]],
`${correctFunction}()`
),
],
fix: (fixer) => {
if (node.arguments.length === 1 || secondArgIsLiteral) {
return [
fixer.replaceTextRange(
[node.callee.property.range[0], node.range[1]],
`${correctFunction}()`
),
];
}

return null;
},
});
},
};
Expand Down