Skip to content

Incorrect output when destructuring private property with duplicate properties and rest element #17282

@JLHwung

Description

@JLHwung

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

REPL

class C {
  static #p = "#p";
  static a = "a";
  static {
    var { a, #p: p, a: x, ...r } = C;
    console.log(x);
  }
}

Configuration file name

No response

Configuration

{
  "plugins": ["@babel/plugin-proposal-destructuring-private"]
}

Current and expected behavior

Current: It logs undefined

Expected: It should log a

Environment

REPL

Possible solution

The output incorrectly excluded a when destructuring the patterns after the private property because a presents before #p:

const _excluded = ["a"];
// helper codes
class C {
  static #p = "#p";
  static a = "a";
  static {
    var {
        a
      } = C,
      p = C.#p,
      {
        a: x,
        ...r
      } = _objectWithoutProperties(C, _excluded);
    console.log(x);
  }
}

For simple all-literal cases, we can probably exclude a from the _excluded list. Otherwise if one of the keys are computed, such as

var { [a()]: a, #p: p, [a()]: x, ...r } = C;

we should further break down the pattern after #p ([a()]: x, ...r) into two declarators:

var _m, _m2;
    var {
        [_m = a()]: a
      } = C,
      p = C.#p,
      {
        [_m2 = a()]: x
      } = C,
      r = _objectWithoutProperties(C, [_m, _m2].map(_toPropertyKey));

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions