-
-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathcreateObjectExpression.js
More file actions
54 lines (44 loc) · 1.18 KB
/
createObjectExpression.js
File metadata and controls
54 lines (44 loc) · 1.18 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
// @flow
import {
booleanLiteral,
isAnyTypeAnnotation,
ObjectExpression,
objectExpression,
objectProperty,
stringLiteral
} from '@babel/types';
type InputObjectType = {
[key: string]: *
};
/**
* Creates an AST representation of an InputObjectType shape object.
*/
const createObjectExpression = (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 (isAnyTypeAnnotation(value)) {
} else if (typeof value === 'string') {
newValue = stringLiteral(value);
} else if (typeof value === 'object') {
newValue = createObjectExpression(value);
} else if (typeof value === 'boolean') {
newValue = booleanLiteral(value);
} else if (typeof value === 'undefined') {
// eslint-disable-next-line no-continue
continue;
} else {
throw new TypeError('Unexpected type: ' + typeof value);
}
properties.push(
objectProperty(
stringLiteral(name),
newValue
)
);
}
return objectExpression(properties);
};
export default createObjectExpression;