Skip to content

Invalid output for static private fields #1131

@bathos

Description

@bathos

It appears references to already-initialized private static members can get lifted out of the lexical scope where their names exist.

Input
export class Foo {
  static #bar = 1;

  static baz = Foo.#bar;
}
version
0.11.6
minify
false
format
esm
target
none / esnext / maybe others do it too
Output
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, {enumerable: true, configurable: true, writable: true, value}) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};

var _Foo = class {
  static #bar;
};
var Foo = _Foo;
Foo.#bar = 1;
__publicField(Foo, "baz", _Foo.#bar);
export {
  Foo
};

The condition for this seems to be the presence of a reference to Foo (or this) in a static initializer (which may or may not be private). I think it’s the same set of scenarios where it’s altering the value of Foo.name.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions