Skip to content

Commit

Permalink
fix(prefer-comparison-matcher): handle resolves and rejects modif…
Browse files Browse the repository at this point in the history
…iers correctly (#1145)
  • Loading branch information
G-Rath committed Jun 6, 2022
1 parent 7965a7c commit b1795ff
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 3 deletions.
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

0 comments on commit b1795ff

Please sign in to comment.