| 1 |
import { COMMENT_KEYS } from "../constants"; |
| 2 |
import type * as t from ".."; |
| 3 |
|
| 4 |
const CLEAR_KEYS = [ |
| 5 |
"tokens", // only exist in t.File |
| 6 |
"start", |
| 7 |
"end", |
| 8 |
"loc", |
| 9 |
// Fixme: should be extra.raw / extra.rawValue? |
| 10 |
"raw", |
| 11 |
"rawValue", |
| 12 |
] as const; |
| 13 |
|
| 14 |
const CLEAR_KEYS_PLUS_COMMENTS = [ |
| 15 |
...COMMENT_KEYS, |
| 16 |
"comments", |
| 17 |
...CLEAR_KEYS, |
| 18 |
] as const; |
| 19 |
|
| 20 |
export type Options = { preserveComments?: boolean }; |
| 21 |
/** |
| 22 |
* Remove all of the _* properties from a node along with the additional metadata |
| 23 |
* properties like location data and raw token data. |
| 24 |
*/ |
| 25 |
export default function removeProperties( |
| 26 |
node: t.Node, |
| 27 |
opts: Options = {}, |
| 28 |
): void { |
| 29 |
const map = opts.preserveComments ? CLEAR_KEYS : CLEAR_KEYS_PLUS_COMMENTS; |
| 30 |
for (const key of map) { |
| 31 |
// @ts-expect-error tokens only exist in t.File |
| 32 |
if (node[key] != null) node[key] = undefined; |
| 33 |
} |
| 34 |
|
| 35 |
for (const key of Object.keys(node)) { |
| 36 |
// @ts-expect-error string can not index node |
| 37 |
if (key[0] === "_" && node[key] != null) node[key] = undefined; |
| 38 |
} |
| 39 |
|
| 40 |
const symbols: Array<symbol> = Object.getOwnPropertySymbols(node); |
| 41 |
for (const sym of symbols) { |
| 42 |
// @ts-expect-error Fixme: document symbol properties |
| 43 |
node[sym] = null; |
| 44 |
} |
| 45 |
} |