-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
valueToNode.ts
113 lines (97 loc) 路 2.79 KB
/
valueToNode.ts
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
import isPlainObject from "lodash/isPlainObject";
import isValidIdentifier from "../validators/isValidIdentifier";
import {
identifier,
booleanLiteral,
nullLiteral,
stringLiteral,
numericLiteral,
regExpLiteral,
arrayExpression,
objectProperty,
objectExpression,
unaryExpression,
binaryExpression,
} from "../builders/generated";
import type * as t from "..";
export default valueToNode as {
(value: undefined): t.Identifier; // TODO: This should return "void 0"
(value: boolean): t.BooleanLiteral;
(value: null): t.NullLiteral;
(value: string): t.StringLiteral;
// Infinities and NaN need to use a BinaryExpression; negative values must be wrapped in UnaryExpression
(value: number): t.NumericLiteral | t.BinaryExpression | t.UnaryExpression;
(value: RegExp): t.RegExpLiteral;
(value: ReadonlyArray<unknown>): t.ArrayExpression;
// this throws with objects that are not PlainObject according to lodash,
// or if there are non-valueToNode-able values
(value: object): t.ObjectExpression;
(value: unknown): t.Expression;
};
function isRegExp(value): value is RegExp {
return Object.prototype.toString.call(value) === "[object RegExp]";
}
function valueToNode(value: unknown): t.Expression {
// undefined
if (value === undefined) {
return identifier("undefined");
}
// boolean
if (value === true || value === false) {
return booleanLiteral(value);
}
// null
if (value === null) {
return nullLiteral();
}
// strings
if (typeof value === "string") {
return stringLiteral(value);
}
// numbers
if (typeof value === "number") {
let result;
if (Number.isFinite(value)) {
result = numericLiteral(Math.abs(value));
} else {
let numerator;
if (Number.isNaN(value)) {
// NaN
numerator = numericLiteral(0);
} else {
// Infinity / -Infinity
numerator = numericLiteral(1);
}
result = binaryExpression("/", numerator, numericLiteral(0));
}
if (value < 0 || Object.is(value, -0)) {
result = unaryExpression("-", result);
}
return result;
}
// regexes
if (isRegExp(value)) {
const pattern = value.source;
const flags = value.toString().match(/\/([a-z]+|)$/)[1];
return regExpLiteral(pattern, flags);
}
// array
if (Array.isArray(value)) {
return arrayExpression(value.map(valueToNode));
}
// object
if (isPlainObject(value)) {
const props = [];
for (const key of Object.keys(value)) {
let nodeKey;
if (isValidIdentifier(key)) {
nodeKey = identifier(key);
} else {
nodeKey = stringLiteral(key);
}
props.push(objectProperty(nodeKey, valueToNode(value[key])));
}
return objectExpression(props);
}
throw new Error("don't know how to turn this value into a node");
}