Skip to content

Invalid read-only when using destructuring shadowed by import #10627

@dasa

Description

@dasa

Bug Report

Current Behavior
The commonjs and amd plugins create invalid throw new Error('"' + "foo" + '" is read-only.'); statements in the output when there is an import with the same name as used in the destructuring even though it's shadowed by a function parameter.

Input Code

import { foo } from "./x";

function test(foo) {
  console.log(foo);
  [foo] = [1];
  ({foo} = { foo: 1 });
  console.log(foo);
}

test(0);

Output:

"use strict";

var _x = require("./x");

function test(foo) {
  console.log(foo);
  [foo] = ([1], function () {
    throw new Error('"' + "foo" + '" is read-only.');
  }());
  ({
    foo
  } = ({
    foo: 1
  }, function () {
    throw new Error('"' + "foo" + '" is read-only.');
  }()));
  console.log(foo);
}

test(0);

Expected behavior/code
No Error's in output, like this when the import name is changed:

"use strict";

var _x = require("./x");

function test(foo) {
  console.log(foo);
  [foo] = [1];
  ({
    foo
  } = {
    foo: 1
  });
  console.log(foo);
}

test(0);

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

  "babel": {
    "plugins": [
      "@babel/plugin-transform-modules-commonjs"
    ]
  }

Environment

  • Babel version(s): v7.6.4
  • Node/npm version: Node 12
  • OS: OSX
  • Monorepo: no
  • How you are using Babel: cli or REPL

Possible Solution

Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.

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