Skip to content

Commit

Permalink
fix(no-restricted-matchers): allow restricting negated resolves and…
Browse files Browse the repository at this point in the history
… `rejects` modifiers (#1142)
  • Loading branch information
G-Rath committed Jun 4, 2022
1 parent 7c5e662 commit 0950a96
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 44 deletions.
76 changes: 76 additions & 0 deletions src/rules/__tests__/no-restricted-matchers.test.ts
Expand Up @@ -99,6 +99,66 @@ ruleTester.run('no-restricted-matchers', rule, {
},
],
},
{
code: 'expect(a).resolves.toBe(b)',
options: [{ resolves: null }],
errors: [
{
messageId: 'restrictedChain',
data: {
message: null,
chain: 'resolves',
},
column: 11,
line: 1,
},
],
},
{
code: 'expect(a).resolves.not.toBe(b)',
options: [{ not: null }],
errors: [
{
messageId: 'restrictedChain',
data: {
message: null,
chain: 'not',
},
column: 20,
line: 1,
},
],
},
{
code: 'expect(a).resolves.not.toBe(b)',
options: [{ resolves: null }],
errors: [
{
messageId: 'restrictedChain',
data: {
message: null,
chain: 'resolves',
},
column: 11,
line: 1,
},
],
},
{
code: 'expect(a).resolves.not.toBe(b)',
options: [{ 'resolves.not': null }],
errors: [
{
messageId: 'restrictedChain',
data: {
message: null,
chain: 'resolves.not',
},
column: 11,
line: 1,
},
],
},
{
code: 'expect(a).not.toBe(b)',
options: [{ 'not.toBe': null }],
Expand All @@ -115,6 +175,22 @@ ruleTester.run('no-restricted-matchers', rule, {
},
],
},
{
code: 'expect(a).resolves.not.toBe(b)',
options: [{ 'resolves.not.toBe': null }],
errors: [
{
messageId: 'restrictedChain',
data: {
message: null,
chain: 'resolves.not.toBe',
},
endColumn: 28,
column: 11,
line: 1,
},
],
},
{
code: 'expect(a).toBe(b)',
options: [{ toBe: 'Prefer `toStrictEqual` instead' }],
Expand Down
97 changes: 53 additions & 44 deletions src/rules/no-restricted-matchers.ts
@@ -1,3 +1,4 @@
import { TSESTree } from '@typescript-eslint/utils';
import { createRule, isExpectCall, parseExpectCall } from './utils';

export default createRule<
Expand Down Expand Up @@ -27,6 +28,25 @@ export default createRule<
},
defaultOptions: [{}],
create(context, [restrictedChains]) {
const reportIfRestricted = (
loc: TSESTree.SourceLocation,
chain: string,
): boolean => {
if (!(chain in restrictedChains)) {
return false;
}

const message = restrictedChains[chain];

context.report({
messageId: message ? 'restrictedChainWithMessage' : 'restrictedChain',
data: { message, chain },
loc,
});

return true;
};

return {
CallExpression(node) {
if (!isExpectCall(node)) {
Expand All @@ -35,61 +55,50 @@ export default createRule<

const { matcher, modifier } = parseExpectCall(node);

if (matcher) {
const chain = matcher.name;

if (chain in restrictedChains) {
const message = restrictedChains[chain];

context.report({
messageId: message
? 'restrictedChainWithMessage'
: 'restrictedChain',
data: { message, chain },
node: matcher.node.property,
});

return;
}
if (
matcher &&
reportIfRestricted(matcher.node.property.loc, matcher.name)
) {
return;
}

if (modifier) {
const chain = modifier.name;

if (chain in restrictedChains) {
const message = restrictedChains[chain];

context.report({
messageId: message
? 'restrictedChainWithMessage'
: 'restrictedChain',
data: { message, chain },
node: modifier.node.property,
});

if (reportIfRestricted(modifier.node.property.loc, modifier.name)) {
return;
}

if (modifier.negation) {
if (
reportIfRestricted(modifier.negation.property.loc, 'not') ||
reportIfRestricted(
{
start: modifier.node.property.loc.start,
end: modifier.negation.property.loc.end,
},
`${modifier.name}.not`,
)
) {
return;
}
}
}

if (matcher && modifier) {
const chain = `${modifier.name}.${matcher.name}`;
let chain: string = modifier.name;

if (chain in restrictedChains) {
const message = restrictedChains[chain];
if (modifier.negation) {
chain += '.not';
}

context.report({
messageId: message
? 'restrictedChainWithMessage'
: 'restrictedChain',
data: { message, chain },
loc: {
start: modifier.node.property.loc.start,
end: matcher.node.property.loc.end,
},
});
chain += `.${matcher.name}`;

return;
}
reportIfRestricted(
{
start: modifier.node.property.loc.start,
end: matcher.node.property.loc.end,
},
chain,
);
}
},
};
Expand Down

0 comments on commit 0950a96

Please sign in to comment.