Failing test involving object rest/spread and clearScope().

This failing test case demonstrates a regression between 7.0.0-beta.38 and
7.0.0-beta.39 in the @babel/plugin-proposal-object-rest-spread package.

I distilled this test case from a larger configuration of plugins in my
application, one of which calls api.traverse.cache.clearScope(). Although
calling clearScope() is an uncommon thing for a plugin to do, it was a
reliable way to reproduce the problem. If I can find other reliable
reproductions, I'll push some additional failing tests to this PR.
Regardless of how common it is, clearing the scope cache should be a safe
operation that only slows down the transform (because scopes have to be
recreated and re-crawled). Crashing due to a spurious duplicate
declaration seems like a bug worth fixing.

My hunch is that [these two lines](eb38ea2b10/packages/babel-plugin-proposal-object-rest-spread/src/index.js (L75-L76))
(which were changed in `7.0.0-beta.39`) are not actually removing the
original rest element as a binding from the enclosing `Scope`, in certain
circumstances, so the new variable declaration ends up colliding with the
old (removed) binding.

Possibly related: #7304 (reported by @julien-f)
This commit is contained in:
Ben Newman 2018-01-31 10:38:22 -05:00 committed by Nicolò Ribaudo
parent 593c1a0861
commit 22555cd15d
4 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,4 @@
it("es7.objectRestSpread", () => {
let original = { a: 1, b: 2 };
let { ...copy } = original;
});

View File

@ -0,0 +1,7 @@
{
"plugins": [
"./plugin-clear-scope",
"proposal-object-rest-spread",
"external-helpers"
]
}

View File

@ -0,0 +1,7 @@
it("es7.objectRestSpread", () => {
let original = {
a: 1,
b: 2
};
let copy = babelHelpers.objectWithoutProperties(original, []);
});

View File

@ -0,0 +1,11 @@
"use strict";
exports.__esModule = true;
exports.default = function (api) {
return {
visitor: {
Program: function () {
api.traverse.cache.clearScope();
}
}
};
};