From 22555cd15d82feadd9d78c13c0eb89194dc48a00 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Wed, 31 Jan 2018 10:38:22 -0500 Subject: [PATCH] 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](https://github.com/babel/babel/blob/eb38ea2b10d07b3451e4f68b6505cb4730aa5311/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) --- .../fixtures/object-rest/duplicate-decl-bug/input.js | 4 ++++ .../object-rest/duplicate-decl-bug/options.json | 7 +++++++ .../fixtures/object-rest/duplicate-decl-bug/output.js | 7 +++++++ .../duplicate-decl-bug/plugin-clear-scope.js | 11 +++++++++++ 4 files changed, 29 insertions(+) create mode 100644 packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/input.js create mode 100644 packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/options.json create mode 100644 packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/output.js create mode 100644 packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/plugin-clear-scope.js diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/input.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/input.js new file mode 100644 index 0000000000..648a545606 --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/input.js @@ -0,0 +1,4 @@ +it("es7.objectRestSpread", () => { + let original = { a: 1, b: 2 }; + let { ...copy } = original; +}); diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/options.json b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/options.json new file mode 100644 index 0000000000..fcf57f7be5 --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "./plugin-clear-scope", + "proposal-object-rest-spread", + "external-helpers" + ] +} diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/output.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/output.js new file mode 100644 index 0000000000..87abb8f817 --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/output.js @@ -0,0 +1,7 @@ +it("es7.objectRestSpread", () => { + let original = { + a: 1, + b: 2 + }; + let copy = babelHelpers.objectWithoutProperties(original, []); +}); diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/plugin-clear-scope.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/plugin-clear-scope.js new file mode 100644 index 0000000000..c9d503d917 --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/duplicate-decl-bug/plugin-clear-scope.js @@ -0,0 +1,11 @@ +"use strict"; +exports.__esModule = true; +exports.default = function (api) { + return { + visitor: { + Program: function () { + api.traverse.cache.clearScope(); + } + } + }; +};