mirror of
https://github.com/hanzoai/babel.git
synced 2026-09-24 16:56:45 +00:00
chore: Cleanup inherit in @babel/types (#18117)
This commit is contained in:
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user