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(no-restricted-matchers): allow restricting negated resolves and rejects modifiers #1142

Merged
merged 1 commit into from Jun 4, 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
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