Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imported value cannot be used as default when destructuring without const/let/var #4870

Closed
noinkling opened this issue May 14, 2017 · 0 comments · Fixed by #4881
Closed

Imported value cannot be used as default when destructuring without const/let/var #4870

noinkling opened this issue May 14, 2017 · 0 comments · Fixed by #4881

Comments

@noinkling
Copy link

noinkling commented May 14, 2017

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

When destructuring using already existing variables (i.e. without const/let/var), imports used as defaults will cause an exception at runtime when trying to access them, e.g. ReferenceError: foo is not defined. Note that there seems to be no problem when using CommonJS syntax.

If the current behavior is a bug, please provide the steps to reproduce.

https://gist.github.com/noinkling/218dc503dc6bb5f844fa53c8a0bc1d32

It's easy to see why this is happening in the compiled output:

...
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__foo__ = __webpack_require__(0);

const bar = 'bar';
let val;

[ val = bar ] = [];
console.log(val);  // "bar"
({ val = bar } = {});
console.log(val);  // "bar"

const [ val2 = __WEBPACK_IMPORTED_MODULE_0__foo__["a" /* default */] ] = [];
console.log(val2);  // "foo"
const { val3 = __WEBPACK_IMPORTED_MODULE_0__foo__["a" /* default */] } = {};
console.log(val3);  // "foo"

[ val = foo ] = [];  // ReferenceError: foo is not defined
({ val = foo } = {});  // ReferenceError: foo is not defined

What is the expected behavior?

Behaves as it would in other destructuring situations.

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

Webpack 2.5.1
Node 7.10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants