|
| 1 | +var keys = (function () { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + /* eslint-disable jsdoc/valid-types -- doesn't allow `readonly`. |
| 5 | + TODO: remove eslint-disable when https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/164 is fixed |
| 6 | + */ |
| 7 | + /** |
| 8 | + * @typedef {{ readonly [type: string]: ReadonlyArray<string> }} VisitorKeys |
| 9 | + */ |
| 10 | + /* eslint-enable jsdoc/valid-types -- doesn't allow `readonly string[]`. TODO: check why */ |
| 11 | + |
| 12 | + /** |
| 13 | + * @type {VisitorKeys} |
| 14 | + */ |
| 15 | + const KEYS = { |
| 16 | + ArrayExpression: ["elements"], |
| 17 | + ArrayPattern: ["elements"], |
| 18 | + ArrowFunctionExpression: ["params", "body"], |
| 19 | + AssignmentExpression: ["left", "right"], |
| 20 | + AssignmentPattern: ["left", "right"], |
| 21 | + AwaitExpression: ["argument"], |
| 22 | + BinaryExpression: ["left", "right"], |
| 23 | + BlockStatement: ["body"], |
| 24 | + BreakStatement: ["label"], |
| 25 | + CallExpression: ["callee", "arguments"], |
| 26 | + CatchClause: ["param", "body"], |
| 27 | + ChainExpression: ["expression"], |
| 28 | + ClassBody: ["body"], |
| 29 | + ClassDeclaration: ["id", "superClass", "body"], |
| 30 | + ClassExpression: ["id", "superClass", "body"], |
| 31 | + ConditionalExpression: ["test", "consequent", "alternate"], |
| 32 | + ContinueStatement: ["label"], |
| 33 | + DebuggerStatement: [], |
| 34 | + DoWhileStatement: ["body", "test"], |
| 35 | + EmptyStatement: [], |
| 36 | + ExperimentalRestProperty: ["argument"], |
| 37 | + ExperimentalSpreadProperty: ["argument"], |
| 38 | + ExportAllDeclaration: ["exported", "source", "attributes"], |
| 39 | + ExportDefaultDeclaration: ["declaration"], |
| 40 | + ExportNamedDeclaration: [ |
| 41 | + "declaration", |
| 42 | + "specifiers", |
| 43 | + "source", |
| 44 | + "attributes", |
| 45 | + ], |
| 46 | + ExportSpecifier: ["local", "exported"], |
| 47 | + ExpressionStatement: ["expression"], |
| 48 | + ForInStatement: ["left", "right", "body"], |
| 49 | + ForOfStatement: ["left", "right", "body"], |
| 50 | + ForStatement: ["init", "test", "update", "body"], |
| 51 | + FunctionDeclaration: ["id", "params", "body"], |
| 52 | + FunctionExpression: ["id", "params", "body"], |
| 53 | + Identifier: [], |
| 54 | + IfStatement: ["test", "consequent", "alternate"], |
| 55 | + ImportAttribute: ["key", "value"], |
| 56 | + ImportDeclaration: ["specifiers", "source", "attributes"], |
| 57 | + ImportDefaultSpecifier: ["local"], |
| 58 | + ImportExpression: ["source", "options"], |
| 59 | + ImportNamespaceSpecifier: ["local"], |
| 60 | + ImportSpecifier: ["imported", "local"], |
| 61 | + JSXAttribute: ["name", "value"], |
| 62 | + JSXClosingElement: ["name"], |
| 63 | + JSXClosingFragment: [], |
| 64 | + JSXElement: ["openingElement", "children", "closingElement"], |
| 65 | + JSXEmptyExpression: [], |
| 66 | + JSXExpressionContainer: ["expression"], |
| 67 | + JSXFragment: ["openingFragment", "children", "closingFragment"], |
| 68 | + JSXIdentifier: [], |
| 69 | + JSXMemberExpression: ["object", "property"], |
| 70 | + JSXNamespacedName: ["namespace", "name"], |
| 71 | + JSXOpeningElement: ["name", "attributes"], |
| 72 | + JSXOpeningFragment: [], |
| 73 | + JSXSpreadAttribute: ["argument"], |
| 74 | + JSXSpreadChild: ["expression"], |
| 75 | + JSXText: [], |
| 76 | + LabeledStatement: ["label", "body"], |
| 77 | + Literal: [], |
| 78 | + LogicalExpression: ["left", "right"], |
| 79 | + MemberExpression: ["object", "property"], |
| 80 | + MetaProperty: ["meta", "property"], |
| 81 | + MethodDefinition: ["key", "value"], |
| 82 | + NewExpression: ["callee", "arguments"], |
| 83 | + ObjectExpression: ["properties"], |
| 84 | + ObjectPattern: ["properties"], |
| 85 | + PrivateIdentifier: [], |
| 86 | + Program: ["body"], |
| 87 | + Property: ["key", "value"], |
| 88 | + PropertyDefinition: ["key", "value"], |
| 89 | + RestElement: ["argument"], |
| 90 | + ReturnStatement: ["argument"], |
| 91 | + SequenceExpression: ["expressions"], |
| 92 | + SpreadElement: ["argument"], |
| 93 | + StaticBlock: ["body"], |
| 94 | + Super: [], |
| 95 | + SwitchCase: ["test", "consequent"], |
| 96 | + SwitchStatement: ["discriminant", "cases"], |
| 97 | + TaggedTemplateExpression: ["tag", "quasi"], |
| 98 | + TemplateElement: [], |
| 99 | + TemplateLiteral: ["quasis", "expressions"], |
| 100 | + ThisExpression: [], |
| 101 | + ThrowStatement: ["argument"], |
| 102 | + TryStatement: ["block", "handler", "finalizer"], |
| 103 | + UnaryExpression: ["argument"], |
| 104 | + UpdateExpression: ["argument"], |
| 105 | + VariableDeclaration: ["declarations"], |
| 106 | + VariableDeclarator: ["id", "init"], |
| 107 | + WhileStatement: ["test", "body"], |
| 108 | + WithStatement: ["object", "body"], |
| 109 | + YieldExpression: ["argument"], |
| 110 | + }; |
| 111 | + |
| 112 | + // Types. |
| 113 | + const NODE_TYPES = Object.keys(KEYS); |
| 114 | + |
| 115 | + // Freeze the keys. |
| 116 | + for (const type of NODE_TYPES) { |
| 117 | + Object.freeze(KEYS[type]); |
| 118 | + } |
| 119 | + Object.freeze(KEYS); |
| 120 | + |
| 121 | + /** |
| 122 | + * @author Toru Nagashima <https://github.com/mysticatea> |
| 123 | + * See LICENSE file in root directory for full license. |
| 124 | + */ |
| 125 | + |
| 126 | + /** |
| 127 | + * @typedef {import('./visitor-keys.js').VisitorKeys} VisitorKeys |
| 128 | + */ |
| 129 | + |
| 130 | + // List to ignore keys. |
| 131 | + const KEY_BLACKLIST = new Set([ |
| 132 | + "parent", |
| 133 | + "leadingComments", |
| 134 | + "trailingComments", |
| 135 | + ]); |
| 136 | + |
| 137 | + /** |
| 138 | + * Check whether a given key should be used or not. |
| 139 | + * @param {string} key The key to check. |
| 140 | + * @returns {boolean} `true` if the key should be used. |
| 141 | + */ |
| 142 | + function filterKey(key) { |
| 143 | + return !KEY_BLACKLIST.has(key) && key[0] !== "_"; |
| 144 | + } |
| 145 | + |
| 146 | + /* eslint-disable jsdoc/valid-types -- doesn't allow `readonly`. |
| 147 | + TODO: remove eslint-disable when https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/164 is fixed |
| 148 | + */ |
| 149 | + /** |
| 150 | + * Get visitor keys of a given node. |
| 151 | + * @param {Object} node The AST node to get keys. |
| 152 | + * @returns {readonly string[]} Visitor keys of the node. |
| 153 | + */ |
| 154 | + function getKeys(node) { |
| 155 | + return Object.keys(node).filter(filterKey); |
| 156 | + } |
| 157 | + /* eslint-enable jsdoc/valid-types -- doesn't allow `readonly` */ |
| 158 | + |
| 159 | + /** |
| 160 | + * Make the union set with `KEYS` and given keys. |
| 161 | + * @param {VisitorKeys} additionalKeys The additional keys. |
| 162 | + * @returns {VisitorKeys} The union set. |
| 163 | + */ |
| 164 | + function unionWith(additionalKeys) { |
| 165 | + const retv = |
| 166 | + /** @type {{ [type: string]: ReadonlyArray<string> }} */ |
| 167 | + (Object.assign({}, KEYS)); |
| 168 | + |
| 169 | + for (const type of Object.keys(additionalKeys)) { |
| 170 | + if (Object.hasOwn(retv, type)) { |
| 171 | + const keys = new Set(additionalKeys[type]); |
| 172 | + |
| 173 | + for (const key of retv[type]) { |
| 174 | + keys.add(key); |
| 175 | + } |
| 176 | + |
| 177 | + retv[type] = Object.freeze(Array.from(keys)); |
| 178 | + } else { |
| 179 | + retv[type] = Object.freeze(Array.from(additionalKeys[type])); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return Object.freeze(retv); |
| 184 | + } |
| 185 | + |
| 186 | + var keys = /*#__PURE__*/Object.freeze({ |
| 187 | + __proto__: null, |
| 188 | + KEYS: KEYS, |
| 189 | + getKeys: getKeys, |
| 190 | + unionWith: unionWith |
| 191 | + }); |
| 192 | + |
| 193 | + return keys; |
| 194 | + |
| 195 | +})(); |
0 commit comments