* Return inserted/replaced paths This gives `Path`’s replacement and insertion methods a consistent return value: the inserted/replaced paths. Before, they could return `undefined`, a `node`, or a the current path inside an array. It was kinda pointless. But now they always return an array of paths, which is useful for solving https://github.com/babel/babel/pull/4935#discussion_r96151368. * Return inserted nodes and not BlockStatement Addded test for bug #4363 * Cleanups - `#replaceWith` will now return the current path if it's the same node - `#insertAfter` and `#insertBefore` use public Path APIs now - Makes container insertion faster (single splice call) - Use public APIs in container insertion - Replacing a statement with an expression returns the expression's path - Replacing an expression with multiple statements returns the inserted closure's body's paths.
162 lines
5.8 KiB
JavaScript
162 lines
5.8 KiB
JavaScript
import traverse from "../lib";
|
|
import assert from "assert";
|
|
import { parse } from "babylon";
|
|
import generate from "babel-generator";
|
|
import * as t from "babel-types";
|
|
|
|
function getPath(code) {
|
|
const ast = parse(code);
|
|
let path;
|
|
traverse(ast, {
|
|
Program: function(_path) {
|
|
path = _path.get("body.0");
|
|
_path.stop();
|
|
},
|
|
});
|
|
|
|
return path;
|
|
}
|
|
|
|
function generateCode(path) {
|
|
return generate(path.parentPath.node).code;
|
|
}
|
|
|
|
describe("modification", function() {
|
|
describe("pushContainer", function() {
|
|
it("pushes identifier into params", function() {
|
|
const rootPath = getPath("function test(a) {}");
|
|
rootPath.pushContainer("params", t.identifier("b"));
|
|
|
|
assert.equal(generateCode(rootPath), "function test(a, b) {}");
|
|
});
|
|
|
|
it("pushes identifier into block", function() {
|
|
const rootPath = getPath("function test(a) {}");
|
|
const path = rootPath.get("body");
|
|
path.pushContainer("body", t.expressionStatement(t.identifier("b")));
|
|
|
|
assert.equal(generateCode(rootPath), "function test(a) {\n b;\n}");
|
|
});
|
|
});
|
|
describe("unshiftContainer", function() {
|
|
it("unshifts identifier into params", function() {
|
|
const rootPath = getPath("function test(a) {}");
|
|
rootPath.unshiftContainer("params", t.identifier("b"));
|
|
|
|
assert.equal(generateCode(rootPath), "function test(b, a) {}");
|
|
});
|
|
|
|
it("unshifts identifier into block", function() {
|
|
const rootPath = getPath("function test(a) {}");
|
|
const path = rootPath.get("body");
|
|
path.unshiftContainer("body", t.expressionStatement(t.identifier("b")));
|
|
|
|
assert.equal(generateCode(rootPath), "function test(a) {\n b;\n}");
|
|
});
|
|
});
|
|
|
|
describe("insertBefore", function() {
|
|
it("returns inserted path with BlockStatement", function() {
|
|
const rootPath = getPath("if (x) { y; }");
|
|
const path = rootPath.get("consequent.body.0");
|
|
const result = path.insertBefore(t.identifier("b"));
|
|
|
|
assert.equal(Array.isArray(result), true);
|
|
assert.equal(result.length, 1);
|
|
assert.deepEqual(result[0].node, t.identifier("b"));
|
|
assert.equal(generateCode(rootPath), "if (x) {\n b\n y;\n}");
|
|
});
|
|
|
|
it("returns inserted path without BlockStatement", function() {
|
|
const rootPath = getPath("if (x) y;");
|
|
const path = rootPath.get("consequent");
|
|
const result = path.insertBefore(t.identifier("b"));
|
|
|
|
assert.equal(Array.isArray(result), true);
|
|
assert.equal(result.length, 1);
|
|
assert.deepEqual(result[0].node, t.identifier("b"));
|
|
assert.equal(generateCode(rootPath), "if (x) {\n b\n y;\n}");
|
|
});
|
|
|
|
it("returns inserted path without BlockStatement without ExpressionStatement", function() {
|
|
const rootPath = getPath("if (x) for (var i = 0; i < 0; i++) {}");
|
|
const path = rootPath.get("consequent");
|
|
const result = path.insertBefore(t.identifier("b"));
|
|
|
|
assert.equal(Array.isArray(result), true);
|
|
assert.equal(result.length, 1);
|
|
assert.deepEqual(result[result.length - 1].node, t.identifier("b"));
|
|
assert.equal(
|
|
generateCode(rootPath),
|
|
"if (x) {\n b\n\n for (var i = 0; i < 0; i++) {}\n}",
|
|
);
|
|
});
|
|
|
|
it("returns inserted path with BlockStatement without ExpressionStatement", function() {
|
|
const rootPath = getPath("if (x) { for (var i = 0; i < 0; i++) {} }");
|
|
const path = rootPath.get("consequent.body.0");
|
|
const result = path.insertBefore(t.identifier("b"));
|
|
|
|
assert.equal(Array.isArray(result), true);
|
|
assert.equal(result.length, 1);
|
|
assert.deepEqual(result[result.length - 1].node, t.identifier("b"));
|
|
assert.equal(
|
|
generateCode(rootPath),
|
|
"if (x) {\n b\n\n for (var i = 0; i < 0; i++) {}\n}",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("insertAfter", function() {
|
|
it("returns inserted path with BlockStatement with ExpressionStatement", function() {
|
|
const rootPath = getPath("if (x) { y; }");
|
|
const path = rootPath.get("consequent.body.0");
|
|
const result = path.insertAfter(t.identifier("b"));
|
|
|
|
assert.equal(Array.isArray(result), true);
|
|
assert.equal(result.length, 1);
|
|
assert.deepEqual(result[result.length - 1].node, t.identifier("b"));
|
|
assert.equal(generateCode(rootPath), "if (x) {\n y;\n b\n}");
|
|
});
|
|
|
|
it("returns inserted path without BlockStatement with ExpressionStatement", function() {
|
|
const rootPath = getPath("if (x) y;");
|
|
const path = rootPath.get("consequent");
|
|
const result = path.insertAfter(t.identifier("b"));
|
|
|
|
assert.equal(Array.isArray(result), true);
|
|
assert.equal(result.length, 1);
|
|
assert.deepEqual(result[result.length - 1].node, t.identifier("b"));
|
|
assert.equal(generateCode(rootPath), "if (x) {\n y;\n b\n}");
|
|
});
|
|
|
|
it("returns inserted path without BlockStatement without ExpressionStatement", function() {
|
|
const rootPath = getPath("if (x) for (var i = 0; i < 0; i++) {}");
|
|
const path = rootPath.get("consequent");
|
|
const result = path.insertAfter(t.identifier("b"));
|
|
|
|
assert.equal(Array.isArray(result), true);
|
|
assert.equal(result.length, 1);
|
|
assert.deepEqual(result[result.length - 1].node, t.identifier("b"));
|
|
assert.equal(
|
|
generateCode(rootPath),
|
|
"if (x) {\n for (var i = 0; i < 0; i++) {}\n\n b\n}",
|
|
);
|
|
});
|
|
|
|
it("returns inserted path with BlockStatement without ExpressionStatement", function() {
|
|
const rootPath = getPath("if (x) { for (var i = 0; i < 0; i++) {} }");
|
|
const path = rootPath.get("consequent.body.0");
|
|
const result = path.insertAfter(t.identifier("b"));
|
|
|
|
assert.equal(Array.isArray(result), true);
|
|
assert.equal(result.length, 1);
|
|
assert.deepEqual(result[result.length - 1].node, t.identifier("b"));
|
|
assert.equal(
|
|
generateCode(rootPath),
|
|
"if (x) {\n for (var i = 0; i < 0; i++) {}\n\n b\n}",
|
|
);
|
|
});
|
|
});
|
|
});
|