| 1 | import { parse } from "@babel/parser"; |
| 2 | import * as t from "@babel/types"; |
| 3 | |
| 4 | import _traverse from "../lib/index.js"; |
| 5 | import _generate from "@babel/generator"; |
| 6 | const traverse = _traverse.default || _traverse; |
| 7 | const generate = _generate.default || _generate; |
| 8 | |
| 9 | function getPath(code, parserOpts) { |
| 10 | const ast = parse(code, parserOpts); |
| 11 | let path; |
| 12 | traverse(ast, { |
| 13 | Program: function (_path) { |
| 14 | path = _path.get("body.0"); |
| 15 | _path.stop(); |
| 16 | }, |
| 17 | }); |
| 18 | |
| 19 | return path; |
| 20 | } |
| 21 | |
| 22 | function generateCode(path) { |
| 23 | return generate(path.parentPath.node).code; |
| 24 | } |
| 25 | |
| 26 | describe("modification", function () { |
| 27 | describe("pushContainer", function () { |
| 28 | it("pushes identifier into params", function () { |
| 29 | const rootPath = getPath("function test(a) {}"); |
| 30 | rootPath.pushContainer("params", t.identifier("b")); |
| 31 | |
| 32 | expect(generateCode(rootPath)).toBe("function test(a, b) {}"); |
| 33 | }); |
| 34 | |
| 35 | it("pushes identifier into block", function () { |
| 36 | const rootPath = getPath("function test(a) {}"); |
| 37 | const path = rootPath.get("body"); |
| 38 | path.pushContainer("body", t.expressionStatement(t.identifier("b"))); |
| 39 | |
| 40 | expect(generateCode(rootPath)).toBe("function test(a) {\n b;\n}"); |
| 41 | }); |
| 42 | |
| 43 | it("properly handles more than one arguments", function () { |
| 44 | const code = "foo(a, b);"; |
| 45 | const ast = parse(code); |
| 46 | traverse(ast, { |
| 47 | CallExpression: function (path) { |
| 48 | path.pushContainer("arguments", t.identifier("d")); |
| 49 | expect(generateCode(path)).toBe("foo(a, b, d);"); |
| 50 | path.pushContainer("arguments", t.stringLiteral("s")); |
| 51 | expect(generateCode(path)).toBe(`foo(a, b, d, "s");`); |
| 52 | }, |
| 53 | }); |
| 54 | }); |
| 55 | |
| 56 | it("should set the correct path.context", function () { |
| 57 | expect.assertions(2); |
| 58 | |
| 59 | const ast = parse("[b];"); |
| 60 | traverse(ast, { |
| 61 | skipKeys: ["consequent"], |
| 62 | ExpressionStatement(path) { |
| 63 | path.traverse({ Identifier() {}, skipKeys: [] }); |
| 64 | |
| 65 | const arr = path.get("expression"); |
| 66 | const x = arr.pushContainer("elements", [ |
| 67 | { type: "Identifier", name: "x" }, |
| 68 | ])[0]; |
| 69 | |
| 70 | expect(x.node.name).toBe("x"); |
| 71 | expect(x.opts.skipKeys).toEqual(["consequent"]); |
| 72 | }, |
| 73 | }); |
| 74 | }); |
| 75 | }); |
| 76 | describe("unshiftContainer", function () { |
| 77 | it("unshifts identifier into params", function () { |
| 78 | const rootPath = getPath("function test(a) {}"); |
| 79 | rootPath.unshiftContainer("params", t.identifier("b")); |
| 80 | |
| 81 | expect(generateCode(rootPath)).toBe("function test(b, a) {}"); |
| 82 | }); |
| 83 | |
| 84 | it("unshifts identifier into block", function () { |
| 85 | const rootPath = getPath("function test(a) {}"); |
| 86 | const path = rootPath.get("body"); |
| 87 | path.unshiftContainer("body", t.expressionStatement(t.identifier("b"))); |
| 88 | |
| 89 | expect(generateCode(rootPath)).toBe("function test(a) {\n b;\n}"); |
| 90 | }); |
| 91 | |
| 92 | it("properly handles more than one arguments", function () { |
| 93 | const code = "foo(a, b);"; |
| 94 | const ast = parse(code); |
| 95 | traverse(ast, { |
| 96 | CallExpression: function (path) { |
| 97 | path.unshiftContainer("arguments", t.identifier("d")); |
| 98 | expect(generateCode(path)).toBe("foo(d, a, b);"); |
| 99 | path.unshiftContainer("arguments", t.stringLiteral("s")); |
| 100 | expect(generateCode(path)).toBe(`foo("s", d, a, b);`); |
| 101 | }, |
| 102 | }); |
| 103 | }); |
| 104 | |
| 105 | it("should set the correct path.context", function () { |
| 106 | expect.assertions(2); |
| 107 | |
| 108 | const ast = parse("[b];"); |
| 109 | traverse(ast, { |
| 110 | skipKeys: ["consequent"], |
| 111 | ExpressionStatement(path) { |
| 112 | path.traverse({ Identifier() {}, skipKeys: [] }); |
| 113 | |
| 114 | const arr = path.get("expression"); |
| 115 | const x = arr.unshiftContainer("elements", [ |
| 116 | { type: "Identifier", name: "x" }, |
| 117 | ])[0]; |
| 118 | |
| 119 | expect(x.node.name).toBe("x"); |
| 120 | expect(x.opts.skipKeys).toEqual(["consequent"]); |
| 121 | }, |
| 122 | }); |
| 123 | }); |
| 124 | }); |
| 125 | |
| 126 | describe("insertBefore", function () { |
| 127 | it("returns inserted path with BlockStatement", function () { |
| 128 | const rootPath = getPath("if (x) { y; }"); |
| 129 | const path = rootPath.get("consequent.body.0"); |
| 130 | const result = path.insertBefore(t.identifier("b")); |
| 131 | |
| 132 | expect(Array.isArray(result)).toBe(true); |
| 133 | expect(result).toHaveLength(1); |
| 134 | expect(result[0].node).toEqual(t.identifier("b")); |
| 135 | expect(generateCode(rootPath)).toBe("if (x) {\n b\n y;\n}"); |
| 136 | }); |
| 137 | |
| 138 | it("returns inserted path without BlockStatement", function () { |
| 139 | const rootPath = getPath("if (x) y;"); |
| 140 | const path = rootPath.get("consequent"); |
| 141 | const result = path.insertBefore(t.identifier("b")); |
| 142 | |
| 143 | expect(Array.isArray(result)).toBe(true); |
| 144 | expect(result).toHaveLength(1); |
| 145 | expect(result[0].node).toEqual(t.identifier("b")); |
| 146 | expect(generateCode(rootPath)).toBe("if (x) {\n b\n y;\n}"); |
| 147 | }); |
| 148 | |
| 149 | it("returns inserted path without BlockStatement without ExpressionStatement", function () { |
| 150 | const rootPath = getPath("if (x) for (var i = 0; i < 0; i++) {}"); |
| 151 | const path = rootPath.get("consequent"); |
| 152 | const result = path.insertBefore(t.identifier("b")); |
| 153 | |
| 154 | expect(Array.isArray(result)).toBe(true); |
| 155 | expect(result).toHaveLength(1); |
| 156 | expect(result[result.length - 1].node).toEqual(t.identifier("b")); |
| 157 | expect(generateCode(rootPath)).toBe( |
| 158 | "if (x) {\n b\n for (var i = 0; i < 0; i++) {}\n}", |
| 159 | ); |
| 160 | }); |
| 161 | |
| 162 | it("returns inserted path with BlockStatement without ExpressionStatement", function () { |
| 163 | const rootPath = getPath("if (x) { for (var i = 0; i < 0; i++) {} }"); |
| 164 | const path = rootPath.get("consequent.body.0"); |
| 165 | const result = path.insertBefore(t.identifier("b")); |
| 166 | |
| 167 | expect(Array.isArray(result)).toBe(true); |
| 168 | expect(result).toHaveLength(1); |
| 169 | expect(result[result.length - 1].node).toEqual(t.identifier("b")); |
| 170 | expect(generateCode(rootPath)).toBe( |
| 171 | "if (x) {\n b\n for (var i = 0; i < 0; i++) {}\n}", |
| 172 | ); |
| 173 | }); |
| 174 | |
| 175 | it("returns inserted path with nested JSXElement", function () { |
| 176 | const ast = parse("<div><span>foo</span></div>", { |
| 177 | plugins: ["jsx"], |
| 178 | }); |
| 179 | let path; |
| 180 | traverse(ast, { |
| 181 | Program: function (_path) { |
| 182 | path = _path.get("body.0"); |
| 183 | }, |
| 184 | JSXElement: function (path) { |
| 185 | const tagName = path.node.openingElement.name.name; |
| 186 | if (tagName !== "span") return; |
| 187 | path.insertBefore( |
| 188 | t.jsxElement( |
| 189 | t.jsxOpeningElement(t.jsxIdentifier("div"), [], false), |
| 190 | t.jsxClosingElement(t.jsxIdentifier("div")), |
| 191 | [], |
| 192 | ), |
| 193 | ); |
| 194 | }, |
| 195 | }); |
| 196 | expect(generateCode(path)).toBe( |
| 197 | "<div><div></div><span>foo</span></div>;", |
| 198 | ); |
| 199 | }); |
| 200 | |
| 201 | describe("when the parent is an export declaration inserts the node before", function () { |
| 202 | it("the ExportNamedDeclaration", function () { |
| 203 | const bodyPath = getPath("export function a() {}", { |
| 204 | sourceType: "module", |
| 205 | }).parentPath; |
| 206 | const fnPath = bodyPath.get("body.0.declaration"); |
| 207 | fnPath.insertBefore(t.identifier("x")); |
| 208 | |
| 209 | expect(bodyPath.get("body")).toHaveLength(2); |
| 210 | expect(bodyPath.get("body.0").node).toEqual(t.identifier("x")); |
| 211 | }); |
| 212 | |
| 213 | it("the ExportDefaultDeclaration, if a declaration is exported", function () { |
| 214 | const bodyPath = getPath("export default function () {}", { |
| 215 | sourceType: "module", |
| 216 | }).parentPath; |
| 217 | const fnPath = bodyPath.get("body.0.declaration"); |
| 218 | fnPath.insertBefore(t.identifier("x")); |
| 219 | |
| 220 | expect(bodyPath.get("body")).toHaveLength(2); |
| 221 | expect(bodyPath.get("body.0").node).toEqual(t.identifier("x")); |
| 222 | }); |
| 223 | |
| 224 | it("the exported expression", function () { |
| 225 | const declPath = getPath("export default fn();", { |
| 226 | sourceType: "module", |
| 227 | }); |
| 228 | const path = declPath.get("declaration"); |
| 229 | path.insertBefore(t.identifier("x")); |
| 230 | |
| 231 | expect(generateCode(declPath)).toBe("export default (x, fn());"); |
| 232 | }); |
| 233 | }); |
| 234 | }); |
| 235 | |
| 236 | describe("insertAfter", function () { |
| 237 | it("returns inserted path with BlockStatement with ExpressionStatement", function () { |
| 238 | const rootPath = getPath("if (x) { y; }"); |
| 239 | const path = rootPath.get("consequent.body.0"); |
| 240 | const result = path.insertAfter(t.identifier("b")); |
| 241 | |
| 242 | expect(Array.isArray(result)).toBe(true); |
| 243 | expect(result).toHaveLength(1); |
| 244 | expect(result[result.length - 1].node).toEqual(t.identifier("b")); |
| 245 | expect(generateCode(rootPath)).toBe("if (x) {\n y;\n b\n}"); |
| 246 | }); |
| 247 | |
| 248 | it("returns inserted path without BlockStatement with ExpressionStatement", function () { |
| 249 | const rootPath = getPath("if (x) y;"); |
| 250 | const path = rootPath.get("consequent"); |
| 251 | const result = path.insertAfter(t.identifier("b")); |
| 252 | |
| 253 | expect(Array.isArray(result)).toBe(true); |
| 254 | expect(result).toHaveLength(1); |
| 255 | expect(result[result.length - 1].node).toEqual(t.identifier("b")); |
| 256 | expect(generateCode(rootPath)).toBe("if (x) {\n y;\n b\n}"); |
| 257 | }); |
| 258 | |
| 259 | it("returns inserted path without BlockStatement without ExpressionStatement", function () { |
| 260 | const rootPath = getPath("if (x) for (var i = 0; i < 0; i++) {}"); |
| 261 | const path = rootPath.get("consequent"); |
| 262 | const result = path.insertAfter(t.identifier("b")); |
| 263 | |
| 264 | expect(Array.isArray(result)).toBe(true); |
| 265 | expect(result).toHaveLength(1); |
| 266 | expect(result[result.length - 1].node).toEqual(t.identifier("b")); |
| 267 | expect(generateCode(rootPath)).toBe( |
| 268 | "if (x) {\n for (var i = 0; i < 0; i++) {}\n b\n}", |
| 269 | ); |
| 270 | }); |
| 271 | |
| 272 | it("returns inserted path with BlockStatement without ExpressionStatement", function () { |
| 273 | const rootPath = getPath("if (x) { for (var i = 0; i < 0; i++) {} }"); |
| 274 | const path = rootPath.get("consequent.body.0"); |
| 275 | const result = path.insertAfter(t.identifier("b")); |
| 276 | |
| 277 | expect(Array.isArray(result)).toBe(true); |
| 278 | expect(result).toHaveLength(1); |
| 279 | expect(result[result.length - 1].node).toEqual(t.identifier("b")); |
| 280 | expect(generateCode(rootPath)).toBe( |
| 281 | "if (x) {\n for (var i = 0; i < 0; i++) {}\n b\n}", |
| 282 | ); |
| 283 | }); |
| 284 | |
| 285 | it("returns inserted path with nested JSXElement", function () { |
| 286 | const ast = parse("<div><span>foo</span></div>", { |
| 287 | plugins: ["jsx"], |
| 288 | }); |
| 289 | let path; |
| 290 | traverse(ast, { |
| 291 | Program: function (_path) { |
| 292 | path = _path.get("body.0"); |
| 293 | }, |
| 294 | JSXElement: function (path) { |
| 295 | const tagName = path.node.openingElement.name.name; |
| 296 | if (tagName !== "span") return; |
| 297 | path.insertAfter( |
| 298 | t.jsxElement( |
| 299 | t.jsxOpeningElement(t.jsxIdentifier("div"), [], false), |
| 300 | t.jsxClosingElement(t.jsxIdentifier("div")), |
| 301 | [], |
| 302 | ), |
| 303 | ); |
| 304 | }, |
| 305 | }); |
| 306 | expect(generateCode(path)).toBe( |
| 307 | "<div><span>foo</span><div></div></div>;", |
| 308 | ); |
| 309 | }); |
| 310 | |
| 311 | describe("when the parent is an export declaration inserts the node after", function () { |
| 312 | it("the ExportNamedDeclaration", function () { |
| 313 | const bodyPath = getPath("export function a() {}", { |
| 314 | sourceType: "module", |
| 315 | }).parentPath; |
| 316 | const fnPath = bodyPath.get("body.0.declaration"); |
| 317 | fnPath.insertAfter(t.identifier("x")); |
| 318 | |
| 319 | expect(bodyPath.get("body")).toHaveLength(2); |
| 320 | expect(bodyPath.get("body.1").node).toEqual( |
| 321 | t.expressionStatement(t.identifier("x")), |
| 322 | ); |
| 323 | }); |
| 324 | |
| 325 | it("the ExportDefaultDeclaration, if a declaration is exported", function () { |
| 326 | const bodyPath = getPath("export default function () {}", { |
| 327 | sourceType: "module", |
| 328 | }).parentPath; |
| 329 | const fnPath = bodyPath.get("body.0.declaration"); |
| 330 | fnPath.insertAfter(t.identifier("x")); |
| 331 | |
| 332 | expect(bodyPath.get("body")).toHaveLength(2); |
| 333 | expect(bodyPath.get("body.1").node).toEqual( |
| 334 | t.expressionStatement(t.identifier("x")), |
| 335 | ); |
| 336 | }); |
| 337 | |
| 338 | it("the exported expression", function () { |
| 339 | const bodyPath = getPath("export default fn();", { |
| 340 | sourceType: "module", |
| 341 | }).parentPath; |
| 342 | const path = bodyPath.get("body.0.declaration"); |
| 343 | path.insertAfter(t.identifier("x")); |
| 344 | |
| 345 | expect(generateCode({ parentPath: bodyPath })).toBe( |
| 346 | "var _temp;\nexport default (_temp = fn(), x, _temp);", |
| 347 | ); |
| 348 | }); |
| 349 | }); |
| 350 | }); |
| 351 | }); |