1 | /**
|
---|
2 | * @author Toru Nagashima <https://github.com/mysticatea>
|
---|
3 | * See LICENSE file in root directory for full license.
|
---|
4 | */
|
---|
5 | import KEYS from "./visitor-keys.js";
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * @typedef {import('./visitor-keys.js').VisitorKeys} VisitorKeys
|
---|
9 | */
|
---|
10 |
|
---|
11 | // List to ignore keys.
|
---|
12 | const KEY_BLACKLIST = new Set([
|
---|
13 | "parent",
|
---|
14 | "leadingComments",
|
---|
15 | "trailingComments"
|
---|
16 | ]);
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Check whether a given key should be used or not.
|
---|
20 | * @param {string} key The key to check.
|
---|
21 | * @returns {boolean} `true` if the key should be used.
|
---|
22 | */
|
---|
23 | function filterKey(key) {
|
---|
24 | return !KEY_BLACKLIST.has(key) && key[0] !== "_";
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Get visitor keys of a given node.
|
---|
29 | * @param {object} node The AST node to get keys.
|
---|
30 | * @returns {readonly string[]} Visitor keys of the node.
|
---|
31 | */
|
---|
32 | export function getKeys(node) {
|
---|
33 | return Object.keys(node).filter(filterKey);
|
---|
34 | }
|
---|
35 |
|
---|
36 | // Disable valid-jsdoc rule because it reports syntax error on the type of @returns.
|
---|
37 | // eslint-disable-next-line valid-jsdoc
|
---|
38 | /**
|
---|
39 | * Make the union set with `KEYS` and given keys.
|
---|
40 | * @param {VisitorKeys} additionalKeys The additional keys.
|
---|
41 | * @returns {VisitorKeys} The union set.
|
---|
42 | */
|
---|
43 | export function unionWith(additionalKeys) {
|
---|
44 | const retv = /** @type {{
|
---|
45 | [type: string]: ReadonlyArray<string>
|
---|
46 | }} */ (Object.assign({}, KEYS));
|
---|
47 |
|
---|
48 | for (const type of Object.keys(additionalKeys)) {
|
---|
49 | if (Object.prototype.hasOwnProperty.call(retv, type)) {
|
---|
50 | const keys = new Set(additionalKeys[type]);
|
---|
51 |
|
---|
52 | for (const key of retv[type]) {
|
---|
53 | keys.add(key);
|
---|
54 | }
|
---|
55 |
|
---|
56 | retv[type] = Object.freeze(Array.from(keys));
|
---|
57 | } else {
|
---|
58 | retv[type] = Object.freeze(Array.from(additionalKeys[type]));
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | return Object.freeze(retv);
|
---|
63 | }
|
---|
64 |
|
---|
65 | export { KEYS };
|
---|