UNPKG

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