Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests about behaviour of replaceWithMultiple #12309

Merged
merged 2 commits into from Nov 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});
});