Skip to content

Commit bec7a7d

Browse files
committed
feat(semantic): add scope to TSConstructorType (#14676)
1 parent 42d8c62 commit bec7a7d

File tree

27 files changed

+384
-109
lines changed

27 files changed

+384
-109
lines changed

apps/oxlint/src-js/generated/deserialize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5201,7 +5201,7 @@ function deserializeTSConstructorType(pos) {
52015201
node = parent = {
52025202
__proto__: NodeProto,
52035203
type: 'TSConstructorType',
5204-
abstract: deserializeBool(pos + 32),
5204+
abstract: deserializeBool(pos + 36),
52055205
typeParameters: null,
52065206
params: null,
52075207
returnType: null,

crates/oxc_ast/src/ast/ts.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,7 @@ pub struct TSFunctionType<'a> {
14461446
}
14471447

14481448
#[ast(visit)]
1449+
#[scope]
14491450
#[derive(Debug)]
14501451
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
14511452
pub struct TSConstructorType<'a> {
@@ -1454,6 +1455,7 @@ pub struct TSConstructorType<'a> {
14541455
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
14551456
pub params: Box<'a, FormalParameters<'a>>,
14561457
pub return_type: Box<'a, TSTypeAnnotation<'a>>,
1458+
pub scope_id: Cell<Option<ScopeId>>,
14571459
}
14581460

14591461
/// TypeScript Mapped Type

crates/oxc_ast/src/generated/assert_layouts.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,14 +1468,15 @@ const _: () = {
14681468
assert!(offset_of!(TSFunctionType, return_type) == 32);
14691469
assert!(offset_of!(TSFunctionType, scope_id) == 40);
14701470

1471-
// Padding: 7 bytes
1471+
// Padding: 3 bytes
14721472
assert!(size_of::<TSConstructorType>() == 40);
14731473
assert!(align_of::<TSConstructorType>() == 8);
14741474
assert!(offset_of!(TSConstructorType, span) == 0);
1475-
assert!(offset_of!(TSConstructorType, r#abstract) == 32);
1475+
assert!(offset_of!(TSConstructorType, r#abstract) == 36);
14761476
assert!(offset_of!(TSConstructorType, type_parameters) == 8);
14771477
assert!(offset_of!(TSConstructorType, params) == 16);
14781478
assert!(offset_of!(TSConstructorType, return_type) == 24);
1479+
assert!(offset_of!(TSConstructorType, scope_id) == 32);
14791480

14801481
// Padding: 2 bytes
14811482
assert!(size_of::<TSMappedType>() == 56);
@@ -3075,13 +3076,14 @@ const _: () = if cfg!(target_family = "wasm") || align_of::<u64>() == 8 {
30753076
assert!(offset_of!(TSFunctionType, scope_id) == 24);
30763077

30773078
// Padding: 3 bytes
3078-
assert!(size_of::<TSConstructorType>() == 24);
3079+
assert!(size_of::<TSConstructorType>() == 28);
30793080
assert!(align_of::<TSConstructorType>() == 4);
30803081
assert!(offset_of!(TSConstructorType, span) == 0);
3081-
assert!(offset_of!(TSConstructorType, r#abstract) == 20);
3082+
assert!(offset_of!(TSConstructorType, r#abstract) == 24);
30823083
assert!(offset_of!(TSConstructorType, type_parameters) == 8);
30833084
assert!(offset_of!(TSConstructorType, params) == 12);
30843085
assert!(offset_of!(TSConstructorType, return_type) == 16);
3086+
assert!(offset_of!(TSConstructorType, scope_id) == 20);
30853087

30863088
// Padding: 2 bytes
30873089
assert!(size_of::<TSMappedType>() == 36);

crates/oxc_ast/src/generated/ast_builder.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10455,6 +10455,42 @@ impl<'a> AstBuilder<'a> {
1045510455
))
1045610456
}
1045710457

10458+
/// Build a [`TSType::TSConstructorType`] with `scope_id`.
10459+
///
10460+
/// This node contains a [`TSConstructorType`] that will be stored in the memory arena.
10461+
///
10462+
/// ## Parameters
10463+
/// * `span`: The [`Span`] covering this node
10464+
/// * `abstract`
10465+
/// * `type_parameters`
10466+
/// * `params`
10467+
/// * `return_type`
10468+
/// * `scope_id`
10469+
#[inline]
10470+
pub fn ts_type_constructor_type_with_scope_id<T1, T2, T3>(
10471+
self,
10472+
span: Span,
10473+
r#abstract: bool,
10474+
type_parameters: T1,
10475+
params: T2,
10476+
return_type: T3,
10477+
scope_id: ScopeId,
10478+
) -> TSType<'a>
10479+
where
10480+
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
10481+
T2: IntoIn<'a, Box<'a, FormalParameters<'a>>>,
10482+
T3: IntoIn<'a, Box<'a, TSTypeAnnotation<'a>>>,
10483+
{
10484+
TSType::TSConstructorType(self.alloc_ts_constructor_type_with_scope_id(
10485+
span,
10486+
r#abstract,
10487+
type_parameters,
10488+
params,
10489+
return_type,
10490+
scope_id,
10491+
))
10492+
}
10493+
1045810494
/// Build a [`TSType::TSFunctionType`].
1045910495
///
1046010496
/// This node contains a [`TSFunctionType`] that will be stored in the memory arena.
@@ -14174,6 +14210,7 @@ impl<'a> AstBuilder<'a> {
1417414210
type_parameters: type_parameters.into_in(self.allocator),
1417514211
params: params.into_in(self.allocator),
1417614212
return_type: return_type.into_in(self.allocator),
14213+
scope_id: Default::default(),
1417714214
}
1417814215
}
1417914216

@@ -14208,6 +14245,83 @@ impl<'a> AstBuilder<'a> {
1420814245
)
1420914246
}
1421014247

14248+
/// Build a [`TSConstructorType`] with `scope_id`.
14249+
///
14250+
/// If you want the built node to be allocated in the memory arena,
14251+
/// use [`AstBuilder::alloc_ts_constructor_type_with_scope_id`] instead.
14252+
///
14253+
/// ## Parameters
14254+
/// * `span`: The [`Span`] covering this node
14255+
/// * `abstract`
14256+
/// * `type_parameters`
14257+
/// * `params`
14258+
/// * `return_type`
14259+
/// * `scope_id`
14260+
#[inline]
14261+
pub fn ts_constructor_type_with_scope_id<T1, T2, T3>(
14262+
self,
14263+
span: Span,
14264+
r#abstract: bool,
14265+
type_parameters: T1,
14266+
params: T2,
14267+
return_type: T3,
14268+
scope_id: ScopeId,
14269+
) -> TSConstructorType<'a>
14270+
where
14271+
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
14272+
T2: IntoIn<'a, Box<'a, FormalParameters<'a>>>,
14273+
T3: IntoIn<'a, Box<'a, TSTypeAnnotation<'a>>>,
14274+
{
14275+
TSConstructorType {
14276+
span,
14277+
r#abstract,
14278+
type_parameters: type_parameters.into_in(self.allocator),
14279+
params: params.into_in(self.allocator),
14280+
return_type: return_type.into_in(self.allocator),
14281+
scope_id: Cell::new(Some(scope_id)),
14282+
}
14283+
}
14284+
14285+
/// Build a [`TSConstructorType`] with `scope_id`, and store it in the memory arena.
14286+
///
14287+
/// Returns a [`Box`] containing the newly-allocated node.
14288+
/// If you want a stack-allocated node, use [`AstBuilder::ts_constructor_type_with_scope_id`] instead.
14289+
///
14290+
/// ## Parameters
14291+
/// * `span`: The [`Span`] covering this node
14292+
/// * `abstract`
14293+
/// * `type_parameters`
14294+
/// * `params`
14295+
/// * `return_type`
14296+
/// * `scope_id`
14297+
#[inline]
14298+
pub fn alloc_ts_constructor_type_with_scope_id<T1, T2, T3>(
14299+
self,
14300+
span: Span,
14301+
r#abstract: bool,
14302+
type_parameters: T1,
14303+
params: T2,
14304+
return_type: T3,
14305+
scope_id: ScopeId,
14306+
) -> Box<'a, TSConstructorType<'a>>
14307+
where
14308+
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
14309+
T2: IntoIn<'a, Box<'a, FormalParameters<'a>>>,
14310+
T3: IntoIn<'a, Box<'a, TSTypeAnnotation<'a>>>,
14311+
{
14312+
Box::new_in(
14313+
self.ts_constructor_type_with_scope_id(
14314+
span,
14315+
r#abstract,
14316+
type_parameters,
14317+
params,
14318+
return_type,
14319+
scope_id,
14320+
),
14321+
self.allocator,
14322+
)
14323+
}
14324+
1421114325
/// Build a [`TSMappedType`].
1421214326
///
1421314327
/// If you want the built node to be allocated in the memory arena,

crates/oxc_ast/src/generated/derive_clone_in.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7532,6 +7532,7 @@ impl<'new_alloc> CloneIn<'new_alloc> for TSConstructorType<'_> {
75327532
type_parameters: CloneIn::clone_in(&self.type_parameters, allocator),
75337533
params: CloneIn::clone_in(&self.params, allocator),
75347534
return_type: CloneIn::clone_in(&self.return_type, allocator),
7535+
scope_id: Default::default(),
75357536
}
75367537
}
75377538

@@ -7542,6 +7543,7 @@ impl<'new_alloc> CloneIn<'new_alloc> for TSConstructorType<'_> {
75427543
type_parameters: CloneIn::clone_in_with_semantic_ids(&self.type_parameters, allocator),
75437544
params: CloneIn::clone_in_with_semantic_ids(&self.params, allocator),
75447545
return_type: CloneIn::clone_in_with_semantic_ids(&self.return_type, allocator),
7546+
scope_id: CloneIn::clone_in_with_semantic_ids(&self.scope_id, allocator),
75457547
}
75467548
}
75477549
}

crates/oxc_ast/src/generated/derive_dummy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,6 +2684,7 @@ impl<'a> Dummy<'a> for TSConstructorType<'a> {
26842684
type_parameters: Dummy::dummy(allocator),
26852685
params: Dummy::dummy(allocator),
26862686
return_type: Dummy::dummy(allocator),
2687+
scope_id: Dummy::dummy(allocator),
26872688
}
26882689
}
26892690
}

crates/oxc_ast/src/generated/get_id.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,25 @@ impl TSFunctionType<'_> {
442442
}
443443
}
444444

445+
impl TSConstructorType<'_> {
446+
/// Get [`ScopeId`] of [`TSConstructorType`].
447+
///
448+
/// Only use this method on a post-semantic AST where [`ScopeId`]s are always defined.
449+
///
450+
/// # Panics
451+
/// Panics if `scope_id` is [`None`].
452+
#[inline]
453+
pub fn scope_id(&self) -> ScopeId {
454+
self.scope_id.get().unwrap()
455+
}
456+
457+
/// Set [`ScopeId`] of [`TSConstructorType`].
458+
#[inline]
459+
pub fn set_scope_id(&self, scope_id: ScopeId) {
460+
self.scope_id.set(Some(scope_id));
461+
}
462+
}
463+
445464
impl TSMappedType<'_> {
446465
/// Get [`ScopeId`] of [`TSMappedType`].
447466
///

crates/oxc_ast_macros/src/generated/structs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub static STRUCTS: phf::Map<&'static str, StructDetails> = ::phf::Map {
133133
("DoWhileStatement", StructDetails { field_order: None }),
134134
("ReturnStatement", StructDetails { field_order: None }),
135135
("TSImportType", StructDetails { field_order: None }),
136-
("TSConstructorType", StructDetails { field_order: Some(&[0, 4, 1, 2, 3]) }),
136+
("TSConstructorType", StructDetails { field_order: Some(&[0, 5, 1, 2, 3, 4]) }),
137137
("TSImportEqualsDeclaration", StructDetails { field_order: None }),
138138
("RegExpLiteral", StructDetails { field_order: None }),
139139
("LogicalExpression", StructDetails { field_order: Some(&[0, 1, 3, 2]) }),

crates/oxc_ast_visit/src/generated/visit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3998,12 +3998,14 @@ pub mod walk {
39983998
pub fn walk_ts_constructor_type<'a, V: Visit<'a>>(visitor: &mut V, it: &TSConstructorType<'a>) {
39993999
let kind = AstKind::TSConstructorType(visitor.alloc(it));
40004000
visitor.enter_node(kind);
4001+
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
40014002
visitor.visit_span(&it.span);
40024003
if let Some(type_parameters) = &it.type_parameters {
40034004
visitor.visit_ts_type_parameter_declaration(type_parameters);
40044005
}
40054006
visitor.visit_formal_parameters(&it.params);
40064007
visitor.visit_ts_type_annotation(&it.return_type);
4008+
visitor.leave_scope();
40074009
visitor.leave_node(kind);
40084010
}
40094011

crates/oxc_ast_visit/src/generated/visit_mut.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4213,12 +4213,14 @@ pub mod walk_mut {
42134213
) {
42144214
let kind = AstType::TSConstructorType;
42154215
visitor.enter_node(kind);
4216+
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
42164217
visitor.visit_span(&mut it.span);
42174218
if let Some(type_parameters) = &mut it.type_parameters {
42184219
visitor.visit_ts_type_parameter_declaration(type_parameters);
42194220
}
42204221
visitor.visit_formal_parameters(&mut it.params);
42214222
visitor.visit_ts_type_annotation(&mut it.return_type);
4223+
visitor.leave_scope();
42224224
visitor.leave_node(kind);
42234225
}
42244226

0 commit comments

Comments
 (0)