Debian Codegraph @

packages/babel-core/test/path.js

raw
1 import { transformSync } from "../lib/index.js";
2 import { fileURLToPath } from "url";
3 import path from "path";
4
5 import _Plugin from "../lib/config/plugin.js";
6 const Plugin = _Plugin.default || _Plugin;
7
8 const cwd = path.dirname(fileURLToPath(import.meta.url));
9
10 describe("traversal path", function () {
11 it("replaceWithSourceString", function () {
12 const expectCode = "function foo() {}";
13
14 const actualCode = transformSync(expectCode, {
15 cwd,
16 plugins: [
17 new Plugin({
18 visitor: {
19 FunctionDeclaration: function (path) {
20 path.replaceWithSourceString("console.whatever()");
21 },
22 },
23 }),
24 ],
25 }).code;
26
27 expect(actualCode).toBe("console.whatever();");
28 });
29
30 it("replaceWith (arrow expression body to block statement body)", function () {
31 const expectCode = "var fn = () => true;";
32
33 const actualCode = transformSync(expectCode, {
34 cwd,
35 plugins: [
36 new Plugin({
37 visitor: {
38 ArrowFunctionExpression: function (path) {
39 path.get("body").replaceWith({
40 type: "BlockStatement",
41 body: [
42 {
43 type: "ReturnStatement",
44 argument: {
45 type: "BooleanLiteral",
46 value: true,
47 },
48 },
49 ],
50 });
51 },
52 },
53 }),
54 ],
55 }).code;
56
57 expect(actualCode).toBe("var fn = () => {\n return true;\n};");
58 });
59
60 it("replaceWith (arrow block statement body to expression body)", function () {
61 const expectCode = "var fn = () => { return true; }";
62
63 const actualCode = transformSync(expectCode, {
64 cwd,
65 plugins: [
66 new Plugin({
67 visitor: {
68 ArrowFunctionExpression: function (path) {
69 path.get("body").replaceWith({
70 type: "BooleanLiteral",
71 value: true,
72 });
73 },
74 },
75 }),
76 ],
77 }).code;
78
79 expect(actualCode).toBe("var fn = () => true;");
80 });
81
82 it("replaceWith (for-in left expression to variable declaration)", function () {
83 const expectCode = "for (KEY in right);";
84
85 const actualCode = transformSync(expectCode, {
86 cwd,
87 plugins: [
88 new Plugin({
89 visitor: {
90 ForInStatement: function (path) {
91 path.get("left").replaceWith({
92 type: "VariableDeclaration",
93 kind: "var",
94 declarations: [
95 {
96 type: "VariableDeclarator",
97 id: {
98 type: "Identifier",
99 name: "KEY",
100 },
101 },
102 ],
103 });
104 },
105 },
106 }),
107 ],
108 }).code;
109
110 expect(actualCode).toBe("for (var KEY in right);");
111 });
112
113 it("replaceWith (for-in left variable declaration to expression)", function () {
114 const expectCode = "for (var KEY in right);";
115
116 const actualCode = transformSync(expectCode, {
117 cwd,
118 plugins: [
119 new Plugin({
120 visitor: {
121 ForInStatement: function (path) {
122 path.get("left").replaceWith({
123 type: "Identifier",
124 name: "KEY",
125 });
126 },
127 },
128 }),
129 ],
130 }).code;
131
132 expect(actualCode).toBe("for (KEY in right);");
133 });
134
135 it("replaceWith (for-loop left expression to variable declaration)", function () {
136 const expectCode = "for (KEY;;);";
137
138 const actualCode = transformSync(expectCode, {
139 cwd,
140 plugins: [
141 new Plugin({
142 visitor: {
143 ForStatement: function (path) {
144 path.get("init").replaceWith({
145 type: "VariableDeclaration",
146 kind: "var",
147 declarations: [
148 {
149 type: "VariableDeclarator",
150 id: {
151 type: "Identifier",
152 name: "KEY",
153 },
154 },
155 ],
156 });
157 },
158 },
159 }),
160 ],
161 }).code;
162
163 expect(actualCode).toBe("for (var KEY;;);");
164 });
165
166 it("replaceWith (for-loop left variable declaration to expression)", function () {
167 const expectCode = "for (var KEY;;);";
168
169 const actualCode = transformSync(expectCode, {
170 cwd,
171 plugins: [
172 new Plugin({
173 visitor: {
174 ForStatement: function (path) {
175 path.get("init").replaceWith({
176 type: "Identifier",
177 name: "KEY",
178 });
179 },
180 },
181 }),
182 ],
183 }).code;
184
185 expect(actualCode).toBe("for (KEY;;);");
186 });
187 });