Skip to content

Commit

Permalink
refactor(transformer/class-properties): placeholder method for transf…
Browse files Browse the repository at this point in the history
…orming private field assignment patterns (#7482)

Add a no-op placeholder method for transforming private fields as `AssignmentPattern`s. e.g. `[object.#prop] = []`.

Implementation will be added later.
  • Loading branch information
overlookmotel committed Nov 25, 2024
1 parent 97de0b7 commit e5d49db
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
10 changes: 8 additions & 2 deletions crates/oxc_transformer/src/es2022/class_properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,18 @@ impl<'a, 'ctx> Traverse<'a> for ClassProperties<'a, 'ctx> {
// TODO: `transform_tagged_template_expression` is no-op at present
self.transform_tagged_template_expression(expr, ctx);
}
// TODO: `[object.#prop] = value`
// TODO: `({x: object.#prop} = value)`
_ => {}
}
}

fn enter_assignment_target(
&mut self,
target: &mut AssignmentTarget<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.transform_assignment_target(target, ctx);
}

fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
match stmt {
// `class C {}`
Expand Down
42 changes: 42 additions & 0 deletions crates/oxc_transformer/src/es2022/class_properties/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,48 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
// See `private/tagged-template` fixture.
}

/// Transform private field in assignment pattern.
///
/// Instance prop:
/// * `[object.#prop] = arr` -> `[_toSetter(_classPrivateFieldSet, [_prop, object])._] = arr`
/// * `({x: object.#prop} = obj)` -> `({ x: _toSetter(_classPrivateFieldSet, [_prop, object])._ } = obj)`
///
/// Static prop:
/// (same as `Expression::PrivateFieldExpression` is transformed to)
/// * `[object.#prop] = arr` -> `[_assertClassBrand(Class, object, _prop)._] = arr`
/// * `({x: object.#prop} = obj)` -> `({ x: _assertClassBrand(Class, object, _prop)._ } = obj)`
//
// `#[inline]` because most `AssignmentTarget`s are not `PrivateFieldExpression`s.
// So we want to bail out in that common case without the cost of a function call.
// Transform of `PrivateFieldExpression`s in broken out into `transform_assignment_target_impl` to
// keep this function as small as possible.
#[inline]
pub(super) fn transform_assignment_target(
&mut self,
target: &mut AssignmentTarget<'a>,
ctx: &mut TraverseCtx<'a>,
) {
// `object.#prop` in assignment pattern.
// Must be in assignment pattern, as `enter_expression` already transformed `AssignmentExpression`s.
if matches!(target, AssignmentTarget::PrivateFieldExpression(_)) {
self.transform_assignment_target_impl(target, ctx);
}
}

#[expect(clippy::unused_self)]
fn transform_assignment_target_impl(
&mut self,
target: &mut AssignmentTarget<'a>,
_ctx: &mut TraverseCtx<'a>,
) {
let AssignmentTarget::PrivateFieldExpression(_private_field) = target else {
unreachable!()
};

// TODO: `[object.#prop] = value`
// TODO: `({x: object.#prop} = value)`
}

/// Duplicate object to be used in get/set pair.
///
/// If `object` may have side effects, create a temp var `_object` and assign to it.
Expand Down

0 comments on commit e5d49db

Please sign in to comment.