-
-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathcreateObjectExpression.js
49 lines (39 loc) · 1.13 KB
/
createObjectExpression.js
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
// @flow
import BabelTypes, {
ObjectExpression
} from '@babel/types';
type InputObjectType = {
[key: string]: *
};
/**
* Creates an AST representation of an InputObjectType shape object.
*/
const createObjectExpression = (t: BabelTypes, object: InputObjectType): ObjectExpression => {
const properties = [];
for (const name of Object.keys(object)) {
const value = object[name];
let newValue;
// eslint-disable-next-line no-empty
if (t.isAnyTypeAnnotation(value)) {
} else if (typeof value === 'string') {
newValue = t.stringLiteral(value);
} else if (typeof value === 'object') {
newValue = createObjectExpression(t, value);
} else if (typeof value === 'boolean') {
newValue = t.booleanLiteral(value);
} else if (typeof value === 'undefined') {
// eslint-disable-next-line no-continue
continue;
} else {
throw new TypeError('Unexpected type: ' + typeof value);
}
properties.push(
t.objectProperty(
t.stringLiteral(name),
newValue
)
);
}
return t.objectExpression(properties);
};
export default createObjectExpression;