Skip to content

Commit 7dc4af3

Browse files
committed
refactor(transformer/class-properties): store is_declaration only on ClassDetails
1 parent 57e91f8 commit 7dc4af3

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

crates/oxc_transformer/src/es2022/class_properties/class.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
5656
return 0;
5757
}
5858

59-
self.is_declaration = false;
60-
61-
self.transform_class(class, ctx);
59+
self.transform_class(class, false, ctx);
6260

6361
// Return number of expressions to be inserted before/after the class
6462
let mut expr_count = self.insert_before.len() + self.insert_after_exprs.len();
@@ -184,9 +182,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
184182
return;
185183
}
186184

187-
self.is_declaration = true;
188-
189-
self.transform_class(class, ctx);
185+
self.transform_class(class, true, ctx);
190186

191187
// TODO: Run other transforms on inserted statements. How?
192188

@@ -292,7 +288,12 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
292288
}
293289

294290
/// Main guts of the transform.
295-
fn transform_class(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) {
291+
fn transform_class(
292+
&mut self,
293+
class: &mut Class<'a>,
294+
is_declaration: bool,
295+
ctx: &mut TraverseCtx<'a>,
296+
) {
296297
// TODO(improve-on-babel): If outer scope is sloppy mode, all code which is moved to outside
297298
// the class should be wrapped in an IIFE with `'use strict'` directive. Babel doesn't do this.
298299

@@ -371,12 +372,12 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
371372
// temp var for class. Static prop in class declaration doesn't.
372373
let mut class_name_binding = class.id.as_ref().map(BoundIdentifier::from_binding_ident);
373374

374-
let need_temp_var = has_static_prop && (!self.is_declaration || class.id.is_none());
375+
let need_temp_var = has_static_prop && (!is_declaration || class.id.is_none());
375376
self.temp_var_is_created = need_temp_var;
376377

377378
let class_temp_binding = if need_temp_var {
378379
let temp_binding = ClassBindings::create_temp_binding(class_name_binding.as_ref(), ctx);
379-
if self.is_declaration {
380+
if is_declaration {
380381
// Anonymous `export default class {}`. Set class name binding to temp var.
381382
// Actual class name will be set to this later.
382383
class_name_binding = Some(temp_binding.clone());
@@ -396,7 +397,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
396397

397398
// Add entry to `classes_stack`
398399
self.classes_stack.push(ClassDetails {
399-
is_declaration: self.is_declaration,
400+
is_declaration,
400401
private_props: if private_props.is_empty() { None } else { Some(private_props) },
401402
bindings: class_bindings,
402403
});
@@ -511,7 +512,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
511512
expr: Expression<'a>,
512513
ctx: &mut TraverseCtx<'a>,
513514
) {
514-
if self.is_declaration {
515+
if self.current_class().is_declaration {
515516
self.insert_after_stmts.push(ctx.ast.statement_expression(SPAN, expr));
516517
} else {
517518
self.insert_after_exprs.push(expr);

crates/oxc_transformer/src/es2022/class_properties/constructor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
455455
// TODO: If static block transform is not enabled, it's possible to construct the class
456456
// within the static block `class C { static { new C() } }` and that'd run before `_super`
457457
// is defined. So it needs to go before the class, not after, in that case.
458-
let init = if self.is_declaration {
458+
let init = if self.current_class().is_declaration {
459459
Some(super_func)
460460
} else {
461461
let assignment = create_assignment(super_binding, super_func, ctx);

crates/oxc_transformer/src/es2022/class_properties/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,6 @@ pub struct ClassProperties<'a, 'ctx> {
218218

219219
// State during transform of class
220220
//
221-
/// `true` for class declaration, `false` for class expression
222-
is_declaration: bool,
223221
/// `true` if temp var for class has been inserted
224222
temp_var_is_created: bool,
225223
/// Scope that instance init initializers will be inserted into
@@ -258,7 +256,6 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
258256
classes_stack: ClassesStack::default(),
259257
class_expression_addresses_stack: NonEmptyStack::new(Address::DUMMY),
260258
// Temporary values - overwritten when entering class
261-
is_declaration: false,
262259
temp_var_is_created: false,
263260
instance_inits_scope_id: ScopeId::new(0),
264261
instance_inits_constructor_scope_id: None,

0 commit comments

Comments
 (0)