Skip to content

Commit d3fe1a2

Browse files
Add files via upload
1 parent 7b7ba05 commit d3fe1a2

2 files changed

Lines changed: 385 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)