babel-plugin-extensible-destructuring
babel-plugin-extensible-destructuring copied to clipboard
Destructuring computed property with default value uses string instead of computed property
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)
Thanks, seems like a bug in the code and your solution seems viable. Can you please create a pull request with the fix?