Skip to content

Commit

Permalink
test: add tests about behaviour of replaceWithMultiple (#12309)
Browse files Browse the repository at this point in the history
* test: add tests about behaviour of replaceWithMultiple

* add more tests
  • Loading branch information
JLHwung committed Nov 4, 2020
1 parent ddf30ee commit 6cb6f9f
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/babel-traverse/test/replacement.js
Expand Up @@ -97,6 +97,21 @@ describe("path/replacement", function () {
/You passed `path\.replaceWith\(\)` a falsy node, use `path\.remove\(\)` instead/,
);
});

it("does not revisit the replaced node if it is the node being replaced", () => {
const ast = parse(`var x;`);
let visitCounter = 0;
traverse(ast, {
VariableDeclaration(path) {
visitCounter++;
if (visitCounter > 1) {
return true;
}
path.replaceWith(path.node);
},
});
expect(visitCounter).toBe(1);
});
});
describe("replaceWithMultiple", () => {
it("does not add extra parentheses for a JSXElement with a JSXElement parent", () => {
Expand All @@ -112,5 +127,35 @@ describe("path/replacement", function () {
});
expect(generate(ast).code).toBe("<div><p></p><h></h></div>;");
});
it("does not revisit one of new nodes if it is the node being replaced and is the head of nodes", () => {
// packages/babel-plugin-transform-block-scoping/src/index.js relies on this behaviour
const ast = parse(`var x;`);
let visitCounter = 0;
traverse(ast, {
VariableDeclaration(path) {
visitCounter++;
if (visitCounter > 1) {
return true;
}
path.replaceWithMultiple([path.node, t.emptyStatement()]);
},
});
expect(visitCounter).toBe(1);
});
it("does not revisit one of new nodes if it is the node being replaced and is the tail of nodes", () => {
// packages/babel-plugin-transform-block-scoping/src/index.js relies on this behaviour
const ast = parse(`var x;`);
let visitCounter = 0;
traverse(ast, {
VariableDeclaration(path) {
visitCounter++;
if (visitCounter > 1) {
return true;
}
path.replaceWithMultiple([t.emptyStatement(), path.node]);
},
});
expect(visitCounter).toBe(1);
});
});
});

0 comments on commit 6cb6f9f

Please sign in to comment.