Debian Codegraph @

packages/babel-plugin-proposal-optional-chaining/test/util.skip-bundled.js

raw
1 import { willPathCastToBoolean } from "../lib/util.js";
2 import { parseSync, traverse } from "@babel/core";
3
4 function getPath(input, parserOpts) {
5 let targetPath;
6 traverse(parseSync(input, { parserOpts, filename: "example.js" }), {
7 OptionalMemberExpression(path) {
8 targetPath = path;
9 path.stop();
10 },
11 noScope: true,
12 });
13 return targetPath;
14 }
15
16 describe("willPathCastToBoolean", () => {
17 const positiveCases = [
18 "if(a?.b) {}",
19 "while(a?.b) {}",
20 "a?.b ? 0 : 1",
21 "for(;a?.b;);",
22 "while(a?.b);",
23 "do {} while (a?.b)",
24 "!a?.b",
25 "if(a && a?.b) {}",
26 "if(a?.b && a) {}",
27 "while(a || a?.b) {}",
28 "while(a?.b || a) {}",
29 "for(; a ?? a?.b ;);",
30 "do {} while (0, a?.b)",
31 "(a?.b, 0)",
32 "!(a && ( b || ( c ?? ( a?.b && d ) ) ) )",
33
34 // parenthesized
35 "!((a?.b), 0)",
36 "((a?.b)) ? 0 : 1",
37 "while( (a || ((a?.b) && c) ) );",
38 ];
39
40 const negativeCases = [
41 "() => a?.b",
42 "for(; a?.b ?? a;);",
43 "a?.b && a",
44 "a && a?.b",
45 "() => a?.b || a",
46 "() => a || a?.b",
47 "a?.b()",
48 "a = a?.b && null",
49
50 // parenthesized
51 "(a?.b)",
52 ];
53
54 describe("default parser options", () => {
55 test.each(positiveCases)(
56 "willPathCastToBoolean(a?.b in %p) should return true",
57 input => {
58 expect(willPathCastToBoolean(getPath(input))).toBe(true);
59 },
60 );
61 test.each(negativeCases)(
62 "willPathCastToBoolean(a?.b in %p) should return false",
63 input => {
64 expect(willPathCastToBoolean(getPath(input))).toBe(false);
65 },
66 );
67 });
68
69 describe("createParenthesizedExpressions", () => {
70 test.each(positiveCases)(
71 "willPathCastToBoolean(a?.b in %p with { createParenthesizedExpressions: true }) should return true",
72 input => {
73 const parserOpts = { createParenthesizedExpressions: true };
74 expect(willPathCastToBoolean(getPath(input, parserOpts))).toBe(true);
75 },
76 );
77 test.each(negativeCases)(
78 "willPathCastToBoolean(a?.b in %p with { createParenthesizedExpressions: true }) should return false",
79 input => {
80 const parserOpts = { createParenthesizedExpressions: true };
81 expect(willPathCastToBoolean(getPath(input, parserOpts))).toBe(false);
82 },
83 );
84 });
85 });