-
-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathresolveObjectValuesToArray.ts
More file actions
117 lines (96 loc) · 2.96 KB
/
resolveObjectValuesToArray.ts
File metadata and controls
117 lines (96 loc) · 2.96 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
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
111
112
113
114
115
116
117
import resolveToValue from './resolveToValue.js';
import type { NodePath } from '@babel/traverse';
import type {
CallExpression,
Identifier,
NumericLiteral,
StringLiteral,
} from '@babel/types';
function isObjectValuesCall(path: NodePath): path is NodePath<CallExpression> {
if (!path.isCallExpression() || path.node.arguments.length !== 1) {
return false;
}
const callee = path.get('callee');
if (!callee.isMemberExpression()) {
return false;
}
const object = callee.get('object');
const property = callee.get('property');
return (
object.isIdentifier({ name: 'Object' }) &&
property.isIdentifier({ name: 'values' })
);
}
// Resolves an ObjectExpression or an ObjectTypeAnnotation
function resolveObjectToPropMap(object: NodePath): Map<string, string> | null {
if (object.isObjectExpression()) {
const values = new Map<string, string>();
let error = false;
object.get('properties').forEach((propPath) => {
if (error || propPath.isObjectMethod()) return;
if (propPath.isObjectProperty()) {
const key = propPath.get('key') as NodePath<
Identifier | NumericLiteral | StringLiteral
>;
let name: string;
// Key is either Identifier or Literal
if (key.isIdentifier()) {
name = key.node.name;
} else if (key.isNumericLiteral() || key.isStringLiteral()) {
name = `${key.node.value}`;
} else {
error = true;
return;
}
const valuePath = resolveToValue(propPath.get('value'));
const value = valuePath.isStringLiteral()
? `"${valuePath.node.value}"`
: valuePath.isNumericLiteral()
? `${valuePath.node.value}`
: // we return null here because there are a lot of cases and we don't know yet what we need to handle
'null';
values.set(name, value);
} else if (propPath.isSpreadElement()) {
const spreadObject = resolveToValue(propPath.get('argument'));
const spreadValues = resolveObjectToPropMap(spreadObject);
if (!spreadValues) {
error = true;
return;
}
for (const entry of spreadValues.entries()) {
const [key, value] = entry;
values.set(key, value);
}
}
});
if (!error) {
return values;
}
}
return null;
}
/**
* Returns an ArrayExpression which contains all the values resolved from an object
*
* Ignores setters in objects
*
* Returns null in case of
* unresolvable spreads
* computed identifier values
*/
export default function resolveObjectValuesToArray(
path: NodePath,
): string[] | null {
if (isObjectValuesCall(path)) {
const [argument] = path.get('arguments');
if (!argument) {
return null;
}
const objectExpression = resolveToValue(argument);
const values = resolveObjectToPropMap(objectExpression);
if (values) {
return Array.from(values.values());
}
}
return null;
}