|
| 1 | +import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils' |
| 2 | +import { AccessorNode, createEslintRule, getAccessorValue } from '../utils' |
| 3 | +import { |
| 4 | + KnownMemberExpressionProperty, |
| 5 | + parseVitestFnCall, |
| 6 | +} from '../utils/parse-vitest-fn-call' |
| 7 | + |
| 8 | +export const RULE_NAME = 'require-awaited-expect-poll' |
| 9 | +export type MESSAGE_ID = 'notAwaited' |
| 10 | +export type Options = [] |
| 11 | + |
| 12 | +export default createEslintRule<Options, MESSAGE_ID>({ |
| 13 | + name: RULE_NAME, |
| 14 | + meta: { |
| 15 | + docs: { |
| 16 | + requiresTypeChecking: false, |
| 17 | + recommended: false, |
| 18 | + description: 'ensure that every `expect.poll` call is awaited', |
| 19 | + }, |
| 20 | + messages: { |
| 21 | + notAwaited: '`{{ method }}` calls should be awaited', |
| 22 | + }, |
| 23 | + type: 'problem', |
| 24 | + schema: [], |
| 25 | + }, |
| 26 | + defaultOptions: [], |
| 27 | + create(context) { |
| 28 | + const reported = new Set<TSESTree.Node>() |
| 29 | + |
| 30 | + return { |
| 31 | + CallExpression(node) { |
| 32 | + const vitestFnCall = parseVitestFnCall(node, context) |
| 33 | + |
| 34 | + if ( |
| 35 | + vitestFnCall?.type !== 'expect' || |
| 36 | + !vitestFnCall.members.length || |
| 37 | + !memberRequiresAwait(vitestFnCall.members[0]) |
| 38 | + ) { |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + const nodeToReport = vitestFnCall.members[0].parent |
| 43 | + |
| 44 | + if (reported.has(nodeToReport)) { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + const topMostNode = skipSequenceExpressions( |
| 49 | + skipMatchersAndModifiers(vitestFnCall.head.node), |
| 50 | + ) |
| 51 | + |
| 52 | + const isHandled = |
| 53 | + topMostNode.parent?.type === AST_NODE_TYPES.AwaitExpression || |
| 54 | + topMostNode.parent?.type === AST_NODE_TYPES.ReturnStatement |
| 55 | + |
| 56 | + if (isHandled) { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + context.report({ |
| 61 | + node: nodeToReport, |
| 62 | + messageId: 'notAwaited', |
| 63 | + data: { |
| 64 | + method: `expect.${getAccessorValue(vitestFnCall.members[0])}`, |
| 65 | + }, |
| 66 | + }) |
| 67 | + |
| 68 | + reported.add(nodeToReport) |
| 69 | + }, |
| 70 | + } |
| 71 | + }, |
| 72 | +}) |
| 73 | + |
| 74 | +const awaitedMembers = ['poll', 'element'] |
| 75 | + |
| 76 | +function memberRequiresAwait(member: KnownMemberExpressionProperty): boolean { |
| 77 | + return awaitedMembers.includes(getAccessorValue(member)) |
| 78 | +} |
| 79 | + |
| 80 | +function skipMatchersAndModifiers(node: AccessorNode): TSESTree.Node { |
| 81 | + let currentNode: TSESTree.Node = node |
| 82 | + |
| 83 | + while ( |
| 84 | + currentNode.parent.type === AST_NODE_TYPES.MemberExpression || |
| 85 | + currentNode.parent.type === AST_NODE_TYPES.CallExpression |
| 86 | + ) { |
| 87 | + currentNode = currentNode.parent |
| 88 | + } |
| 89 | + |
| 90 | + return currentNode |
| 91 | +} |
| 92 | + |
| 93 | +function skipSequenceExpressions(node: TSESTree.Node): TSESTree.Node { |
| 94 | + let currentNode: TSESTree.Node = node |
| 95 | + |
| 96 | + while ( |
| 97 | + currentNode.parent?.type === AST_NODE_TYPES.SequenceExpression && |
| 98 | + currentNode.parent.expressions.at(-1) === currentNode |
| 99 | + ) { |
| 100 | + currentNode = currentNode.parent |
| 101 | + } |
| 102 | + |
| 103 | + return currentNode |
| 104 | +} |
0 commit comments