Skip to content

Commit

Permalink
feat(semantic): add some scopes to align TypeScript's container
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jun 29, 2024
1 parent 21b964b commit 44587fa
Show file tree
Hide file tree
Showing 13 changed files with 758 additions and 133 deletions.
30 changes: 21 additions & 9 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ pub use match_ts_type;
///
/// <https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#handbook-content>
#[visited_node]
#[derive(Debug, Hash)]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type", rename_all = "camelCase"))]
pub struct TSConditionalType<'a> {
Expand All @@ -237,6 +238,7 @@ pub struct TSConditionalType<'a> {
pub extends_type: TSType<'a>,
pub true_type: TSType<'a>,
pub false_type: TSType<'a>,
pub scope_id: Cell<Option<ScopeId>>,
}

/// string | string[] | (() => string) | { s: string }
Expand Down Expand Up @@ -566,8 +568,7 @@ pub struct TSTypeParameterInstantiation<'a> {
}

#[visited_node]
#[scope]
#[derive(Debug)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type", rename_all = "camelCase"))]
pub struct TSTypeParameter<'a> {
Expand All @@ -579,7 +580,6 @@ pub struct TSTypeParameter<'a> {
pub r#in: bool,
pub out: bool,
pub r#const: bool,
pub scope_id: Cell<Option<ScopeId>>,
}

#[visited_node]
Expand All @@ -593,16 +593,19 @@ pub struct TSTypeParameterDeclaration<'a> {
}

#[visited_node]
#[derive(Debug, Hash)]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type", rename_all = "camelCase"))]
pub struct TSTypeAliasDeclaration<'a> {
#[cfg_attr(feature = "serialize", serde(flatten))]
pub span: Span,
pub id: BindingIdentifier<'a>,
#[scope(enter_before)]
pub type_annotation: TSType<'a>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub declare: bool,
pub scope_id: Cell<Option<ScopeId>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand All @@ -629,17 +632,20 @@ pub struct TSClassImplements<'a> {
///
/// interface `BindingIdentifier` `TypeParameters_opt` `InterfaceExtendsClause_opt` `ObjectType`
#[visited_node]
#[derive(Debug, Hash)]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type", rename_all = "camelCase"))]
pub struct TSInterfaceDeclaration<'a> {
#[cfg_attr(feature = "serialize", serde(flatten))]
pub span: Span,
pub id: BindingIdentifier<'a>,
#[scope(enter_before)]
pub body: Box<'a, TSInterfaceBody<'a>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
pub declare: bool,
pub scope_id: Cell<Option<ScopeId>>,
}

#[visited_node]
Expand Down Expand Up @@ -713,7 +719,8 @@ pub enum TSMethodSignatureKind {
}

#[visited_node]
#[derive(Debug, Hash)]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type", rename_all = "camelCase"))]
pub struct TSMethodSignature<'a> {
Expand All @@ -727,10 +734,12 @@ pub struct TSMethodSignature<'a> {
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub scope_id: Cell<Option<ScopeId>>,
}

#[visited_node]
#[derive(Debug, Hash)]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type", rename_all = "camelCase"))]
pub struct TSConstructSignatureDeclaration<'a> {
Expand All @@ -739,6 +748,7 @@ pub struct TSConstructSignatureDeclaration<'a> {
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub scope_id: Cell<Option<ScopeId>>,
}

#[visited_node]
Expand Down Expand Up @@ -981,7 +991,8 @@ pub struct TSConstructorType<'a> {
}

#[visited_node]
#[derive(Debug, Hash)]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type", rename_all = "camelCase"))]
pub struct TSMappedType<'a> {
Expand All @@ -992,6 +1003,7 @@ pub struct TSMappedType<'a> {
pub type_annotation: Option<TSType<'a>>,
pub optional: TSMappedTypeModifierOperator,
pub readonly: TSMappedTypeModifierOperator,
pub scope_id: Cell<Option<ScopeId>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
33 changes: 15 additions & 18 deletions crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,12 +1716,9 @@ impl<'a> AstBuilder<'a> {
return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
) -> TSSignature<'a> {
TSSignature::TSConstructSignatureDeclaration(self.alloc(TSConstructSignatureDeclaration {
span,
params,
return_type,
type_parameters,
}))
TSSignature::TSConstructSignatureDeclaration(self.alloc(
TSConstructSignatureDeclaration::new(span, params, return_type, type_parameters),
))
}

#[inline]
Expand All @@ -1737,7 +1734,7 @@ impl<'a> AstBuilder<'a> {
return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
) -> TSSignature<'a> {
TSSignature::TSMethodSignature(self.alloc(TSMethodSignature {
TSSignature::TSMethodSignature(self.alloc(TSMethodSignature::new(
span,
key,
computed,
Expand All @@ -1747,7 +1744,7 @@ impl<'a> AstBuilder<'a> {
params,
return_type,
type_parameters,
}))
)))
}

#[inline]
Expand Down Expand Up @@ -1847,19 +1844,19 @@ impl<'a> AstBuilder<'a> {
self,
span: Span,
id: BindingIdentifier<'a>,
body: Box<'a, TSInterfaceBody<'a>>,
type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
body: Box<'a, TSInterfaceBody<'a>>,
declare: bool,
) -> Declaration<'a> {
Declaration::TSInterfaceDeclaration(self.alloc(TSInterfaceDeclaration {
Declaration::TSInterfaceDeclaration(self.alloc(TSInterfaceDeclaration::new(
span,
id,
body,
type_parameters,
extends,
body,
declare,
}))
)))
}

#[inline]
Expand All @@ -1871,13 +1868,13 @@ impl<'a> AstBuilder<'a> {
type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
declare: bool,
) -> Declaration<'a> {
Declaration::TSTypeAliasDeclaration(self.alloc(TSTypeAliasDeclaration {
Declaration::TSTypeAliasDeclaration(self.alloc(TSTypeAliasDeclaration::new(
span,
id,
type_annotation,
type_parameters,
declare,
}))
)))
}

#[inline]
Expand Down Expand Up @@ -2008,13 +2005,13 @@ impl<'a> AstBuilder<'a> {
true_type: TSType<'a>,
false_type: TSType<'a>,
) -> TSType<'a> {
TSType::TSConditionalType(self.alloc(TSConditionalType {
TSType::TSConditionalType(self.alloc(TSConditionalType::new(
span,
check_type,
extends_type,
true_type,
false_type,
}))
)))
}

#[inline]
Expand All @@ -2027,14 +2024,14 @@ impl<'a> AstBuilder<'a> {
optional: TSMappedTypeModifierOperator,
readonly: TSMappedTypeModifierOperator,
) -> TSType<'a> {
TSType::TSMappedType(self.alloc(TSMappedType {
TSType::TSMappedType(self.alloc(TSMappedType::new(
span,
type_parameter,
name_type,
type_annotation,
optional,
readonly,
}))
)))
}

#[inline]
Expand Down
Loading

0 comments on commit 44587fa

Please sign in to comment.