Skip to content

[Bug]: The internal class binding captured in element decorators should not be mutated #16356

@JLHwung

Description

@JLHwung

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

{
  const errs = [];
  const fns = [];
  const captureAndAssertUninitialized = function (fn) {
    fns.push(fn);
    try {
      fn();
    } catch (err) {
      errs.push(err);
    } finally {
      return v => v;
    }
  }

  @captureAndAssertUninitialized(() => C)
  class C {
    @captureAndAssertUninitialized(() => C) [captureAndAssertUninitialized(() => C)]
  }

  console.log(errs, fns.map(fn => fn()));

  C = null;

  console.log(fns.map(fn => fn()));
}

REPL

Configuration file name

No response

Configuration

{
  "plugins": [["proposal-decorators", { "version": "2023-11" }]]
}

Current and expected behavior

Current:

The first console.log prints [ReferenceError], [C, C, C], the second console.log prints [null, null, null].

Expected:

The first console.log should print [ReferenceError, ReferenceError, ReferenceError], [C, C, C],
because decorators and computed keys are evaluated before the class binding is bound.

The second console.log should print [null, C, C], because C within the class decorators points to the mutable binding created in BindingClassDeclarationEvaluation, while C in the element decorators and computed keys point to the internal immutable class binding.

Environment

REPL

Possible solution

No response

Additional context

This is the point 6 of #16117.

Metadata

Metadata

Assignees

Labels

Spec: DecoratorsoutdatedA 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