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-comparison-matcher): handle resolves and rejects modifiers correctly #1145

Merged
merged 1 commit into from Jun 6, 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
86 changes: 86 additions & 0 deletions src/rules/__tests__/prefer-comparison-matcher.test.ts
Expand Up @@ -40,6 +40,18 @@ const generateInvalidCases = (
},
],
},
{
code: `expect(value ${operator} 1).resolves.${equalityMatcher}(true);`,
output: `expect(value).resolves.${preferredMatcher}(1);`,
errors: [
{
messageId: 'useToBeComparison',
data: { preferredMatcher },
column: 27 + operator.length,
line: 1,
},
],
},
{
code: `expect(value ${operator} 1).${equalityMatcher}(false);`,
output: `expect(value).${preferredMatcherWhenNegated}(1);`,
Expand All @@ -64,6 +76,18 @@ const generateInvalidCases = (
},
],
},
{
code: `expect(value ${operator} 1).resolves.${equalityMatcher}(false);`,
output: `expect(value).resolves.${preferredMatcherWhenNegated}(1);`,
errors: [
{
messageId: 'useToBeComparison',
data: { preferredMatcher: preferredMatcherWhenNegated },
column: 27 + operator.length,
line: 1,
},
],
},
{
code: `expect(value ${operator} 1).not.${equalityMatcher}(true);`,
output: `expect(value).${preferredMatcherWhenNegated}(1);`,
Expand All @@ -88,6 +112,18 @@ const generateInvalidCases = (
},
],
},
{
code: `expect(value ${operator} 1).resolves.not.${equalityMatcher}(true);`,
output: `expect(value).resolves.${preferredMatcherWhenNegated}(1);`,
errors: [
{
messageId: 'useToBeComparison',
data: { preferredMatcher: preferredMatcherWhenNegated },
column: 27 + operator.length,
line: 1,
},
],
},
{
code: `expect(value ${operator} 1).not.${equalityMatcher}(false);`,
output: `expect(value).${preferredMatcher}(1);`,
Expand All @@ -100,6 +136,54 @@ const generateInvalidCases = (
},
],
},
{
code: `expect(value ${operator} 1).resolves.not.${equalityMatcher}(false);`,
output: `expect(value).resolves.${preferredMatcher}(1);`,
errors: [
{
messageId: 'useToBeComparison',
data: { preferredMatcher },
column: 27 + operator.length,
line: 1,
},
],
},
{
code: `expect(value ${operator} 1)["resolves"].not.${equalityMatcher}(false);`,
output: `expect(value).resolves.${preferredMatcher}(1);`,
errors: [
{
messageId: 'useToBeComparison',
data: { preferredMatcher },
column: 30 + operator.length,
line: 1,
},
],
},
{
code: `expect(value ${operator} 1)["resolves"]["not"].${equalityMatcher}(false);`,
output: `expect(value).resolves.${preferredMatcher}(1);`,
errors: [
{
messageId: 'useToBeComparison',
data: { preferredMatcher },
column: 30 + operator.length,
line: 1,
},
],
},
{
code: `expect(value ${operator} 1)["resolves"]["not"]['${equalityMatcher}'](false);`,
output: `expect(value).resolves.${preferredMatcher}(1);`,
errors: [
{
messageId: 'useToBeComparison',
data: { preferredMatcher },
column: 30 + operator.length,
line: 1,
},
],
},
];
};

Expand Down Expand Up @@ -128,6 +212,8 @@ const generateValidStringLiteralCases = (operator: string, matcher: string) => {
`expect(${b} ${operator} ${a}).not.${matcher}(true)`,
`expect(${b} ${operator} ${a}).not.${matcher}(false)`,
`expect(${b} ${operator} ${b}).not.${matcher}(false)`,
`expect(${b} ${operator} ${b}).resolves.not.${matcher}(false)`,
`expect(${b} ${operator} ${b}).resolves.${matcher}(false)`,
],
]);
};
Expand Down
19 changes: 16 additions & 3 deletions src/rules/prefer-comparison-matcher.ts
@@ -1,6 +1,7 @@
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
import {
MaybeTypeCast,
ModifierName,
ParsedEqualityMatcherCall,
ParsedExpectMatcher,
createRule,
Expand Down Expand Up @@ -122,9 +123,15 @@ export default createRule({
return;
}

const negation = modifier?.negation
? { node: modifier.negation }
: modifier?.name === ModifierName.not
? modifier
: null;

const preferredMatcher = determineMatcher(
comparison.operator,
followTypeAssertionChain(matcher.arguments[0]).value === !!modifier,
followTypeAssertionChain(matcher.arguments[0]).value === !!negation,
);

if (!preferredMatcher) {
Expand All @@ -135,6 +142,12 @@ export default createRule({
fix(fixer) {
const sourceCode = context.getSourceCode();

// preserve the existing modifier if it's not a negation
const modifierText =
modifier && modifier?.node !== negation?.node
? `.${modifier.name}`
: '';

return [
// replace the comparison argument with the left-hand side of the comparison
fixer.replaceText(
Expand All @@ -144,7 +157,7 @@ export default createRule({
// replace the current matcher & modifier with the preferred matcher
fixer.replaceTextRange(
[expectCallEnd, matcher.node.range[1]],
`.${preferredMatcher}`,
`${modifierText}.${preferredMatcher}`,
),
// replace the matcher argument with the right-hand side of the comparison
fixer.replaceText(
Expand All @@ -155,7 +168,7 @@ export default createRule({
},
messageId: 'useToBeComparison',
data: { preferredMatcher },
node: (modifier || matcher).node.property,
node: (negation || matcher).node.property,
});
},
};
Expand Down