@@ -239,23 +239,33 @@ impl<'a> ClassProperties<'a, '_> {
239239 return ;
240240 }
241241
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+
242252 // Determine where to insert instance property initializers in constructor
243253 let instance_inits_insert_location = if let Some ( constructor) = constructor {
244254 // Existing constructor
245255 let constructor = constructor. value . as_mut ( ) ;
246256 if has_super_class {
247257 let ( insert_scopes, insert_location) =
248258 Self :: replace_super_in_constructor ( constructor, ctx) ;
249- self . instance_inits_scope_id = insert_scopes. insert_in_scope_id ;
250- self . instance_inits_constructor_scope_id = insert_scopes. constructor_scope_id ;
259+ instance_inits_scope_id = insert_scopes. insert_in_scope_id ;
260+ instance_inits_constructor_scope_id = insert_scopes. constructor_scope_id ;
251261 insert_location
252262 } else {
253263 let constructor_scope_id = constructor. scope_id ( ) ;
254- self . instance_inits_scope_id = constructor_scope_id;
264+ instance_inits_scope_id = constructor_scope_id;
255265 // Only record `constructor_scope_id` if constructor's scope has some bindings.
256266 // If it doesn't, no need to check for shadowed symbols in instance prop initializers,
257267 // because no bindings to clash with.
258- self . instance_inits_constructor_scope_id =
268+ instance_inits_constructor_scope_id =
259269 if ctx. scoping ( ) . get_bindings ( constructor_scope_id) . is_empty ( ) {
260270 None
261271 } else {
@@ -270,8 +280,7 @@ impl<'a> ClassProperties<'a, '_> {
270280 NodeId :: DUMMY ,
271281 ScopeFlags :: Function | ScopeFlags :: Constructor | ScopeFlags :: StrictMode ,
272282 ) ;
273- self . instance_inits_scope_id = constructor_scope_id;
274- self . instance_inits_constructor_scope_id = None ;
283+ instance_inits_scope_id = constructor_scope_id;
275284 InstanceInitsInsertLocation :: NewConstructor
276285 } ;
277286
@@ -300,7 +309,13 @@ impl<'a> ClassProperties<'a, '_> {
300309 match element {
301310 ClassElement :: PropertyDefinition ( prop) => {
302311 if !prop. r#static {
303- self . convert_instance_property ( prop, & mut instance_inits, ctx) ;
312+ self . convert_instance_property (
313+ prop,
314+ & mut instance_inits,
315+ instance_inits_scope_id,
316+ instance_inits_constructor_scope_id,
317+ ctx,
318+ ) ;
304319 }
305320 }
306321 ClassElement :: MethodDefinition ( method) => {
@@ -321,7 +336,13 @@ impl<'a> ClassProperties<'a, '_> {
321336 // Create a constructor if there isn't one.
322337 match instance_inits_insert_location {
323338 InstanceInitsInsertLocation :: NewConstructor => {
324- self . insert_constructor ( body, instance_inits, has_super_class, ctx) ;
339+ Self :: insert_constructor (
340+ body,
341+ instance_inits,
342+ has_super_class,
343+ instance_inits_scope_id,
344+ ctx,
345+ ) ;
325346 }
326347 InstanceInitsInsertLocation :: ExistingConstructor ( stmt_index) => {
327348 self . insert_inits_into_constructor_as_statements (
@@ -336,11 +357,17 @@ impl<'a> ClassProperties<'a, '_> {
336357 constructor. as_mut ( ) . unwrap ( ) ,
337358 instance_inits,
338359 & super_binding,
360+ instance_inits_scope_id,
339361 ctx,
340362 ) ;
341363 }
342364 InstanceInitsInsertLocation :: SuperFnOutsideClass ( super_binding) => {
343- self . create_super_function_outside_constructor ( instance_inits, & super_binding, ctx) ;
365+ self . create_super_function_outside_constructor (
366+ instance_inits,
367+ & super_binding,
368+ instance_inits_scope_id,
369+ ctx,
370+ ) ;
344371 }
345372 }
346373 }
0 commit comments