| 1 |
import { INHERIT_KEYS } from "../constants"; |
| 2 |
import inheritsComments from "../comments/inheritsComments"; |
| 3 |
import type * as t from ".."; |
| 4 |
|
| 5 |
/** |
| 6 |
* Inherit all contextual properties from `parent` node to `child` node. |
| 7 |
*/ |
| 8 |
export default function inherits<T extends t.Node | null | undefined>( |
| 9 |
child: T, |
| 10 |
parent: t.Node | null | undefined, |
| 11 |
): T { |
| 12 |
if (!child || !parent) return child; |
| 13 |
|
| 14 |
// optionally inherit specific properties if not null |
| 15 |
for (const key of INHERIT_KEYS.optional) { |
| 16 |
// @ts-expect-error Fixme: refine parent types |
| 17 |
if (child[key] == null) { |
| 18 |
// @ts-expect-error Fixme: refine parent types |
| 19 |
child[key] = parent[key]; |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
// force inherit "private" properties |
| 24 |
for (const key of Object.keys(parent)) { |
| 25 |
if (key[0] === "_" && key !== "__clone") { |
| 26 |
// @ts-expect-error Fixme: refine parent types |
| 27 |
child[key] = parent[key]; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// force inherit select properties |
| 32 |
for (const key of INHERIT_KEYS.force) { |
| 33 |
// @ts-expect-error Fixme: refine parent types |
| 34 |
child[key] = parent[key]; |
| 35 |
} |
| 36 |
|
| 37 |
inheritsComments(child, parent); |
| 38 |
|
| 39 |
return child; |
| 40 |
} |