-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgetVariableDeclarationIdentifierNames.mjs
110 lines (102 loc) · 3.87 KB
/
getVariableDeclarationIdentifierNames.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// @ts-check
import babel from "@babel/core";
/**
* Gets identifier names from a variable declaration Babel AST node. Used to
* find export names within a named export declaration that contains a variable
* declaration.
* @param {babel.types.VariableDeclaration} variableDeclaration Variable
* declaration Babel AST node.
* @returns {Array<string>} Identifier names.
*/
export default function getVariableDeclarationIdentifierNames(
variableDeclaration,
) {
if (!babel.types.isVariableDeclaration(variableDeclaration))
throw new TypeError(
"Argument 1 `variableDeclaration` must be a `VariableDeclaration` Babel AST node.",
);
/** @type {Array<string>} */
const names = [];
/**
* Recursively collects identifier names.
* @param {babel.types.Node} node Babel AST node.
*/
function collectIdentifierNames(node) {
switch (node.type) {
case "Identifier":
// E.g. `export const a = 1`
// ^
// E.g. `export var a, b, c = 1`
// ^
// E.g. `export const { a } = { a: 1 }`
// ^
// E.g. `export const [a] = [1]`
// ^
names.push(node.name);
break;
case "ObjectPattern":
// E.g. `export const { a } = { a: 1 }`
// ^^^^^
for (const property of node.properties)
switch (property.type) {
case "ObjectProperty":
// E.g. `export const { a } = { a: 1 }`
// ^
// E.g. `export const { a: { b } } = { a: { b: 1 } }`
// ^^^^^^^^
// E.g. `export const { a: [b] } = { a: [1] }`
// ^^^^^^
// E.g. `export const { a: b } = { a: 1 }`
// ^^^^
collectIdentifierNames(
// To account for property renaming use the `value`, not the
// `key`. If the property isn’t renamed they are the same.
property.value,
);
break;
case "RestElement":
// E.g. `export const { a, ...b } = { a: 1, b: 1, c: 1 }`
// ^^^^
collectIdentifierNames(property.argument);
}
break;
case "ArrayPattern":
// E.g. `export const [a, b] = [1, 2]`
// ^^^^^^
for (const element of node.elements)
if (
// A `null` element represents a skipped array item.
// E.g. `export const [, a] = [1, 2]`
// ^
element !== null
)
if (element.type === "RestElement")
// E.g. `export const [a, ...b] = [1, 2, 3]`
// ^^^^
// E.g. `export const [a, ...[b]] = [1, 2, 3]`
// ^^^^^^
collectIdentifierNames(element.argument);
// As the element is neither skipped or rest, further recursion is
// necessary.
// E.g. `export const [a] = [1]`
// ^
// E.g. `export const [[a]] = [[1]]`
// ^^^
// E.g. `export const [{ a }] = [{ a: 1 }]`
// ^^^^^
else collectIdentifierNames(element);
}
}
// The variable declaration could have one or more declarations.
// E.g. `export const a = 1`
// ^^^^^
// E.g. `export var a, b, c = 1`
// ^ ^ ^^^^^
// Only the `id` (not the `init` value) is relevant for gathering identifier
// names.
// E.g. `export const a = 1`
// ^
for (const { id } of variableDeclaration.declarations)
collectIdentifierNames(id);
return names;
}