From 4fb4fa63e7d15b3993d466e79d5051ab1b1b8989 Mon Sep 17 00:00:00 2001 From: yepitschunked <125177+yepitschunked@users.noreply.github.com> Date: Wed, 25 Oct 2023 00:30:13 -0700 Subject: [PATCH] Avoid dynamic dispatch when calling wrapCheck (#16060) * Avoid dynamic dispatch when calling wrapCheck * Update visitors.ts * Update visitors.ts * Update visitors.ts --- packages/babel-traverse/src/visitors.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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); } };