|
| 1 | +import { ASTUtils, TSESTree } from '@typescript-eslint/utils'; |
| 2 | + |
| 3 | +import { createTestingLibraryRule } from '../create-testing-library-rule'; |
| 4 | +import { |
| 5 | + findClosestCallExpressionNode, |
| 6 | + getFunctionName, |
| 7 | + getInnermostReturningFunction, |
| 8 | + getVariableReferences, |
| 9 | + isPromiseHandled, |
| 10 | +} from '../node-utils'; |
| 11 | +import { EVENTS_SIMULATORS } from '../utils'; |
| 12 | + |
| 13 | +export const RULE_NAME = 'await-async-event'; |
| 14 | +export type MessageIds = 'awaitAsyncEvent' | 'awaitAsyncEventWrapper'; |
| 15 | +const FIRE_EVENT_NAME = 'fireEvent'; |
| 16 | +const USER_EVENT_NAME = 'userEvent'; |
| 17 | +type EventModules = typeof EVENTS_SIMULATORS[number]; |
| 18 | +export type Options = [ |
| 19 | + { |
| 20 | + eventModule: EventModules | EventModules[]; |
| 21 | + } |
| 22 | +]; |
| 23 | + |
| 24 | +export default createTestingLibraryRule<Options, MessageIds>({ |
| 25 | + name: RULE_NAME, |
| 26 | + meta: { |
| 27 | + type: 'problem', |
| 28 | + docs: { |
| 29 | + description: 'Enforce promises from async event methods are handled', |
| 30 | + recommendedConfig: { |
| 31 | + dom: ['error', { eventModule: 'userEvent' }], |
| 32 | + angular: ['error', { eventModule: 'userEvent' }], |
| 33 | + react: ['error', { eventModule: 'userEvent' }], |
| 34 | + vue: ['error', { eventModule: ['fireEvent', 'userEvent'] }], |
| 35 | + marko: ['error', { eventModule: ['fireEvent', 'userEvent'] }], |
| 36 | + }, |
| 37 | + }, |
| 38 | + messages: { |
| 39 | + awaitAsyncEvent: |
| 40 | + 'Promise returned from async event method `{{ name }}` must be handled', |
| 41 | + awaitAsyncEventWrapper: |
| 42 | + 'Promise returned from `{{ name }}` wrapper over async event method must be handled', |
| 43 | + }, |
| 44 | + schema: [ |
| 45 | + { |
| 46 | + type: 'object', |
| 47 | + default: {}, |
| 48 | + additionalProperties: false, |
| 49 | + properties: { |
| 50 | + eventModule: { |
| 51 | + default: USER_EVENT_NAME, |
| 52 | + oneOf: [ |
| 53 | + { |
| 54 | + type: 'string', |
| 55 | + enum: EVENTS_SIMULATORS, |
| 56 | + }, |
| 57 | + { |
| 58 | + type: 'array', |
| 59 | + items: { |
| 60 | + type: 'string', |
| 61 | + enum: EVENTS_SIMULATORS, |
| 62 | + }, |
| 63 | + }, |
| 64 | + ], |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + ], |
| 69 | + }, |
| 70 | + defaultOptions: [ |
| 71 | + { |
| 72 | + eventModule: USER_EVENT_NAME, |
| 73 | + }, |
| 74 | + ], |
| 75 | + |
| 76 | + create(context, [options], helpers) { |
| 77 | + const functionWrappersNames: string[] = []; |
| 78 | + |
| 79 | + function reportUnhandledNode( |
| 80 | + node: TSESTree.Identifier, |
| 81 | + closestCallExpressionNode: TSESTree.CallExpression, |
| 82 | + messageId: MessageIds = 'awaitAsyncEvent' |
| 83 | + ): void { |
| 84 | + if (!isPromiseHandled(node)) { |
| 85 | + context.report({ |
| 86 | + node: closestCallExpressionNode.callee, |
| 87 | + messageId, |
| 88 | + data: { name: node.name }, |
| 89 | + }); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + function detectEventMethodWrapper(node: TSESTree.Identifier): void { |
| 94 | + const innerFunction = getInnermostReturningFunction(context, node); |
| 95 | + |
| 96 | + if (innerFunction) { |
| 97 | + functionWrappersNames.push(getFunctionName(innerFunction)); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + const eventModules = |
| 102 | + typeof options.eventModule === 'string' |
| 103 | + ? [options.eventModule] |
| 104 | + : options.eventModule; |
| 105 | + const isFireEventEnabled = eventModules.includes(FIRE_EVENT_NAME); |
| 106 | + const isUserEventEnabled = eventModules.includes(USER_EVENT_NAME); |
| 107 | + |
| 108 | + return { |
| 109 | + 'CallExpression Identifier'(node: TSESTree.Identifier) { |
| 110 | + if ( |
| 111 | + (isFireEventEnabled && helpers.isFireEventMethod(node)) || |
| 112 | + (isUserEventEnabled && helpers.isUserEventMethod(node)) |
| 113 | + ) { |
| 114 | + detectEventMethodWrapper(node); |
| 115 | + |
| 116 | + const closestCallExpression = findClosestCallExpressionNode( |
| 117 | + node, |
| 118 | + true |
| 119 | + ); |
| 120 | + |
| 121 | + if (!closestCallExpression || !closestCallExpression.parent) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + const references = getVariableReferences( |
| 126 | + context, |
| 127 | + closestCallExpression.parent |
| 128 | + ); |
| 129 | + |
| 130 | + if (references.length === 0) { |
| 131 | + reportUnhandledNode(node, closestCallExpression); |
| 132 | + } else { |
| 133 | + for (const reference of references) { |
| 134 | + if (ASTUtils.isIdentifier(reference.identifier)) { |
| 135 | + reportUnhandledNode( |
| 136 | + reference.identifier, |
| 137 | + closestCallExpression |
| 138 | + ); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } else if (functionWrappersNames.includes(node.name)) { |
| 143 | + // report promise returned from function wrapping fire event method |
| 144 | + // previously detected |
| 145 | + const closestCallExpression = findClosestCallExpressionNode( |
| 146 | + node, |
| 147 | + true |
| 148 | + ); |
| 149 | + |
| 150 | + if (!closestCallExpression) { |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + reportUnhandledNode( |
| 155 | + node, |
| 156 | + closestCallExpression, |
| 157 | + 'awaitAsyncEventWrapper' |
| 158 | + ); |
| 159 | + } |
| 160 | + }, |
| 161 | + }; |
| 162 | + }, |
| 163 | +}); |
0 commit comments