Skip to content

Commit 20cdb1f

Browse files
committed
feat(semantic): align class scope with typescript (#4195)
```ts class Klass <T> extends Root <R> {} // ^^^^^ ^^^ ^^^^ ^^^ ^^ // id type_paramter super_class super_type_parameters body ``` I reorder fields according to the order above The class scope is not defined in the spec. But we need to create a scope for `class` to store `TypeParamters`
1 parent ab41c52 commit 20cdb1f

File tree

14 files changed

+345
-378
lines changed

14 files changed

+345
-378
lines changed

crates/oxc_ast/src/ast/js.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,12 +1592,12 @@ pub struct Class<'a> {
15921592
pub decorators: Vec<'a, Decorator<'a>>,
15931593
#[scope(enter_before)]
15941594
pub id: Option<BindingIdentifier<'a>>,
1595+
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
15951596
#[visit_as(ClassHeritage)]
15961597
pub super_class: Option<Expression<'a>>,
1597-
pub body: Box<'a, ClassBody<'a>>,
1598-
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
15991598
pub super_type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
16001599
pub implements: Option<Vec<'a, TSClassImplements<'a>>>,
1600+
pub body: Box<'a, ClassBody<'a>>,
16011601
pub r#abstract: bool,
16021602
pub declare: bool,
16031603
pub scope_id: Cell<Option<ScopeId>>,

crates/oxc_ast/src/generated/ast_builder.rs

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -485,29 +485,29 @@ impl<'a> AstBuilder<'a> {
485485
span: Span,
486486
decorators: Vec<'a, Decorator<'a>>,
487487
id: Option<BindingIdentifier<'a>>,
488+
type_parameters: T1,
488489
super_class: Option<Expression<'a>>,
489-
body: T1,
490-
type_parameters: T2,
491-
super_type_parameters: T3,
490+
super_type_parameters: T2,
492491
implements: Option<Vec<'a, TSClassImplements<'a>>>,
492+
body: T3,
493493
r#abstract: bool,
494494
declare: bool,
495495
) -> Expression<'a>
496496
where
497-
T1: IntoIn<'a, Box<'a, ClassBody<'a>>>,
498-
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
499-
T3: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
497+
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
498+
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
499+
T3: IntoIn<'a, Box<'a, ClassBody<'a>>>,
500500
{
501501
Expression::ClassExpression(self.alloc(self.class(
502502
r#type,
503503
span,
504504
decorators,
505505
id,
506-
super_class,
507-
body,
508506
type_parameters,
507+
super_class,
509508
super_type_parameters,
510509
implements,
510+
body,
511511
r#abstract,
512512
declare,
513513
)))
@@ -2728,29 +2728,29 @@ impl<'a> AstBuilder<'a> {
27282728
span: Span,
27292729
decorators: Vec<'a, Decorator<'a>>,
27302730
id: Option<BindingIdentifier<'a>>,
2731+
type_parameters: T1,
27312732
super_class: Option<Expression<'a>>,
2732-
body: T1,
2733-
type_parameters: T2,
2734-
super_type_parameters: T3,
2733+
super_type_parameters: T2,
27352734
implements: Option<Vec<'a, TSClassImplements<'a>>>,
2735+
body: T3,
27362736
r#abstract: bool,
27372737
declare: bool,
27382738
) -> Declaration<'a>
27392739
where
2740-
T1: IntoIn<'a, Box<'a, ClassBody<'a>>>,
2741-
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
2742-
T3: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
2740+
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
2741+
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
2742+
T3: IntoIn<'a, Box<'a, ClassBody<'a>>>,
27432743
{
27442744
Declaration::ClassDeclaration(self.alloc(self.class(
27452745
r#type,
27462746
span,
27472747
decorators,
27482748
id,
2749-
super_class,
2750-
body,
27512749
type_parameters,
2750+
super_class,
27522751
super_type_parameters,
27532752
implements,
2753+
body,
27542754
r#abstract,
27552755
declare,
27562756
)))
@@ -3942,29 +3942,29 @@ impl<'a> AstBuilder<'a> {
39423942
span: Span,
39433943
decorators: Vec<'a, Decorator<'a>>,
39443944
id: Option<BindingIdentifier<'a>>,
3945+
type_parameters: T1,
39453946
super_class: Option<Expression<'a>>,
3946-
body: T1,
3947-
type_parameters: T2,
3948-
super_type_parameters: T3,
3947+
super_type_parameters: T2,
39493948
implements: Option<Vec<'a, TSClassImplements<'a>>>,
3949+
body: T3,
39503950
r#abstract: bool,
39513951
declare: bool,
39523952
) -> Class<'a>
39533953
where
3954-
T1: IntoIn<'a, Box<'a, ClassBody<'a>>>,
3955-
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
3956-
T3: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
3954+
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
3955+
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
3956+
T3: IntoIn<'a, Box<'a, ClassBody<'a>>>,
39573957
{
39583958
Class {
39593959
r#type,
39603960
span,
39613961
decorators,
39623962
id,
3963-
super_class,
3964-
body: body.into_in(self.allocator),
39653963
type_parameters: type_parameters.into_in(self.allocator),
3964+
super_class,
39663965
super_type_parameters: super_type_parameters.into_in(self.allocator),
39673966
implements,
3967+
body: body.into_in(self.allocator),
39683968
r#abstract,
39693969
declare,
39703970
scope_id: Default::default(),
@@ -3978,29 +3978,29 @@ impl<'a> AstBuilder<'a> {
39783978
span: Span,
39793979
decorators: Vec<'a, Decorator<'a>>,
39803980
id: Option<BindingIdentifier<'a>>,
3981+
type_parameters: T1,
39813982
super_class: Option<Expression<'a>>,
3982-
body: T1,
3983-
type_parameters: T2,
3984-
super_type_parameters: T3,
3983+
super_type_parameters: T2,
39853984
implements: Option<Vec<'a, TSClassImplements<'a>>>,
3985+
body: T3,
39863986
r#abstract: bool,
39873987
declare: bool,
39883988
) -> Box<'a, Class<'a>>
39893989
where
3990-
T1: IntoIn<'a, Box<'a, ClassBody<'a>>>,
3991-
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
3992-
T3: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
3990+
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
3991+
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
3992+
T3: IntoIn<'a, Box<'a, ClassBody<'a>>>,
39933993
{
39943994
self.class(
39953995
r#type,
39963996
span,
39973997
decorators,
39983998
id,
3999-
super_class,
4000-
body,
40013999
type_parameters,
4000+
super_class,
40024001
super_type_parameters,
40034002
implements,
4003+
body,
40044004
r#abstract,
40054005
declare,
40064006
)
@@ -4943,29 +4943,29 @@ impl<'a> AstBuilder<'a> {
49434943
span: Span,
49444944
decorators: Vec<'a, Decorator<'a>>,
49454945
id: Option<BindingIdentifier<'a>>,
4946+
type_parameters: T1,
49464947
super_class: Option<Expression<'a>>,
4947-
body: T1,
4948-
type_parameters: T2,
4949-
super_type_parameters: T3,
4948+
super_type_parameters: T2,
49504949
implements: Option<Vec<'a, TSClassImplements<'a>>>,
4950+
body: T3,
49514951
r#abstract: bool,
49524952
declare: bool,
49534953
) -> ExportDefaultDeclarationKind<'a>
49544954
where
4955-
T1: IntoIn<'a, Box<'a, ClassBody<'a>>>,
4956-
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
4957-
T3: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
4955+
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
4956+
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
4957+
T3: IntoIn<'a, Box<'a, ClassBody<'a>>>,
49584958
{
49594959
ExportDefaultDeclarationKind::ClassDeclaration(self.alloc(self.class(
49604960
r#type,
49614961
span,
49624962
decorators,
49634963
id,
4964-
super_class,
4965-
body,
49664964
type_parameters,
4965+
super_class,
49674966
super_type_parameters,
49684967
implements,
4968+
body,
49694969
r#abstract,
49704970
declare,
49714971
)))

crates/oxc_ast/src/generated/visit.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,16 @@ pub trait Visit<'a>: Sized {
803803
walk_class_heritage(self, it);
804804
}
805805

806+
#[inline]
807+
fn visit_ts_class_implementses(&mut self, it: &Vec<'a, TSClassImplements<'a>>) {
808+
walk_ts_class_implementses(self, it);
809+
}
810+
811+
#[inline]
812+
fn visit_ts_class_implements(&mut self, it: &TSClassImplements<'a>) {
813+
walk_ts_class_implements(self, it);
814+
}
815+
806816
#[inline]
807817
fn visit_class_body(&mut self, it: &ClassBody<'a>) {
808818
walk_class_body(self, it);
@@ -843,16 +853,6 @@ pub trait Visit<'a>: Sized {
843853
walk_accessor_property(self, it);
844854
}
845855

846-
#[inline]
847-
fn visit_ts_class_implementses(&mut self, it: &Vec<'a, TSClassImplements<'a>>) {
848-
walk_ts_class_implementses(self, it);
849-
}
850-
851-
#[inline]
852-
fn visit_ts_class_implements(&mut self, it: &TSClassImplements<'a>) {
853-
walk_ts_class_implements(self, it);
854-
}
855-
856856
#[inline]
857857
fn visit_conditional_expression(&mut self, it: &ConditionalExpression<'a>) {
858858
walk_conditional_expression(self, it);
@@ -2929,19 +2929,19 @@ pub mod walk {
29292929
if let Some(id) = &it.id {
29302930
visitor.visit_binding_identifier(id);
29312931
}
2932-
if let Some(super_class) = &it.super_class {
2933-
visitor.visit_class_heritage(super_class);
2934-
}
2935-
visitor.visit_class_body(&it.body);
29362932
if let Some(type_parameters) = &it.type_parameters {
29372933
visitor.visit_ts_type_parameter_declaration(type_parameters);
29382934
}
2935+
if let Some(super_class) = &it.super_class {
2936+
visitor.visit_class_heritage(super_class);
2937+
}
29392938
if let Some(super_type_parameters) = &it.super_type_parameters {
29402939
visitor.visit_ts_type_parameter_instantiation(super_type_parameters);
29412940
}
29422941
if let Some(implements) = &it.implements {
29432942
visitor.visit_ts_class_implementses(implements);
29442943
}
2944+
visitor.visit_class_body(&it.body);
29452945
visitor.leave_scope();
29462946
visitor.leave_node(kind);
29472947
}
@@ -2953,6 +2953,27 @@ pub mod walk {
29532953
visitor.leave_node(kind);
29542954
}
29552955

2956+
#[inline]
2957+
pub fn walk_ts_class_implementses<'a, V: Visit<'a>>(
2958+
visitor: &mut V,
2959+
it: &Vec<'a, TSClassImplements<'a>>,
2960+
) {
2961+
for el in it.iter() {
2962+
visitor.visit_ts_class_implements(el);
2963+
}
2964+
}
2965+
2966+
#[inline]
2967+
pub fn walk_ts_class_implements<'a, V: Visit<'a>>(visitor: &mut V, it: &TSClassImplements<'a>) {
2968+
let kind = AstKind::TSClassImplements(visitor.alloc(it));
2969+
visitor.enter_node(kind);
2970+
visitor.visit_ts_type_name(&it.expression);
2971+
if let Some(type_parameters) = &it.type_parameters {
2972+
visitor.visit_ts_type_parameter_instantiation(type_parameters);
2973+
}
2974+
visitor.leave_node(kind);
2975+
}
2976+
29562977
#[inline]
29572978
pub fn walk_class_body<'a, V: Visit<'a>>(visitor: &mut V, it: &ClassBody<'a>) {
29582979
let kind = AstKind::ClassBody(visitor.alloc(it));
@@ -3072,27 +3093,6 @@ pub mod walk {
30723093
}
30733094
}
30743095

3075-
#[inline]
3076-
pub fn walk_ts_class_implementses<'a, V: Visit<'a>>(
3077-
visitor: &mut V,
3078-
it: &Vec<'a, TSClassImplements<'a>>,
3079-
) {
3080-
for el in it.iter() {
3081-
visitor.visit_ts_class_implements(el);
3082-
}
3083-
}
3084-
3085-
#[inline]
3086-
pub fn walk_ts_class_implements<'a, V: Visit<'a>>(visitor: &mut V, it: &TSClassImplements<'a>) {
3087-
let kind = AstKind::TSClassImplements(visitor.alloc(it));
3088-
visitor.enter_node(kind);
3089-
visitor.visit_ts_type_name(&it.expression);
3090-
if let Some(type_parameters) = &it.type_parameters {
3091-
visitor.visit_ts_type_parameter_instantiation(type_parameters);
3092-
}
3093-
visitor.leave_node(kind);
3094-
}
3095-
30963096
#[inline]
30973097
pub fn walk_conditional_expression<'a, V: Visit<'a>>(
30983098
visitor: &mut V,

0 commit comments

Comments
 (0)