Skip to content

Commit

Permalink
test: add replaceWith test to traverse
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Nov 24, 2020
1 parent 9b91f0f commit fc63612
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/babel-traverse/test/replacement.js
Expand Up @@ -112,6 +112,33 @@ describe("path/replacement", function () {
});
expect(visitCounter).toBe(1);
});

// https://github.com/babel/babel/issues/12386
it("updates pathCache with the replaced node", () => {
const ast = parse(`() => (a?.b)?.c`, {
createParenthesizedExpressions: true,
});
traverse(ast, {
OptionalMemberExpression(path) {
path.node.type = "MemberExpression";
path.replaceWith(path.node.__clone());
path.parentPath.ensureBlock();

const aQuestionDotB = path.get("object").get("expression");
const aQuestionDotBNode = aQuestionDotB.node;
aQuestionDotBNode.type = "MemberExpression";
aQuestionDotB.replaceWith(aQuestionDotBNode.__clone());
},
ParenthesizedExpression(path) {
path.replaceWith(path.node.expression);
},
});
expect(generate(ast).code).toMatchInlineSnapshot(`
"() => {
return a.b.c;
};"
`);
});
});
describe("replaceWithMultiple", () => {
it("does not add extra parentheses for a JSXElement with a JSXElement parent", () => {
Expand Down

0 comments on commit fc63612

Please sign in to comment.