Skip to content

Commit

Permalink
Don't insert the same node into the AST multiple times (fixes babel/m…
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Aug 4, 2017
1 parent 30c4d6b commit c83ae9d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ export default function() {
),
);
} else {
path.replaceWith(remap);
path.replaceWith(
// Clone the node before inserting it to ensure that different nodes in the AST are represented
// by different objects.
t.cloneWithoutLoc(remap),
);
}
this.requeueInParent(path);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const assert = require("assert");
const babel = require("babel-core");

test("Doesn't use the same object for two different nodes in the AST", function() {
const code = 'import Foo from "bar"; Foo; Foo;';

const ast = babel.transform(code, {
plugins: [[require("../"), { loose: true }]],
}).ast;

assert.equal(ast.program.body[3].expression.type, "MemberExpression");
assert.equal(ast.program.body[4].expression.type, "MemberExpression");

assert.notEqual(
ast.program.body[3].expression,
ast.program.body[4].expression,
"Expected different nodes in the AST to not be the same object",
);
});

0 comments on commit c83ae9d

Please sign in to comment.