test: add tests about behaviour of replaceWithMultiple (#12309)

* test: add tests about behaviour of replaceWithMultiple

* add more tests
This commit is contained in:
Huáng Jùnliàng 2020-11-04 14:46:53 -05:00 committed by GitHub
parent ddf30ee233
commit 6cb6f9f5ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,6 +97,21 @@ describe("path/replacement", function () {
/You passed `path\.replaceWith\(\)` a falsy node, use `path\.remove\(\)` instead/, /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", () => { describe("replaceWithMultiple", () => {
it("does not add extra parentheses for a JSXElement with a JSXElement parent", () => { it("does not add extra parentheses for a JSXElement with a JSXElement parent", () => {
@ -112,5 +127,35 @@ describe("path/replacement", function () {
}); });
expect(generate(ast).code).toBe("<div><p></p><h></h></div>;"); 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);
});
}); });
}); });