Skip to content

transform-es2015-modules-commonjs destructuring error #5768

@jonhartnett

Description

@jonhartnett

When destructuring into an exported variable, transform-es2015-modules-commonjs fails to generate code to keep the local variable and the exports value in sync.

Input Code

export let x = 0;
export function f1(){
    x = 1;
}
export function f2(){
    [x] = [2];
}
export function f3(){
    ({x} = {x: 3});
}

Babel Configuration (.babelrc, package.json, cli command)

{
  "plugins": ["transform-es2015-modules-commonjs"]
}

Expected Generated Code

"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.f1 = f1;
exports.f2 = f2;
exports.f3 = f3;
let x = exports.x = 0;
function f1() {
    exports.x = x = 1;
}
function f2() {
    [x] = [2];
    exports.x = x; //this is one possible solution
}
function f3() {
    ({ x } = { x: 3 });
    exports.x = x;
}

Current Generated Code

"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.f1 = f1;
exports.f2 = f2;
exports.f3 = f3;
let x = exports.x = 0;
function f1() {
    exports.x = x = 1;
}
function f2() {
    [x] = [2];
}
function f3() {
    ({ x } = { x: 3 });
}

Possible Solution

Add a line to keep the local variable and the export value in sync when destructuring into export variables.

software version(s)
Babel 6.24.1
babel-plugin-transform-es2015-modules-commonjs 6.24.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has PRoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions