Skip to content

this.#field not rewritten in class field initializers when class has @decorated accessor #28118

Closed

Description

@lvivski

bun version: 1.3.10

example:

function id(
  value: ClassAccessorDecoratorTarget<any, any>,
  context: ClassAccessorDecoratorContext,
): ClassAccessorDecoratorResult<any, any> {
  return value
}

class Broken {
  @id accessor label: string = ''
  #name = 'hello'
  #callback = () => this.#name
  run() { return this.#callback() }
}

console.log(new Broken().run())

expected behavior:
hello

actual behavior:
SyntaxError: Cannot reference undeclared private names: "#name"

additional info:
When a class contains a @decorated accessor, Bun lowers all private #fields to WeakMap-based helpers (__privateAdd / __privateGet). The rewrite is correct inside methods and the constructor body, but missed inside class field initializer expressions. This affects all field initializer expressions (arrow functions, template literals, plain expressions).

Activity

  1. self-assigned this
    on Mar 14, 2026
  2. lvivski commented on Mar 14, 2026

    @lvivski
    Author

    The bare accessor (without decorator) path already does the right thing. It only lowers the accessor field and keeps other #private fields intact. I think the @decorated accessor path should do the same.

  3. added a commit that references this issue on Mar 14, 2026
    96f71f9
  4. added a commit that references this issue on May 25, 2026
    0ee9510
  5. robobun commented on Jun 5, 2026

    @robobun
    Collaborator

    Confirmed still present on 1.4.0-canary.1, and it is not limited to accessor decorators. Any decorated member triggers the private lowering, and the rewrite also misses these two shapes:

    Undecorated field initializer in a class with a decorated method:

    function decorator() { return (v, ctx) => {}; }
    class Foo {
      static #m = function (tag) { return { Foo, tag }; };
      @decorator() test() { return "t"; }
      fromInit = o?.make().Foo.#m("init"); // left verbatim, #m's declaration was lowered away
    }

    Private field initializer whose function body references another private (the initializer is moved into a constructor-injected __privateAdd, but its body is not rewritten):

    function decorator() { return (v, ctx) => {}; }
    class Counter {
      #tag = "inst";
      #read = function () { return this.#tag; }; // lowered to __privateAdd(this, _read, function() { return this.#tag; })
      @decorator() run() { return this.self().#read(); }
      self() { return this; }
    }
    new Counter().run();

    Both fail with SyntaxError: Cannot reference undeclared private names. Mechanically: field initializers are moved into constructor injection statements (and static blocks) during phases that run before the private-access rewrite pass, and those statement lists are not included in the rewrite targets in src/js_parser/lower/lower_decorators.rs.

  6. added a commit that references this issue on Sep 13, 2026
    16b81df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions