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

Destructuring computed property with default value uses string instead of computed property #27

Open
jordanhenderson opened this issue Sep 1, 2018 · 1 comment

Comments

@jordanhenderson
Copy link

jordanhenderson commented Sep 1, 2018

When running extensible destructuring against the following:

const a = 0;
const { [a]: test = 'default' } = { 0: 'test' };

The variable 'test' evaluates to 'default', when this should be 'test'. Without this plugin, when just using babel-env, babel produces the correct expected output. This syntax is extremely helpful for handling when a computed property is not matched in the parent object.

Running babel produces the following syntax:

var __extensible_get__ = require('extensible-runtime').immutable;

var a = 0;
var _ = { 0: 'test' };

var test = __extensible_get__(_, 'a', 'default');

(Note, the string 'a' instead of variable a)
I was able to resolve this on my own codebase by change to line 214 of src/index.js to the following, however unsure if this will have unintended side effects.
objRef = extensibleGet(propRef, prop.computed ? prop.key : t.stringLiteral(prop.key.name), pattern.right)

@hleumas
Copy link
Contributor

hleumas commented Sep 2, 2018

Thanks, seems like a bug in the code and your solution seems viable. Can you please create a pull request with the fix?

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

No branches or pull requests

2 participants