|
| 1 | +import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils'; |
| 2 | +import { getDocsUrl, ASYNC_UTILS, LIBRARY_MODULES } from '../utils'; |
| 3 | +import { |
| 4 | + findClosestCallExpressionNode, |
| 5 | + isMemberExpression, |
| 6 | +} from '../node-utils'; |
| 7 | + |
| 8 | +export const RULE_NAME = 'no-wait-for-snapshot'; |
| 9 | +export type MessageIds = 'noWaitForSnapshot'; |
| 10 | +type Options = []; |
| 11 | + |
| 12 | +const ASYNC_UTILS_REGEXP = new RegExp(`^(${ASYNC_UTILS.join('|')})$`); |
| 13 | +const SNAPSHOT_REGEXP = /^(toMatchSnapshot|toMatchInlineSnapshot)$/; |
| 14 | + |
| 15 | +export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({ |
| 16 | + name: RULE_NAME, |
| 17 | + meta: { |
| 18 | + type: 'problem', |
| 19 | + docs: { |
| 20 | + description: |
| 21 | + 'Ensures no snapshot is generated inside of a `waitFor` call', |
| 22 | + category: 'Best Practices', |
| 23 | + recommended: 'warn', |
| 24 | + }, |
| 25 | + messages: { |
| 26 | + noWaitForSnapshot: |
| 27 | + "A snapshot can't be generated inside of a `{{ name }}` call", |
| 28 | + }, |
| 29 | + fixable: null, |
| 30 | + schema: [], |
| 31 | + }, |
| 32 | + defaultOptions: [], |
| 33 | + |
| 34 | + create(context) { |
| 35 | + const asyncUtilsUsage: Array<{ |
| 36 | + node: TSESTree.Identifier | TSESTree.MemberExpression; |
| 37 | + name: string; |
| 38 | + }> = []; |
| 39 | + const importedAsyncUtils: string[] = []; |
| 40 | + const snapshotUsage: TSESTree.Identifier[] = []; |
| 41 | + |
| 42 | + return { |
| 43 | + 'ImportDeclaration > ImportSpecifier,ImportNamespaceSpecifier'( |
| 44 | + node: TSESTree.Node |
| 45 | + ) { |
| 46 | + const parent = node.parent as TSESTree.ImportDeclaration; |
| 47 | + |
| 48 | + if (!LIBRARY_MODULES.includes(parent.source.value.toString())) return; |
| 49 | + |
| 50 | + let name; |
| 51 | + if (node.type === 'ImportSpecifier') { |
| 52 | + name = node.imported.name; |
| 53 | + } |
| 54 | + |
| 55 | + if (node.type === 'ImportNamespaceSpecifier') { |
| 56 | + name = node.local.name; |
| 57 | + } |
| 58 | + |
| 59 | + importedAsyncUtils.push(name); |
| 60 | + }, |
| 61 | + [`CallExpression > Identifier[name=${ASYNC_UTILS_REGEXP}]`]( |
| 62 | + node: TSESTree.Identifier |
| 63 | + ) { |
| 64 | + asyncUtilsUsage.push({ node, name: node.name }); |
| 65 | + }, |
| 66 | + [`CallExpression > MemberExpression > Identifier[name=${ASYNC_UTILS_REGEXP}]`]( |
| 67 | + node: TSESTree.Identifier |
| 68 | + ) { |
| 69 | + const memberExpression = node.parent as TSESTree.MemberExpression; |
| 70 | + const identifier = memberExpression.object as TSESTree.Identifier; |
| 71 | + const memberExpressionName = identifier.name; |
| 72 | + |
| 73 | + asyncUtilsUsage.push({ |
| 74 | + node: memberExpression, |
| 75 | + name: memberExpressionName, |
| 76 | + }); |
| 77 | + }, |
| 78 | + [`Identifier[name=${SNAPSHOT_REGEXP}]`](node: TSESTree.Identifier) { |
| 79 | + snapshotUsage.push(node); |
| 80 | + }, |
| 81 | + 'Program:exit'() { |
| 82 | + const testingLibraryUtilUsage = asyncUtilsUsage.filter(usage => { |
| 83 | + if (isMemberExpression(usage.node)) { |
| 84 | + const object = usage.node.object as TSESTree.Identifier; |
| 85 | + |
| 86 | + return importedAsyncUtils.includes(object.name); |
| 87 | + } |
| 88 | + |
| 89 | + return importedAsyncUtils.includes(usage.name); |
| 90 | + }); |
| 91 | + |
| 92 | + function getClosestAsyncUtil( |
| 93 | + asyncUtilUsage: { |
| 94 | + node: TSESTree.Identifier | TSESTree.MemberExpression; |
| 95 | + name: string; |
| 96 | + }, |
| 97 | + node: TSESTree.Node |
| 98 | + ) { |
| 99 | + let callExpression = findClosestCallExpressionNode(node); |
| 100 | + while (callExpression != null) { |
| 101 | + if (callExpression.callee === asyncUtilUsage.node) |
| 102 | + return asyncUtilUsage; |
| 103 | + callExpression = findClosestCallExpressionNode( |
| 104 | + callExpression.parent |
| 105 | + ); |
| 106 | + } |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + snapshotUsage.forEach(node => { |
| 111 | + testingLibraryUtilUsage.forEach(asyncUtilUsage => { |
| 112 | + const closestAsyncUtil = getClosestAsyncUtil(asyncUtilUsage, node); |
| 113 | + if (closestAsyncUtil != null) { |
| 114 | + let name; |
| 115 | + if (isMemberExpression(closestAsyncUtil.node)) { |
| 116 | + name = (closestAsyncUtil.node.property as TSESTree.Identifier) |
| 117 | + .name; |
| 118 | + } else { |
| 119 | + name = closestAsyncUtil.name; |
| 120 | + } |
| 121 | + context.report({ |
| 122 | + node, |
| 123 | + messageId: 'noWaitForSnapshot', |
| 124 | + data: { name }, |
| 125 | + }); |
| 126 | + } |
| 127 | + }); |
| 128 | + }); |
| 129 | + }, |
| 130 | + }; |
| 131 | + }, |
| 132 | +}); |
0 commit comments