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

refactor: migrate no-wait-for-snapshot to v4 #271

Merged
merged 2 commits into from
Dec 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
128 changes: 33 additions & 95 deletions lib/rules/no-wait-for-snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { getDocsUrl, ASYNC_UTILS, LIBRARY_MODULES } from '../utils';
import { ASTUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import { ASYNC_UTILS } from '../utils';
import {
findClosestCallExpressionNode,
isMemberExpression,
Expand All @@ -9,10 +10,9 @@ export const RULE_NAME = 'no-wait-for-snapshot';
export type MessageIds = 'noWaitForSnapshot';
type Options = [];

const ASYNC_UTILS_REGEXP = new RegExp(`^(${ASYNC_UTILS.join('|')})$`);
const SNAPSHOT_REGEXP = /^(toMatchSnapshot|toMatchInlineSnapshot)$/;

export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
Expand All @@ -31,102 +31,40 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
},
defaultOptions: [],

create(context) {
const asyncUtilsUsage: Array<{
node: TSESTree.Identifier | TSESTree.MemberExpression;
name: string;
}> = [];
const importedAsyncUtils: string[] = [];
const snapshotUsage: TSESTree.Identifier[] = [];

return {
'ImportDeclaration > ImportSpecifier,ImportNamespaceSpecifier'(
node: TSESTree.Node
) {
const parent = node.parent as TSESTree.ImportDeclaration;

if (!LIBRARY_MODULES.includes(parent.source.value.toString())) {
return;
}

let name;
if (node.type === 'ImportSpecifier') {
name = node.imported.name;
create(context, _, helpers) {
function getClosestAsyncUtil(node: TSESTree.Node) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this could be simplified or solved with one of the node-utils utilities. Anyway, I just kept the function from the original function (though I changed it to a do/while)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the method is fine here for now, it seems really specific. However, there is one thing which can be reused from detection helpers: ASYNC_UTILS.includes(callExpression.callee.name) should be replaced by helpers.isAsyncUtil(callExpression.callee)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solved in e0bf288

let n = node;
do {
const callExpression = findClosestCallExpressionNode(n);
if (
ASTUtils.isIdentifier(callExpression.callee) &&
helpers.isNodeComingFromTestingLibrary(callExpression.callee) &&
ASYNC_UTILS.includes(callExpression.callee.name)
) {
return callExpression.callee;
}

if (node.type === 'ImportNamespaceSpecifier') {
name = node.local.name;
if (
isMemberExpression(callExpression.callee) &&
ASTUtils.isIdentifier(callExpression.callee.property) &&
helpers.isNodeComingFromTestingLibrary(callExpression.callee)
) {
return callExpression.callee.property;
}
n = findClosestCallExpressionNode(callExpression.parent);
} while (n !== null);
return null;
}

importedAsyncUtils.push(name);
},
[`CallExpression > Identifier[name=${ASYNC_UTILS_REGEXP}]`](
node: TSESTree.Identifier
) {
asyncUtilsUsage.push({ node, name: node.name });
},
[`CallExpression > MemberExpression > Identifier[name=${ASYNC_UTILS_REGEXP}]`](
node: TSESTree.Identifier
) {
const memberExpression = node.parent as TSESTree.MemberExpression;
const identifier = memberExpression.object as TSESTree.Identifier;
const memberExpressionName = identifier.name;

asyncUtilsUsage.push({
node: memberExpression,
name: memberExpressionName,
});
},
return {
[`Identifier[name=${SNAPSHOT_REGEXP}]`](node: TSESTree.Identifier) {
snapshotUsage.push(node);
},
'Program:exit'() {
const testingLibraryUtilUsage = asyncUtilsUsage.filter((usage) => {
if (isMemberExpression(usage.node)) {
const object = usage.node.object as TSESTree.Identifier;

return importedAsyncUtils.includes(object.name);
}

return importedAsyncUtils.includes(usage.name);
});

function getClosestAsyncUtil(
asyncUtilUsage: {
node: TSESTree.Identifier | TSESTree.MemberExpression;
name: string;
},
node: TSESTree.Node
) {
let callExpression = findClosestCallExpressionNode(node);
while (callExpression != null) {
if (callExpression.callee === asyncUtilUsage.node)
return asyncUtilUsage;
callExpression = findClosestCallExpressionNode(
callExpression.parent
);
}
return null;
const closestAsyncUtil = getClosestAsyncUtil(node);
if (closestAsyncUtil === null) {
return;
}

snapshotUsage.forEach((node) => {
testingLibraryUtilUsage.forEach((asyncUtilUsage) => {
const closestAsyncUtil = getClosestAsyncUtil(asyncUtilUsage, node);
if (closestAsyncUtil != null) {
let name;
if (isMemberExpression(closestAsyncUtil.node)) {
name = (closestAsyncUtil.node.property as TSESTree.Identifier)
.name;
} else {
name = closestAsyncUtil.name;
}
context.report({
node,
messageId: 'noWaitForSnapshot',
data: { name },
});
}
});
context.report({
node,
messageId: 'noWaitForSnapshot',
data: { name: closestAsyncUtil.name },
});
},
};
Expand Down
72 changes: 64 additions & 8 deletions tests/lib/rules/no-wait-for-snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,14 @@ ruleTester.run(RULE_NAME, rule, {
await ${asyncUtil}(() => expect(foo).toMatchSnapshot());
});
`,
errors: [{ line: 4, messageId: 'noWaitForSnapshot' }],
errors: [
{
line: 4,
messageId: 'noWaitForSnapshot',
data: { name: asyncUtil },
column: 36 + asyncUtil.length,
},
],
})),
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
Expand All @@ -142,7 +149,14 @@ ruleTester.run(RULE_NAME, rule, {
});
});
`,
errors: [{ line: 5, messageId: 'noWaitForSnapshot' }],
errors: [
{
line: 5,
messageId: 'noWaitForSnapshot',
data: { name: asyncUtil },
column: 27,
},
],
})),
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
Expand All @@ -151,7 +165,14 @@ ruleTester.run(RULE_NAME, rule, {
await asyncUtils.${asyncUtil}(() => expect(foo).toMatchSnapshot());
});
`,
errors: [{ line: 4, messageId: 'noWaitForSnapshot' }],
errors: [
{
line: 4,
messageId: 'noWaitForSnapshot',
data: { name: asyncUtil },
column: 47 + asyncUtil.length,
},
],
})),
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
Expand All @@ -162,7 +183,14 @@ ruleTester.run(RULE_NAME, rule, {
});
});
`,
errors: [{ line: 5, messageId: 'noWaitForSnapshot' }],
errors: [
{
line: 5,
messageId: 'noWaitForSnapshot',
data: { name: asyncUtil },
column: 27,
},
],
})),
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
Expand All @@ -171,7 +199,14 @@ ruleTester.run(RULE_NAME, rule, {
await ${asyncUtil}(() => expect(foo).toMatchInlineSnapshot());
});
`,
errors: [{ line: 4, messageId: 'noWaitForSnapshot' }],
errors: [
{
line: 4,
messageId: 'noWaitForSnapshot',
data: { name: asyncUtil },
column: 36 + asyncUtil.length,
},
],
})),
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
Expand All @@ -182,7 +217,14 @@ ruleTester.run(RULE_NAME, rule, {
});
});
`,
errors: [{ line: 5, messageId: 'noWaitForSnapshot' }],
errors: [
{
line: 5,
messageId: 'noWaitForSnapshot',
data: { name: asyncUtil },
column: 27,
},
],
})),
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
Expand All @@ -191,7 +233,14 @@ ruleTester.run(RULE_NAME, rule, {
await asyncUtils.${asyncUtil}(() => expect(foo).toMatchInlineSnapshot());
});
`,
errors: [{ line: 4, messageId: 'noWaitForSnapshot' }],
errors: [
{
line: 4,
messageId: 'noWaitForSnapshot',
data: { name: asyncUtil },
column: 47 + asyncUtil.length,
},
],
})),
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
Expand All @@ -202,7 +251,14 @@ ruleTester.run(RULE_NAME, rule, {
});
});
`,
errors: [{ line: 5, messageId: 'noWaitForSnapshot' }],
errors: [
{
line: 5,
messageId: 'noWaitForSnapshot',
data: { name: asyncUtil },
column: 27,
},
],
})),
],
});