Skip to content

Commit 596978b

Browse files
committed
refactor(transformer/class-properties): simplify to get constructor method only once (#10493)
Benefit from #10495, we can move getting `instance_inits_scope_id` and `instance_inits_constructor_scope_id` part to after all instance properties transformed. So that we can avoid to get `constructor` twice due to borrow checker.
1 parent e10dfc8 commit 596978b

File tree

1 file changed

+44
-51
lines changed
  • crates/oxc_transformer/src/es2022/class_properties

1 file changed

+44
-51
lines changed

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

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ impl<'a> ClassProperties<'a, '_> {
7878
let mut has_static_private_method_or_static_block = false;
7979
// TODO: Store `FxIndexMap`s in a pool and re-use them
8080
let mut private_props = FxIndexMap::default();
81-
let mut constructor = None;
8281
for element in &mut body.body {
8382
match element {
8483
ClassElement::PropertyDefinition(prop) => {
@@ -107,11 +106,7 @@ impl<'a> ClassProperties<'a, '_> {
107106
}
108107
}
109108
ClassElement::MethodDefinition(method) => {
110-
if method.kind == MethodDefinitionKind::Constructor {
111-
if method.value.body.is_some() {
112-
constructor = Some(method);
113-
}
114-
} else if let PropertyKey::PrivateIdentifier(ident) = &method.key {
109+
if let PropertyKey::PrivateIdentifier(ident) = &method.key {
115110
if method.r#static {
116111
has_static_private_method_or_static_block = true;
117112
} else {
@@ -239,51 +234,6 @@ impl<'a> ClassProperties<'a, '_> {
239234
return;
240235
}
241236

242-
// Scope that instance property initializers will be inserted into.
243-
// This is usually class constructor, but can also be a `_super` function which is created.
244-
let instance_inits_scope_id;
245-
// Scope of class constructor, if instance property initializers will be inserted into constructor.
246-
// Used for checking for variable name clashes.
247-
// e.g. `class C { prop = x(); constructor(x) {} }`
248-
// - `x` in constructor needs to be renamed when `x()` is moved into constructor body.
249-
// `None` if class has no existing constructor, as then there can't be any clashes.
250-
let mut instance_inits_constructor_scope_id = None;
251-
252-
// Determine where to insert instance property initializers in constructor
253-
let instance_inits_insert_location = if let Some(constructor) = constructor {
254-
// Existing constructor
255-
let constructor = constructor.value.as_mut();
256-
if has_super_class {
257-
let (insert_scopes, insert_location) =
258-
Self::replace_super_in_constructor(constructor, ctx);
259-
instance_inits_scope_id = insert_scopes.insert_in_scope_id;
260-
instance_inits_constructor_scope_id = insert_scopes.constructor_scope_id;
261-
insert_location
262-
} else {
263-
let constructor_scope_id = constructor.scope_id();
264-
instance_inits_scope_id = constructor_scope_id;
265-
// Only record `constructor_scope_id` if constructor's scope has some bindings.
266-
// If it doesn't, no need to check for shadowed symbols in instance prop initializers,
267-
// because no bindings to clash with.
268-
instance_inits_constructor_scope_id =
269-
if ctx.scoping().get_bindings(constructor_scope_id).is_empty() {
270-
None
271-
} else {
272-
Some(constructor_scope_id)
273-
};
274-
InstanceInitsInsertLocation::ExistingConstructor(0)
275-
}
276-
} else {
277-
// No existing constructor - create scope for one
278-
let constructor_scope_id = ctx.scoping_mut().add_scope(
279-
Some(class_scope_id),
280-
NodeId::DUMMY,
281-
ScopeFlags::Function | ScopeFlags::Constructor | ScopeFlags::StrictMode,
282-
);
283-
instance_inits_scope_id = constructor_scope_id;
284-
InstanceInitsInsertLocation::NewConstructor
285-
};
286-
287237
// Extract instance properties initializers.
288238
//
289239
// We leave the properties themselves in place, but take the initializers.
@@ -326,6 +276,49 @@ impl<'a> ClassProperties<'a, '_> {
326276
}
327277
}
328278

279+
// Scope that instance property initializers will be inserted into.
280+
// This is usually class constructor, but can also be a `_super` function which is created.
281+
let instance_inits_scope_id;
282+
// Scope of class constructor, if instance property initializers will be inserted into constructor.
283+
// Used for checking for variable name clashes.
284+
// e.g. `class C { prop = x(); constructor(x) {} }`
285+
// - `x` in constructor needs to be renamed when `x()` is moved into constructor body.
286+
// `None` if class has no existing constructor, as then there can't be any clashes.
287+
let mut instance_inits_constructor_scope_id = None;
288+
289+
// Determine where to insert instance property initializers in constructor
290+
let instance_inits_insert_location = if let Some(constructor) = constructor.as_deref_mut() {
291+
if has_super_class {
292+
let (insert_scopes, insert_location) =
293+
Self::replace_super_in_constructor(constructor, ctx);
294+
instance_inits_scope_id = insert_scopes.insert_in_scope_id;
295+
instance_inits_constructor_scope_id = insert_scopes.constructor_scope_id;
296+
insert_location
297+
} else {
298+
let constructor_scope_id = constructor.scope_id();
299+
instance_inits_scope_id = constructor_scope_id;
300+
// Only record `constructor_scope_id` if constructor's scope has some bindings.
301+
// If it doesn't, no need to check for shadowed symbols in instance prop initializers,
302+
// because no bindings to clash with.
303+
instance_inits_constructor_scope_id =
304+
if ctx.scoping().get_bindings(constructor_scope_id).is_empty() {
305+
None
306+
} else {
307+
Some(constructor_scope_id)
308+
};
309+
InstanceInitsInsertLocation::ExistingConstructor(0)
310+
}
311+
} else {
312+
// No existing constructor - create scope for one
313+
let constructor_scope_id = ctx.scoping_mut().add_scope(
314+
Some(class_scope_id),
315+
NodeId::DUMMY,
316+
ScopeFlags::Function | ScopeFlags::Constructor | ScopeFlags::StrictMode,
317+
);
318+
instance_inits_scope_id = constructor_scope_id;
319+
InstanceInitsInsertLocation::NewConstructor
320+
};
321+
329322
// Reparent property initializers scope to `instance_inits_scope_id`.
330323
self.reparent_initializers_scope(
331324
instance_inits.as_slice(),

0 commit comments

Comments
 (0)