babel/babel

[Bug]: Element decorator referencing both `this` and local private field is not compiled properly

nicolo-ribaudo opened this issue · 1 comments

💻

  • Would you like to work on a fix?

How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

Input code

class A {
  #x = console.log.bind(console, "outer #x");
  foo() {
    let after;
    
    @(new A().#x)
    class B {
	  #x = console.log.bind(console, "inner #x");
      @(after = () => new B().#x(this instanceof A), () => {}) f;
    }

    after();
  }
}

new A().foo();

Configuration file name

No response

Configuration

Decorators 2023-05 and (unreleased) 2023-11

Current and expected behavior

I think it should log

"outer #x", class B {}, { kind: "class", ... }
"inner #x", true

but it throws about the second #x access

Environment

main

Possible solution

No response

Additional context

No response

This is the fifth issue in #16117, we have a test that gets disabled due to this issue: packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-ordering/.field-initializers-after-methods-private.

I have a WIP branch on this issue: https://github.com/JLHwung/babel/tree/fix-decorator-evaluation-private-environment, it is pending decorated class cases @dec class C { static @capture(t => t.#q)[foo()]; #q } and function-context references like super usage replacement. Feel free to take over if you want to tackle this issue.