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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions src/rules/__tests__/prefer-equality-matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ ruleTester.run('prefer-equality-matcher: ===', rule, {
},
],
},
{
code: 'expect(a === b).resolves.toBe(true);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a === b).resolves.toBe(false);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.not.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a === b).not.toBe(true);',
errors: [
Expand All @@ -89,6 +115,58 @@ ruleTester.run('prefer-equality-matcher: ===', rule, {
},
],
},
{
code: 'expect(a === b).resolves.not.toBe(true);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.not.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a === b).resolves.not.toBe(false);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a === b)["resolves"].not.toBe(false);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.${equalityMatcher}(b);`,
),
column: 29,
line: 1,
},
],
},
{
code: 'expect(a === b)["resolves"]["not"]["toBe"](false);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.${equalityMatcher}(b);`,
),
column: 29,
line: 1,
},
],
},
],
});

Expand Down Expand Up @@ -125,6 +203,32 @@ ruleTester.run('prefer-equality-matcher: !==', rule, {
},
],
},
{
code: 'expect(a !== b).resolves.toBe(true);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.not.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a !== b).resolves.toBe(false);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a !== b).not.toBe(true);',
errors: [
Expand All @@ -151,5 +255,31 @@ ruleTester.run('prefer-equality-matcher: !==', rule, {
},
],
},
{
code: 'expect(a !== b).resolves.not.toBe(true);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a !== b).resolves.not.toBe(false);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.not.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
],
});
24 changes: 19 additions & 5 deletions src/rules/prefer-equality-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,33 @@ export default createRule({
matcher.arguments[0],
).value;

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

// we need to negate the expectation if the current expected
// value is itself negated by the "not" modifier
const addNotModifier =
(comparison.operator === '!==' ? !matcherValue : matcherValue) ===
!!modifier;
!!negation;

const buildFixer =
(equalityMatcher: string): TSESLint.ReportFixFunction =>
fixer => {
const sourceCode = context.getSourceCode();

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

if (addNotModifier) {
modifierText += `.${ModifierName.not}`;
}

return [
// replace the comparison argument with the left-hand side of the comparison
fixer.replaceText(
Expand All @@ -105,9 +121,7 @@ export default createRule({
// replace the current matcher & modifier with the preferred matcher
fixer.replaceTextRange(
[expectCallEnd, matcher.node.range[1]],
addNotModifier
? `.${ModifierName.not}.${equalityMatcher}`
: `.${equalityMatcher}`,
`${modifierText}.${equalityMatcher}`,
),
// replace the matcher argument with the right-hand side of the comparison
fixer.replaceText(
Expand All @@ -126,7 +140,7 @@ export default createRule({
fix: buildFixer(equalityMatcher),
}),
),
node: (modifier || matcher).node.property,
node: (negation || matcher).node.property,
});
},
};
Expand Down