Skip to content

Future proofing of static blocks transform #10956

@nicolo-ribaudo

Description

@nicolo-ribaudo

Describe the bug

There is a stage 2.7 TC39 proposal that would break the way transpilers transform static blocks to private fields, if the static block freezes the class: tc39/proposal-nonextensible-applies-to-private#1.

It'd be great to get all the transpilers updated to be compatible, just in case.

Input code

class A {
    static #foo = 1;
    static { Object.freeze(this); }
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true,
  "env": {
    "targets": "chrome 90"
  }
}

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.13.2&code=H4sIAAAAAAAAA0vOSSwuVnBUqOZSAILiksSSzGQF5bT8fAVbBUNrZMFqBf%2BkrNTkEr20otTUqlSNkozMYk1rhVquWgDElPrbRAAAAA%3D%3D&config=H4sIAAAAAAAAA1WOSw7CMAxE9zlF5DULVkhwBw4RBbekaj6yU0RU9e4kaVPanT0zfp5ZSAkDa3jIOY95CYoYad%2BzwslF9c0KoLaKNZkQ4dLcgYvVqZGxSsvqwOg9Y3M2zRpnunRka28DIfM5WKLK9SOeyWKjg%2FWvqZpb55gC1n58g3%2BoPdvBYPjZLiNNq4bucwAp6jGWNqDf5C3K%2B7USxfIDz55nsCoBAAA%3D

SWC Info output

No response

Expected behavior

See Babel PR for example: babel/babel#17443

Given this input:

class C {
  static #FOO = "#FOO";
  static {
    C.#FOO = #BAR in C;
  }
  static #BAR = C.#FOO;
  static {
    Object.freeze(C);
  }
}

Something like

var lastBlocks;
class C {
  static #FOO = "#FOO";
  static #BAR = (() => {
    C.#FOO = #BAR in C;
  })(), lastBlocks = (() => {
    Object.freeze(C);
  }), C.#FOO;
}
lastBlocks();

Actual behavior

For this code:

class A {
    static #foo = 1;
    static { Object.freeze(this); }
}

SWC generates

class A {
    static #foo = 1;
    static #_ = Object.freeze(this);
}

which with that proposal will throw because it's trying to add #_ to a frozen object.

Version

1.13.2

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions