.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / node_modules / eslint-visitor-keys / lib / index.js
1 /**
2  * @author Toru Nagashima <https://github.com/mysticatea>
3  * See LICENSE file in root directory for full license.
4  */
5 "use strict";
6
7 const KEYS = require("./visitor-keys.json");
8
9 // Types.
10 const NODE_TYPES = Object.freeze(Object.keys(KEYS));
11
12 // Freeze the keys.
13 for (const type of NODE_TYPES) {
14     Object.freeze(KEYS[type]);
15 }
16 Object.freeze(KEYS);
17
18 // List to ignore keys.
19 const KEY_BLACKLIST = new Set([
20     "parent",
21     "leadingComments",
22     "trailingComments"
23 ]);
24
25 /**
26  * Check whether a given key should be used or not.
27  * @param {string} key The key to check.
28  * @returns {boolean} `true` if the key should be used.
29  */
30 function filterKey(key) {
31     return !KEY_BLACKLIST.has(key) && key[0] !== "_";
32 }
33
34 //------------------------------------------------------------------------------
35 // Public interfaces
36 //------------------------------------------------------------------------------
37
38 module.exports = Object.freeze({
39
40     /**
41      * Visitor keys.
42      * @type {{ [type: string]: string[] | undefined }}
43      */
44     KEYS,
45
46     /**
47      * Get visitor keys of a given node.
48      * @param {Object} node The AST node to get keys.
49      * @returns {string[]} Visitor keys of the node.
50      */
51     getKeys(node) {
52         return Object.keys(node).filter(filterKey);
53     },
54
55     // Disable valid-jsdoc rule because it reports syntax error on the type of @returns.
56     // eslint-disable-next-line valid-jsdoc
57     /**
58      * Make the union set with `KEYS` and given keys.
59      * @param {Object} additionalKeys The additional keys.
60      * @returns {{ [type: string]: string[] | undefined }} The union set.
61      */
62     unionWith(additionalKeys) {
63         const retv = Object.assign({}, KEYS);
64
65         for (const type of Object.keys(additionalKeys)) {
66             if (retv.hasOwnProperty(type)) {
67                 const keys = new Set(additionalKeys[type]);
68
69                 for (const key of retv[type]) {
70                     keys.add(key);
71                 }
72
73                 retv[type] = Object.freeze(Array.from(keys));
74             } else {
75                 retv[type] = Object.freeze(Array.from(additionalKeys[type]));
76             }
77         }
78
79         return Object.freeze(retv);
80     }
81 });