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-in-document): handle toHaveLength without any arguments and with trailing commas #276

Merged
merged 3 commits into from Nov 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/__tests__/lib/rules/prefer-in-document.js
Expand Up @@ -51,6 +51,10 @@ const valid = [
expect(foo).not.toHaveLength(0)`,
`let foo;
expect(foo).toHaveLength(1);`,
`let foo;
expect(foo).toHaveLength()`,
`let foo;
expect(foo).toHaveLength(1, 2, 3)`,
`expect(screen.notAQuery('foo-bar')).toHaveLength(1)`,
`expect(screen.getAllByText('foo-bar')).toHaveLength(2)`,
`import foo from "./foo";
Expand Down Expand Up @@ -99,6 +103,82 @@ const valid = [
expect(element).toBeInTheDocument`,
];
const invalid = [
invalidCase(
`expect(screen.getByText('foo')).toHaveLength()`,
`expect(screen.getByText('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(screen.getAllByText('foo')).toHaveLength()`,
`expect(screen.getByText('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(screen.getByRole('foo')).toHaveLength()`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength()`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(screen.getByRole('foo')).toHaveLength(0,2,3)`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(0,2,3,)`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(screen.getByRole('foo')).toHaveLength(1,2,3)`,
`expect(screen.getByRole('foo')).toBeInTheDocument()`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(1,2,3,)`,
`expect(screen.getByRole('foo')).toBeInTheDocument()`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(0//comment
)`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument(//comment
)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(1,//comment
)`,
`expect(screen.getByRole('foo')).toBeInTheDocument(//comment
)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(0,2,3//comment
)`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument(//comment
)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(1,2,3,//comment
)`,
`expect(screen.getByRole('foo')).toBeInTheDocument(//comment
)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(0,2,//comment
3,4)`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument(//comment
)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(1,2,//comment
3,4,)`,
`expect(screen.getByRole('foo')).toBeInTheDocument(//comment
)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(0,2/*comment*/,3)`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument(/*comment*/)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(1,2,/*comment*/3,)`,
`expect(screen.getByRole('foo')).toBeInTheDocument(/*comment*/)`
),
invalidCase(
`expect(screen.getByText('foo')).toHaveLength(1)`,
`expect(screen.getByText('foo')).toBeInTheDocument()`
Expand Down
13 changes: 12 additions & 1 deletion src/rules/prefer-in-document.js
Expand Up @@ -40,7 +40,12 @@ function usesToBeOrToEqualWithNull(matcherNode, matcherArguments) {
}

function usesToHaveLengthZero(matcherNode, matcherArguments) {
return matcherNode.name === "toHaveLength" && matcherArguments[0].value === 0;
// matcherArguments.length === 0: toHaveLength() will cause jest matcher error
// matcherArguments[0].value: toHaveLength(0, ...) means zero length
return (
matcherNode.name === "toHaveLength" &&
(matcherArguments.length === 0 || matcherArguments[0].value === 0)
);
}

export const create = (context) => {
Expand Down Expand Up @@ -117,6 +122,12 @@ export const create = (context) => {

// Remove any arguments in the matcher
for (const argument of Array.from(matcherArguments)) {
const sourceCode = context.getSourceCode();
const token = sourceCode.getTokenAfter(argument);
if (token.value === "," && token.type === "Punctuator") {
// Remove commas if toHaveLength had more than one argument or a trailing comma
operations.push(fixer.replaceText(token, ""));
}
operations.push(fixer.remove(argument));
}
Comment on lines 124 to 132
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better fix for this should be to use the starting location of the first argument and then the ending location of the last argument - that way we don't have to actually care about the commas at all.

Copy link
Contributor Author

@berkinanik berkinanik Nov 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might me right, I thought about it also (couldn't tried since I don't know the working mechanism of the eslint very well),
but I felt like comments would create problems if we handled it that way

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problems with comments are not something we can really avoid - I'm happy to pick up getting the PR landed if you've not got the bandwidth :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would be great, thanks 🙂

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok so I was thinking about the handing for another situation (ESLint fixers are a bit mystic because of all the different ways code can be shaped 😂) - what you've got is good; it also turns out we're not handling trailing commas properly in eslint-plugin-jest either so thanks for being this to my attention!


Expand Down