extractAssignedNames.ts
1.1 KB
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
import { Node } from 'estree-walker';
interface Extractors {
[key: string]: (names: Array<string>, param: Node) => void;
}
const extractors: Extractors = {
ArrayPattern(names: Array<string>, param: Node) {
for (const element of param.elements) {
if (element) extractors[element.type](names, element);
}
},
AssignmentPattern(names: Array<string>, param: Node) {
extractors[param.left.type](names, param.left);
},
Identifier(names: Array<string>, param: Node) {
names.push(param.name);
},
MemberExpression() {},
ObjectPattern(names: Array<string>, param: Node) {
for (const prop of param.properties) {
if (prop.type === 'RestElement') {
extractors.RestElement(names, prop);
} else {
extractors[prop.value.type](names, prop.value);
}
}
},
RestElement(names: Array<string>, param: Node) {
extractors[param.argument.type](names, param.argument);
}
};
const extractAssignedNames = function extractAssignedNames(param: Node): Array<string> {
const names: Array<string> = [];
extractors[param.type](names, param);
return names;
};
export { extractAssignedNames as default };