|
| 1 | +import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils'; |
| 2 | +import { getDocsUrl, TESTING_FRAMEWORK_SETUP_HOOKS } from '../utils'; |
| 3 | +import { |
| 4 | + isLiteral, |
| 5 | + isProperty, |
| 6 | + isIdentifier, |
| 7 | + isObjectPattern, |
| 8 | + isCallExpression, |
| 9 | + isRenderFunction, |
| 10 | + isImportSpecifier, |
| 11 | +} from '../node-utils'; |
| 12 | + |
| 13 | +export const RULE_NAME = 'no-render-in-setup'; |
| 14 | +export type MessageIds = 'noRenderInSetup'; |
| 15 | + |
| 16 | +export function findClosestBeforeHook( |
| 17 | + node: TSESTree.Node, |
| 18 | + testingFrameworkSetupHooksToFilter: string[] |
| 19 | +): TSESTree.Identifier | null { |
| 20 | + if (node === null) return null; |
| 21 | + if ( |
| 22 | + isCallExpression(node) && |
| 23 | + isIdentifier(node.callee) && |
| 24 | + testingFrameworkSetupHooksToFilter.includes(node.callee.name) |
| 25 | + ) { |
| 26 | + return node.callee; |
| 27 | + } |
| 28 | + |
| 29 | + return findClosestBeforeHook(node.parent, testingFrameworkSetupHooksToFilter); |
| 30 | +} |
| 31 | + |
| 32 | +export default ESLintUtils.RuleCreator(getDocsUrl)({ |
| 33 | + name: RULE_NAME, |
| 34 | + meta: { |
| 35 | + type: 'problem', |
| 36 | + docs: { |
| 37 | + description: 'Disallow the use of `render` in setup functions', |
| 38 | + category: 'Best Practices', |
| 39 | + recommended: false, |
| 40 | + }, |
| 41 | + messages: { |
| 42 | + noRenderInSetup: |
| 43 | + 'Move `render` out of `{{name}}` and into individual tests.', |
| 44 | + }, |
| 45 | + fixable: null, |
| 46 | + schema: [ |
| 47 | + { |
| 48 | + type: 'object', |
| 49 | + properties: { |
| 50 | + renderFunctions: { |
| 51 | + type: 'array', |
| 52 | + }, |
| 53 | + allowTestingFrameworkSetupHook: { |
| 54 | + enum: TESTING_FRAMEWORK_SETUP_HOOKS, |
| 55 | + }, |
| 56 | + }, |
| 57 | + anyOf: [ |
| 58 | + { |
| 59 | + required: ['renderFunctions'], |
| 60 | + }, |
| 61 | + { |
| 62 | + required: ['allowTestingFrameworkSetupHook'], |
| 63 | + }, |
| 64 | + ], |
| 65 | + }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + defaultOptions: [ |
| 69 | + { |
| 70 | + renderFunctions: [], |
| 71 | + allowTestingFrameworkSetupHook: '', |
| 72 | + }, |
| 73 | + ], |
| 74 | + |
| 75 | + create(context, [{ renderFunctions, allowTestingFrameworkSetupHook }]) { |
| 76 | + let renderImportedFromTestingLib = false; |
| 77 | + let wildcardImportName: string | null = null; |
| 78 | + |
| 79 | + return { |
| 80 | + // checks if import has shape: |
| 81 | + // import * as dtl from '@testing-library/dom'; |
| 82 | + 'ImportDeclaration[source.value=/testing-library/] ImportNamespaceSpecifier'( |
| 83 | + node: TSESTree.ImportNamespaceSpecifier |
| 84 | + ) { |
| 85 | + wildcardImportName = node.local && node.local.name; |
| 86 | + }, |
| 87 | + // checks if `render` is imported from a '@testing-library/foo' |
| 88 | + 'ImportDeclaration[source.value=/testing-library/]'( |
| 89 | + node: TSESTree.ImportDeclaration |
| 90 | + ) { |
| 91 | + renderImportedFromTestingLib = node.specifiers.some(specifier => { |
| 92 | + return ( |
| 93 | + isImportSpecifier(specifier) && specifier.local.name === 'render' |
| 94 | + ); |
| 95 | + }); |
| 96 | + }, |
| 97 | + [`VariableDeclarator > CallExpression > Identifier[name="require"]`]( |
| 98 | + node: TSESTree.Identifier |
| 99 | + ) { |
| 100 | + const { |
| 101 | + arguments: callExpressionArgs, |
| 102 | + } = node.parent as TSESTree.CallExpression; |
| 103 | + const testingLibImport = callExpressionArgs.find( |
| 104 | + args => |
| 105 | + isLiteral(args) && |
| 106 | + typeof args.value === 'string' && |
| 107 | + RegExp(/testing-library/, 'g').test(args.value) |
| 108 | + ); |
| 109 | + if (!testingLibImport) { |
| 110 | + return; |
| 111 | + } |
| 112 | + const declaratorNode = node.parent |
| 113 | + .parent as TSESTree.VariableDeclarator; |
| 114 | + |
| 115 | + renderImportedFromTestingLib = |
| 116 | + isObjectPattern(declaratorNode.id) && |
| 117 | + declaratorNode.id.properties.some( |
| 118 | + property => |
| 119 | + isProperty(property) && |
| 120 | + isIdentifier(property.key) && |
| 121 | + property.key.name === 'render' |
| 122 | + ); |
| 123 | + }, |
| 124 | + CallExpression(node) { |
| 125 | + let testingFrameworkSetupHooksToFilter = TESTING_FRAMEWORK_SETUP_HOOKS; |
| 126 | + if (allowTestingFrameworkSetupHook.length !== 0) { |
| 127 | + testingFrameworkSetupHooksToFilter = TESTING_FRAMEWORK_SETUP_HOOKS.filter( |
| 128 | + hook => hook !== allowTestingFrameworkSetupHook |
| 129 | + ); |
| 130 | + } |
| 131 | + const beforeHook = findClosestBeforeHook( |
| 132 | + node, |
| 133 | + testingFrameworkSetupHooksToFilter |
| 134 | + ); |
| 135 | + // if `render` is imported from a @testing-library/foo or |
| 136 | + // imported with a wildcard, add `render` to the list of |
| 137 | + // disallowed render functions |
| 138 | + const disallowedRenderFns = |
| 139 | + renderImportedFromTestingLib || wildcardImportName |
| 140 | + ? ['render', ...renderFunctions] |
| 141 | + : renderFunctions; |
| 142 | + |
| 143 | + if (isRenderFunction(node, disallowedRenderFns) && beforeHook) { |
| 144 | + context.report({ |
| 145 | + node, |
| 146 | + messageId: 'noRenderInSetup', |
| 147 | + data: { |
| 148 | + name: beforeHook.name, |
| 149 | + }, |
| 150 | + }); |
| 151 | + } |
| 152 | + }, |
| 153 | + }; |
| 154 | + }, |
| 155 | +}); |
0 commit comments