diff --git a/packages/babel-traverse/src/visitors.ts b/packages/babel-traverse/src/visitors.ts index 0b0f603daefb..0e8facf33d2c 100644 --- a/packages/babel-traverse/src/visitors.ts +++ b/packages/babel-traverse/src/visitors.ts @@ -1,4 +1,5 @@ import * as virtualTypes from "./path/lib/virtual-types.ts"; +import * as virtualTypesValidators from "./path/lib/virtual-types-validator.ts"; import type { Node } from "@babel/types"; import { DEPRECATED_KEYS, @@ -330,9 +331,11 @@ function ensureCallbackArrays(obj: Visitor) { } function wrapCheck(nodeType: VIRTUAL_TYPES, fn: Function) { + const fnKey = `is${nodeType}`; + // @ts-expect-error we know virtualTypesValidators will contain `fnKey`, but TS doesn't + const validator = virtualTypesValidators[fnKey]; const newFn = function (this: unknown, path: NodePath) { - // @ts-expect-error: Expression produces a union type that is too complex to represent. - if (path[`is${nodeType}`]()) { + if (validator.call(path)) { return fn.apply(this, arguments); } };