Debian Codegraph @

packages/babel-types/test/converters.js

raw
1 import * as t from "../lib/index.js";
2 import { parse } from "@babel/parser";
3
4 import _generate from "@babel/generator";
5 const generate = _generate.default || _generate;
6
7 function parseCode(string) {
8 return parse(string, {
9 allowReturnOutsideFunction: true,
10 }).program.body[0];
11 }
12 function generateCode(node) {
13 return generate(node).code;
14 }
15
16 describe("converters", function () {
17 it("toIdentifier", function () {
18 expect(t.toIdentifier("swag-lord")).toBe("swagLord");
19 expect(t.toIdentifier("ɵ2")).toBe("ɵ2");
20 expect(t.toIdentifier("ℬ1")).toBe("ℬ1");
21 expect(t.toIdentifier("1bc")).toBe("bc");
22 expect(t.toIdentifier("\u0487a")).toBe("_\u0487a");
23 });
24
25 describe("valueToNode", function () {
26 it("number", function () {
27 expect(t.valueToNode(Math.PI)).toEqual(t.numericLiteral(Math.PI));
28 expect(t.valueToNode(-Math.PI)).toEqual(
29 t.unaryExpression("-", t.numericLiteral(Math.PI)),
30 );
31 expect(t.valueToNode(0)).toEqual(t.numericLiteral(0));
32 expect(t.valueToNode(-0)).toEqual(
33 t.unaryExpression("-", t.numericLiteral(0)),
34 );
35 expect(t.valueToNode(NaN)).toEqual(
36 t.binaryExpression("/", t.numericLiteral(0), t.numericLiteral(0)),
37 );
38 expect(t.valueToNode(-NaN)).toEqual(
39 t.binaryExpression("/", t.numericLiteral(0), t.numericLiteral(0)),
40 );
41
42 expect(t.valueToNode(Infinity)).toEqual(
43 t.binaryExpression("/", t.numericLiteral(1), t.numericLiteral(0)),
44 );
45 expect(t.valueToNode(-Infinity)).toEqual(
46 t.unaryExpression(
47 "-",
48 t.binaryExpression("/", t.numericLiteral(1), t.numericLiteral(0)),
49 ),
50 );
51 });
52 it("string", function () {
53 expect(t.valueToNode('This is a "string"')).toEqual(
54 t.stringLiteral('This is a "string"'),
55 );
56 });
57 it("boolean", function () {
58 expect(t.valueToNode(true)).toEqual(t.booleanLiteral(true));
59 expect(t.valueToNode(false)).toEqual(t.booleanLiteral(false));
60 });
61 it("null", function () {
62 expect(t.valueToNode(null)).toEqual(t.nullLiteral());
63 });
64 it("undefined", function () {
65 expect(t.valueToNode(undefined)).toEqual(t.identifier("undefined"));
66 });
67 it("RegExp", function () {
68 expect(t.valueToNode(/abc.+/gm)).toEqual(t.regExpLiteral("abc.+", "gm"));
69 });
70 it("array", function () {
71 expect(t.valueToNode([1, "a"])).toEqual(
72 t.arrayExpression([t.numericLiteral(1), t.stringLiteral("a")]),
73 );
74 });
75 it("object", function () {
76 expect(
77 t.valueToNode({
78 a: 1,
79 "b c": 2,
80 }),
81 ).toEqual(
82 t.objectExpression([
83 t.objectProperty(t.identifier("a"), t.numericLiteral(1)),
84 t.objectProperty(t.stringLiteral("b c"), t.numericLiteral(2)),
85 ]),
86 );
87 });
88 it("throws if cannot convert", function () {
89 expect(function () {
90 t.valueToNode(Object);
91 }).toThrow();
92 expect(function () {
93 t.valueToNode(Symbol());
94 }).toThrow();
95 });
96 });
97 describe("toKeyAlias", function () {
98 beforeEach(function () {
99 // make tests deterministic
100 t.toKeyAlias.uid = 0;
101 });
102 it("doesn't change string literals", function () {
103 expect(
104 t.toKeyAlias(t.objectProperty(t.stringLiteral("a"), t.nullLiteral())),
105 ).toBe('"a"');
106 });
107 it("wraps around at Number.MAX_SAFE_INTEGER", function () {
108 expect(
109 t.toKeyAlias(
110 t.objectMethod("method", t.identifier("a"), [], t.blockStatement([])),
111 ),
112 ).toBe("0");
113 });
114 });
115 describe("toStatement", function () {
116 it("noop on statements", function () {
117 const node = t.emptyStatement();
118 expect(t.toStatement(node)).toEqual(node);
119 t.assertEmptyStatement(node);
120 });
121 it("mutate class expression to declaration", function () {
122 const node = t.classExpression(
123 t.identifier("A"),
124 null,
125 t.classBody([]),
126 [],
127 );
128 t.toStatement(node);
129 t.assertClassDeclaration(node);
130 });
131 it("fail if class expression has no id", function () {
132 const node = t.classExpression(null, null, t.classBody([]), []);
133 expect(function () {
134 t.toStatement(node);
135 }).toThrow(Error);
136 expect(t.toStatement(node, /* ignore = */ true)).toBe(false);
137 t.assertClassExpression(node);
138 });
139 it("mutate function expression to declaration", function () {
140 const node = t.functionExpression(
141 t.identifier("A"),
142 [],
143 t.blockStatement([]),
144 );
145 t.toStatement(node);
146 t.assertFunctionDeclaration(node);
147 });
148 it("fail if function expression has no id", function () {
149 const node = t.functionExpression(null, [], t.blockStatement([]));
150 expect(function () {
151 t.toStatement(node);
152 }).toThrow(Error);
153 expect(t.toStatement(node, /* ignore = */ true)).toBe(false);
154 t.assertFunctionExpression(node);
155 });
156 it("assignment expression", function () {
157 const node = t.assignmentExpression(
158 "+=",
159 t.identifier("x"),
160 t.numericLiteral(1),
161 );
162 t.assertExpressionStatement(t.toStatement(node));
163 t.assertAssignmentExpression(node);
164 });
165 it("fail if cannot convert node type", function () {
166 const node = t.yieldExpression(t.identifier("foo"));
167 expect(function () {
168 t.toStatement(node);
169 }).toThrow(Error);
170 expect(t.toStatement(node, /* ignore = */ true)).toBe(false);
171 t.assertYieldExpression(node);
172 });
173 });
174 describe("toExpression", function () {
175 it("noop on expressions", function () {
176 const node = t.identifier("a");
177 expect(t.toExpression(node)).toEqual(node);
178 t.assertIdentifier(node);
179 });
180 it("mutate class declaration to expression", function () {
181 const node = t.classDeclaration(
182 t.identifier("A"),
183 null,
184 t.classBody([]),
185 [],
186 );
187 t.toExpression(node);
188 t.assertClassExpression(node);
189 });
190 it("mutate function declaration to expression", function () {
191 const node = t.functionDeclaration(
192 t.identifier("A"),
193 [],
194 t.blockStatement([]),
195 );
196 t.toExpression(node);
197 t.assertFunctionExpression(node);
198 });
199 it("mutate object method to expression", function () {
200 const node = t.objectMethod(
201 "method",
202 t.identifier("A"),
203 [],
204 t.blockStatement([]),
205 );
206 t.toExpression(node);
207 t.assertFunctionExpression(node);
208 });
209 it("mutate class method to expression", function () {
210 const node = t.classMethod(
211 "constructor",
212 t.identifier("A"),
213 [],
214 t.blockStatement([]),
215 );
216 t.toExpression(node);
217 t.assertFunctionExpression(node);
218 });
219 it("expression statement", function () {
220 const inner = t.yieldExpression(t.identifier("foo"));
221 const node = t.expressionStatement(inner);
222 t.assertYieldExpression(t.toExpression(node));
223 expect(t.toExpression(node)).toEqual(inner);
224 t.assertExpressionStatement(node);
225 });
226 it("fail if cannot convert node type", function () {
227 const node = t.program([]);
228 expect(function () {
229 t.toExpression(node);
230 }).toThrow(Error);
231 t.assertProgram(node);
232 });
233 });
234 describe("toSequenceExpression", function () {
235 let scope;
236 const undefinedNode = t.identifier("undefined");
237 beforeEach(function () {
238 scope = [];
239 scope.buildUndefinedNode = function () {
240 return undefinedNode;
241 };
242 });
243 it("gathers nodes into sequence", function () {
244 const node = t.identifier("a");
245 const sequence = t.toSequenceExpression([undefinedNode, node], scope);
246 t.assertSequenceExpression(sequence);
247 expect(sequence.expressions[0]).toBe(undefinedNode);
248 expect(sequence.expressions[1]).toBe(node);
249 t.assertIdentifier(node);
250 });
251 it("avoids sequence for single node", function () {
252 const node = t.identifier("a");
253 let sequence = t.toSequenceExpression([node], scope);
254 expect(sequence).toBe(node);
255
256 const block = t.blockStatement([t.expressionStatement(node)]);
257 sequence = t.toSequenceExpression([block], scope);
258 expect(sequence).toBe(node);
259 });
260 it("gathers expression", function () {
261 const node = t.identifier("a");
262 const sequence = t.toSequenceExpression([undefinedNode, node], scope);
263 expect(sequence.expressions[1]).toBe(node);
264 });
265 it("gathers expression statement", function () {
266 const node = t.expressionStatement(t.identifier("a"));
267 const sequence = t.toSequenceExpression([undefinedNode, node], scope);
268 expect(sequence.expressions[1]).toBe(node.expression);
269 });
270 it("gathers var declarations", function () {
271 const node = parseCode("var a, b = 1;");
272 const sequence = t.toSequenceExpression([undefinedNode, node], scope);
273 t.assertIdentifier(scope[0].id, { name: "a" });
274 t.assertIdentifier(scope[1].id, { name: "b" });
275 expect(generateCode(sequence.expressions[1])).toBe("b = 1");
276 expect(generateCode(sequence.expressions[2])).toBe("undefined");
277 });
278 it("skips undefined if expression after var declaration", function () {
279 const node = parseCode("{ var a, b = 1; true }");
280 const sequence = t.toSequenceExpression([undefinedNode, node], scope);
281 expect(generateCode(sequence.expressions[1])).toBe("b = 1, true");
282 });
283 it("bails on let and const declarations", function () {
284 let node = parseCode("let a, b = 1;");
285 let sequence = t.toSequenceExpression([undefinedNode, node], scope);
286 expect(sequence).toBeUndefined();
287
288 node = parseCode("const b = 1;");
289 sequence = t.toSequenceExpression([undefinedNode, node], scope);
290 expect(sequence).toBeUndefined();
291 });
292 it("gathers if statements", function () {
293 let node = parseCode("if (true) { true }");
294 let sequence = t.toSequenceExpression([undefinedNode, node], scope);
295 expect(generateCode(sequence.expressions[1])).toBe(
296 "true ? true : undefined",
297 );
298
299 node = parseCode("if (true) { true } else { b }");
300 sequence = t.toSequenceExpression([undefinedNode, node], scope);
301 expect(generateCode(sequence.expressions[1])).toBe("true ? true : b");
302 });
303 it("bails in if statements if recurse bails", function () {
304 let node = parseCode("if (true) { return }");
305 let sequence = t.toSequenceExpression([undefinedNode, node], scope);
306 expect(sequence).toBeUndefined();
307
308 node = parseCode("if (true) { true } else { return }");
309 sequence = t.toSequenceExpression([undefinedNode, node], scope);
310 expect(sequence).toBeUndefined();
311 });
312 it("gathers block statements", function () {
313 let node = parseCode("{ a }");
314 let sequence = t.toSequenceExpression([undefinedNode, node], scope);
315 expect(generateCode(sequence.expressions[1])).toBe("a");
316
317 node = parseCode("{ a; b; }");
318 sequence = t.toSequenceExpression([undefinedNode, node], scope);
319 expect(generateCode(sequence.expressions[1])).toBe("a, b");
320 });
321 it("bails in block statements if recurse bails", function () {
322 const node = parseCode("{ return }");
323 const sequence = t.toSequenceExpression([undefinedNode, node], scope);
324 expect(sequence).toBeUndefined();
325 });
326 it("gathers empty statements if first element", function () {
327 const node = parseCode(";");
328 const sequence = t.toSequenceExpression([undefinedNode, node], scope);
329 expect(generateCode(sequence)).toBe("undefined");
330 });
331 it("skips empty statement if expression afterwards", function () {
332 const node = parseCode("{ ; true }");
333 const sequence = t.toSequenceExpression([undefinedNode, node], scope);
334 expect(generateCode(sequence.expressions[1])).toBe("true");
335 });
336 });
337 });