|
| 1 | +import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils'; |
| 2 | +import { getDocsUrl } from '../utils'; |
| 3 | +import { |
| 4 | + isIdentifier, |
| 5 | + isMemberExpression, |
| 6 | + isObjectPattern, |
| 7 | + isProperty, |
| 8 | + isRenderVariableDeclarator, |
| 9 | +} from '../node-utils'; |
| 10 | + |
| 11 | +export const RULE_NAME = 'no-container'; |
| 12 | + |
| 13 | +export default ESLintUtils.RuleCreator(getDocsUrl)({ |
| 14 | + name: RULE_NAME, |
| 15 | + meta: { |
| 16 | + type: 'problem', |
| 17 | + docs: { |
| 18 | + description: 'Disallow the use of container methods', |
| 19 | + category: 'Best Practices', |
| 20 | + recommended: 'error', |
| 21 | + }, |
| 22 | + messages: { |
| 23 | + noContainer: |
| 24 | + 'Avoid using container methods. Prefer using the methods from Testing Library, such as "getByRole()"', |
| 25 | + }, |
| 26 | + fixable: null, |
| 27 | + schema: [ |
| 28 | + { |
| 29 | + type: 'object', |
| 30 | + properties: { |
| 31 | + renderFunctions: { |
| 32 | + type: 'array', |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + ], |
| 37 | + }, |
| 38 | + defaultOptions: [ |
| 39 | + { |
| 40 | + renderFunctions: [], |
| 41 | + }, |
| 42 | + ], |
| 43 | + |
| 44 | + create(context, [options]) { |
| 45 | + const { renderFunctions } = options; |
| 46 | + const destructuredContainerPropNames: string[] = []; |
| 47 | + let renderWrapperName: string = null; |
| 48 | + let containerName: string = null; |
| 49 | + let containerCallsMethod = false; |
| 50 | + |
| 51 | + function showErrorIfChainedContainerMethod( |
| 52 | + innerNode: TSESTree.MemberExpression |
| 53 | + ) { |
| 54 | + if (isMemberExpression(innerNode)) { |
| 55 | + if (isIdentifier(innerNode.object)) { |
| 56 | + const isContainerName = innerNode.object.name === containerName; |
| 57 | + const isRenderWrapper = innerNode.object.name === renderWrapperName; |
| 58 | + |
| 59 | + containerCallsMethod = |
| 60 | + isIdentifier(innerNode.property) && |
| 61 | + innerNode.property.name === 'container' && |
| 62 | + isRenderWrapper; |
| 63 | + |
| 64 | + if (isContainerName || containerCallsMethod) { |
| 65 | + context.report({ |
| 66 | + node: innerNode, |
| 67 | + messageId: 'noContainer', |
| 68 | + }); |
| 69 | + } |
| 70 | + } |
| 71 | + showErrorIfChainedContainerMethod( |
| 72 | + innerNode.object as TSESTree.MemberExpression |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return { |
| 78 | + VariableDeclarator(node) { |
| 79 | + if (isRenderVariableDeclarator(node, renderFunctions)) { |
| 80 | + if (isObjectPattern(node.id)) { |
| 81 | + const containerIndex = node.id.properties.findIndex( |
| 82 | + property => |
| 83 | + isProperty(property) && |
| 84 | + isIdentifier(property.key) && |
| 85 | + property.key.name === 'container' |
| 86 | + ); |
| 87 | + const nodeValue = |
| 88 | + containerIndex !== -1 && node.id.properties[containerIndex].value; |
| 89 | + if (isIdentifier(nodeValue)) { |
| 90 | + containerName = nodeValue.name; |
| 91 | + } else { |
| 92 | + isObjectPattern(nodeValue) && |
| 93 | + nodeValue.properties.forEach( |
| 94 | + property => |
| 95 | + isProperty(property) && |
| 96 | + isIdentifier(property.key) && |
| 97 | + destructuredContainerPropNames.push(property.key.name) |
| 98 | + ); |
| 99 | + } |
| 100 | + } else { |
| 101 | + renderWrapperName = isIdentifier(node.id) && node.id.name; |
| 102 | + } |
| 103 | + } |
| 104 | + }, |
| 105 | + |
| 106 | + CallExpression(node: TSESTree.CallExpression) { |
| 107 | + if (isMemberExpression(node.callee)) { |
| 108 | + showErrorIfChainedContainerMethod(node.callee); |
| 109 | + } else { |
| 110 | + isIdentifier(node.callee) && |
| 111 | + destructuredContainerPropNames.includes(node.callee.name) && |
| 112 | + context.report({ |
| 113 | + node, |
| 114 | + messageId: 'noContainer', |
| 115 | + }); |
| 116 | + } |
| 117 | + }, |
| 118 | + }; |
| 119 | + }, |
| 120 | +}); |
0 commit comments