Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid dynamic dispatch when calling wrapCheck #16060

Merged
merged 4 commits into from Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions 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,
Expand Down Expand Up @@ -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);
}
};
Expand Down