Skip to content

Don't throw when destructuring into a var named as an import#10628

Merged
nicolo-ribaudo merged 1 commit intobabel:masterfrom
nicolo-ribaudo:import-shadowed-destructuring-assign
Nov 4, 2019
Merged

Don't throw when destructuring into a var named as an import#10628
nicolo-ribaudo merged 1 commit intobabel:masterfrom
nicolo-ribaudo:import-shadowed-destructuring-assign

Conversation

@nicolo-ribaudo
Copy link
Copy Markdown
Member

Q                       A
Fixed Issues? Fixes #10627
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories area: modules labels Nov 2, 2019
@buildsize
Copy link
Copy Markdown

buildsize Bot commented Nov 2, 2019

File name Previous Size New Size Change
babel-preset-env.js 2.77 MB 2.73 MB -37.19 KB (1%)
babel-preset-env.min.js 1.67 MB 1.65 MB -17.8 KB (1%)
babel.js 2.95 MB 2.92 MB -31.92 KB (1%)
babel.min.js 1.63 MB 1.61 MB -15.94 KB (1%)
test262.tap 4.84 MB [deleted]

} = {});
}

_x.foo = (2, function () {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a follow up PR, this could be left as foo since we are throwing anyway.

@JLHwung JLHwung self-requested a review November 3, 2019 22:41
.pop();
const programScopeIds = Object.keys(ids).filter(
localName =>
scope.getBinding(localName) === path.scope.getBinding(localName),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: consider to guard with scope.hasBinding to avoid unnecessary path.scope.getBinding.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do it and found a bug which, if fixed, would break @babel/plugin-transform-runtime <= 7.6 😢

This is what currently happens:

  1. The imports are analyzed
  2. The imports are removed -> The bindings are removed
  3. References to imported bindings are rewritten (this file)
    i) When we are comparing the bindings, the global one is undefined
    ii) If the local binding is undefined, it hasn't been redefined.

I tried to swap 2 and 3, but I still can optimize away the check for the local binding because @babel/transform-runtime doesn't register the import it injects in the scope tracker.

@nicolo-ribaudo nicolo-ribaudo force-pushed the import-shadowed-destructuring-assign branch 2 times, most recently from 14a6afa to 65c0f1f Compare November 4, 2019 21:50
@nicolo-ribaudo nicolo-ribaudo merged commit 43aa7e2 into babel:master Nov 4, 2019
@nicolo-ribaudo nicolo-ribaudo deleted the import-shadowed-destructuring-assign branch November 4, 2019 21:50
@lock lock Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Feb 3, 2020
@lock lock Bot locked as resolved and limited conversation to collaborators Feb 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: modules outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid read-only when using destructuring shadowed by import

3 participants