Debian Codegraph @

packages/babel-types/src/converters/valueToNode.ts

raw
1 import isValidIdentifier from "../validators/isValidIdentifier";
2 import {
3 identifier,
4 booleanLiteral,
5 nullLiteral,
6 stringLiteral,
7 numericLiteral,
8 regExpLiteral,
9 arrayExpression,
10 objectProperty,
11 objectExpression,
12 unaryExpression,
13 binaryExpression,
14 } from "../builders/generated";
15 import type * as t from "..";
16
17 export default valueToNode as {
18 (value: undefined): t.Identifier; // TODO: This should return "void 0"
19 (value: boolean): t.BooleanLiteral;
20 (value: null): t.NullLiteral;
21 (value: string): t.StringLiteral;
22 // Infinities and NaN need to use a BinaryExpression; negative values must be wrapped in UnaryExpression
23 (value: number): t.NumericLiteral | t.BinaryExpression | t.UnaryExpression;
24 (value: RegExp): t.RegExpLiteral;
25 (value: ReadonlyArray<unknown>): t.ArrayExpression;
26
27 // this throws with objects that are not plain objects,
28 // or if there are non-valueToNode-able values
29 (value: object): t.ObjectExpression;
30
31 (value: unknown): t.Expression;
32 };
33
34 // @ts-expect-error: Object.prototype.toString must return a string
35 const objectToString: (value: unknown) => string = Function.call.bind(
36 Object.prototype.toString,
37 );
38
39 function isRegExp(value: unknown): value is RegExp {
40 return objectToString(value) === "[object RegExp]";
41 }
42
43 function isPlainObject(value: unknown): value is object {
44 if (
45 typeof value !== "object" ||
46 value === null ||
47 Object.prototype.toString.call(value) !== "[object Object]"
48 ) {
49 return false;
50 }
51 const proto = Object.getPrototypeOf(value);
52 // Object.prototype's __proto__ is null. Every other class's __proto__.__proto__ is
53 // not null by default. We cannot check if proto === Object.prototype because it
54 // could come from another realm.
55 return proto === null || Object.getPrototypeOf(proto) === null;
56 }
57
58 function valueToNode(value: unknown): t.Expression {
59 // undefined
60 if (value === undefined) {
61 return identifier("undefined");
62 }
63
64 // boolean
65 if (value === true || value === false) {
66 return booleanLiteral(value);
67 }
68
69 // null
70 if (value === null) {
71 return nullLiteral();
72 }
73
74 // strings
75 if (typeof value === "string") {
76 return stringLiteral(value);
77 }
78
79 // numbers
80 if (typeof value === "number") {
81 let result;
82 if (Number.isFinite(value)) {
83 result = numericLiteral(Math.abs(value));
84 } else {
85 let numerator;
86 if (Number.isNaN(value)) {
87 // NaN
88 numerator = numericLiteral(0);
89 } else {
90 // Infinity / -Infinity
91 numerator = numericLiteral(1);
92 }
93
94 result = binaryExpression("/", numerator, numericLiteral(0));
95 }
96
97 if (value < 0 || Object.is(value, -0)) {
98 result = unaryExpression("-", result);
99 }
100
101 return result;
102 }
103
104 // regexes
105 if (isRegExp(value)) {
106 const pattern = value.source;
107 const flags = value.toString().match(/\/([a-z]+|)$/)[1];
108 return regExpLiteral(pattern, flags);
109 }
110
111 // array
112 if (Array.isArray(value)) {
113 return arrayExpression(value.map(valueToNode));
114 }
115
116 // object
117 if (isPlainObject(value)) {
118 const props = [];
119 for (const key of Object.keys(value)) {
120 let nodeKey;
121 if (isValidIdentifier(key)) {
122 nodeKey = identifier(key);
123 } else {
124 nodeKey = stringLiteral(key);
125 }
126 props.push(
127 objectProperty(
128 nodeKey,
129 valueToNode(
130 // @ts-expect-error key must present in value
131 value[key],
132 ),
133 ),
134 );
135 }
136 return objectExpression(props);
137 }
138
139 throw new Error("don't know how to turn this value into a node");
140 }