chore: Cleanup inherit in @babel/types (#18117)

This commit is contained in:
Nicolò Ribaudo
2026-07-06 10:22:22 -04:00
committed by GitHub
parent 91277e9e7e
commit 710096c6d1
+16 -4
View File
@@ -1,14 +1,26 @@
import type * as t from "../index.ts";
type MaybeArray = unknown[] | null | undefined;
type ArrayKeys<C> = {
[K in keyof C]: C[K] extends MaybeArray ? K : never;
}[keyof C];
export default function inherit<C extends t.Node, P extends t.Node>(
key: keyof C & keyof P,
key: ArrayKeys<C> & ArrayKeys<P>,
child: C,
parent: P,
): void {
if (child && parent) {
// @ts-expect-error Could further refine key definitions
child[key] = Array.from(
new Set(([] as any[]).concat(child[key], parent[key]).filter(Boolean)),
const parentVal = parent[key] as MaybeArray;
if (!parentVal) return;
const childVal = child[key] as MaybeArray;
(child[key] as MaybeArray) = Array.from(
childVal
? new Set([...childVal, ...parentVal].filter(Boolean))
: parentVal,
);
}
}