Skip to content

Commit

Permalink
Avoid dynamic dispatch when calling wrapCheck (#16060)
Browse files Browse the repository at this point in the history
* Avoid dynamic dispatch when calling wrapCheck

* Update visitors.ts

* Update visitors.ts

* Update visitors.ts
  • Loading branch information
yepitschunked committed Oct 25, 2023
1 parent 6d9725c commit 4fb4fa6
Showing 1 changed file with 5 additions and 2 deletions.
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

0 comments on commit 4fb4fa6

Please sign in to comment.