/
valueToNode.ts
140 lines (122 loc) 路 3.55 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import isValidIdentifier from "../validators/isValidIdentifier.ts";
import {
identifier,
booleanLiteral,
nullLiteral,
stringLiteral,
numericLiteral,
regExpLiteral,
arrayExpression,
objectProperty,
objectExpression,
unaryExpression,
binaryExpression,
} from "../builders/generated/index.ts";
import type * as t from "../index.ts";
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 plain objects,
// or if there are non-valueToNode-able values
(value: object): t.ObjectExpression;
(value: unknown): t.Expression;
};
// @ts-expect-error: Object.prototype.toString must return a string
const objectToString: (value: unknown) => string = Function.call.bind(
Object.prototype.toString,
);
function isRegExp(value: unknown): value is RegExp {
return objectToString(value) === "[object RegExp]";
}
function isPlainObject(value: unknown): value is object {
if (
typeof value !== "object" ||
value === null ||
Object.prototype.toString.call(value) !== "[object Object]"
) {
return false;
}
const proto = Object.getPrototypeOf(value);
// Object.prototype's __proto__ is null. Every other class's __proto__.__proto__ is
// not null by default. We cannot check if proto === Object.prototype because it
// could come from another realm.
return proto === null || Object.getPrototypeOf(proto) === null;
}
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(
// @ts-expect-error key must present in value
value[key],
),
),
);
}
return objectExpression(props);
}
throw new Error("don't know how to turn this value into a node");
}