Skip to content

Commit

Permalink
feat(transformer): class properties transform (#7011)
Browse files Browse the repository at this point in the history
Add class properties transform.

Implementation is incomplete. Notable missing parts:

* Scopes are not updated where property initializers move from class body into class constructor / `_super` function.
* Does not handle binding shadowing problems when property initializers move from class body into class constructor.
* `this` and references to class name in static property initializers need to be transformed to point to a temp var.
* Not all usages of private properties are supported (see below).
* Code which is moved to outside of class body is not transformed by other transforms for class declarations (works OK for class expressions). This includes static property initializers, static blocks, and computed property/method keys.
* Only basic checks for whether computed property/method keys may have side effects.
* Numerous other small issues noted in TODO comments through the code.

### Private properties

Currently does not handle the following usages of private properties:

```js
class Class {
  #prop;
  static #static;

  method() {
    object?.#prop;
    object?.#prop();
    [object.#prop] = [1];
    ({x: object.#prop} = {x: 1});
    object.#prop`xyz`;

    object?.#static;
    object?.#static();
    [object.#static] = [1];
    ({x: object.#static} = {x: 1});
    object.#static`xyz`;
  }
}
```
  • Loading branch information
overlookmotel committed Nov 25, 2024
1 parent 3b2a347 commit 9778298
Show file tree
Hide file tree
Showing 16 changed files with 5,044 additions and 124 deletions.
10 changes: 10 additions & 0 deletions crates/oxc_transformer/src/common/helper_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ pub enum Helper {
ObjectDestructuringEmpty,
ObjectWithoutProperties,
ToPropertyKey,
DefineProperty,
ClassPrivateFieldInitSpec,
ClassPrivateFieldGet2,
ClassPrivateFieldSet2,
AssertClassBrand,
}

impl Helper {
Expand All @@ -162,6 +167,11 @@ impl Helper {
Self::ObjectDestructuringEmpty => "objectDestructuringEmpty",
Self::ObjectWithoutProperties => "objectWithoutProperties",
Self::ToPropertyKey => "toPropertyKey",
Self::DefineProperty => "defineProperty",
Self::ClassPrivateFieldInitSpec => "classPrivateFieldInitSpec",
Self::ClassPrivateFieldGet2 => "classPrivateFieldGet2",
Self::ClassPrivateFieldSet2 => "classPrivateFieldSet2",
Self::AssertClassBrand => "assertClassBrand",
}
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_transformer/src/common/var_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ impl<'a> VarDeclarationsStore<'a> {

/// Add a `let` declaration to be inserted at top of current enclosing statement block,
/// given a `BoundIdentifier`.
#[expect(dead_code)]
pub fn insert_let(
&self,
binding: &BoundIdentifier<'a>,
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_transformer/src/compiler_assumptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ pub struct CompilerAssumptions {
pub set_computed_properties: bool,

#[serde(default)]
#[deprecated = "Not Implemented"]
pub set_public_class_fields: bool,

#[serde(default)]
Expand Down
94 changes: 0 additions & 94 deletions crates/oxc_transformer/src/es2022/class_properties.rs

This file was deleted.

Loading

0 comments on commit 9778298

Please sign in to comment.