Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1590,14 +1590,14 @@ pub struct Class<'a> {
#[cfg_attr(feature = "serialize", serde(flatten))]
pub span: Span,
pub decorators: Vec<'a, Decorator<'a>>,
#[scope(enter_before)]
pub id: Option<BindingIdentifier<'a>>,
#[scope(enter_before)]
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
#[visit_as(ClassHeritage)]
pub super_class: Option<Expression<'a>>,
pub body: Box<'a, ClassBody<'a>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub super_type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
pub implements: Option<Vec<'a, TSClassImplements<'a>>>,
pub body: Box<'a, ClassBody<'a>>,
pub r#abstract: bool,
pub declare: bool,
pub scope_id: Cell<Option<ScopeId>>,
Expand Down
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 @@ -230,7 +230,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 @@ -240,6 +241,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>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would Option<Cell<ScopeId>> be better or worse?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be Cell<Option<ScopeId>> because the Option value needs to be mutated from None to Some(...) with only an immutable &TSConditionalType (in Semantic).

}

/// string | string[] | (() => string) | { s: string }
Expand Down Expand Up @@ -579,8 +581,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 @@ -592,7 +593,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 @@ -606,16 +606,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_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub type_annotation: TSType<'a>,
pub declare: bool,
pub scope_id: Cell<Option<ScopeId>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand All @@ -642,17 +645,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 extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub body: Box<'a, TSInterfaceBody<'a>>,
pub declare: bool,
pub scope_id: Cell<Option<ScopeId>>,
}

#[visited_node]
Expand Down Expand Up @@ -726,7 +732,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 @@ -740,10 +747,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 @@ -752,6 +761,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 @@ -994,7 +1004,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 @@ -1005,6 +1016,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
90 changes: 65 additions & 25 deletions crates/oxc_ast/src/ast_impl/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,31 +103,6 @@ impl<'a> TSTypeName<'a> {
}
}

impl<'a> TSTypeParameter<'a> {
pub fn new(
span: Span,
name: BindingIdentifier<'a>,
constraint: Option<TSType<'a>>,
default: Option<TSType<'a>>,
r#in: bool,
out: bool,
r#const: bool,
) -> Self {
Self { span, name, constraint, default, r#in, out, r#const, scope_id: Cell::default() }
}
}

impl<'a> Hash for TSTypeParameter<'a> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.constraint.hash(state);
self.default.hash(state);
self.r#in.hash(state);
self.out.hash(state);
self.r#const.hash(state);
}
}

impl<'a> TSType<'a> {
/// Remove nested parentheses from this type.
pub fn without_parenthesized(&self) -> &Self {
Expand Down Expand Up @@ -233,3 +208,68 @@ impl ImportOrExportKind {
matches!(self, Self::Type)
}
}

impl<'a> Hash for TSMappedType<'a> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.span.hash(state);
self.type_parameter.hash(state);
self.name_type.hash(state);
self.type_annotation.hash(state);
self.optional.hash(state);
self.readonly.hash(state);
}
}

impl<'a> Hash for TSConditionalType<'a> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.span.hash(state);
self.check_type.hash(state);
self.extends_type.hash(state);
self.true_type.hash(state);
self.false_type.hash(state);
}
}

impl<'a> Hash for TSInterfaceDeclaration<'a> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.span.hash(state);
self.id.hash(state);
self.type_parameters.hash(state);
self.extends.hash(state);
self.body.hash(state);
self.declare.hash(state);
}
}

impl<'a> Hash for TSTypeAliasDeclaration<'a> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.span.hash(state);
self.id.hash(state);
self.type_parameters.hash(state);
self.type_annotation.hash(state);
self.declare.hash(state);
}
}

impl<'a> Hash for TSMethodSignature<'a> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.span.hash(state);
self.key.hash(state);
self.computed.hash(state);
self.optional.hash(state);
self.kind.hash(state);
self.this_param.hash(state);
self.params.hash(state);
self.return_type.hash(state);
self.type_parameters.hash(state);
}
}

impl<'a> Hash for TSConstructSignatureDeclaration<'a> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.span.hash(state);
self.params.hash(state);
self.return_type.hash(state);
self.type_parameters.hash(state);
}
}
3 changes: 3 additions & 0 deletions crates/oxc_ast/src/ast_kind_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ impl<'a> AstKind<'a> {
Self::TSNamedTupleMember(_) => "TSNamedTupleMember".into(),

Self::TSPropertySignature(_) => "TSPropertySignature".into(),
Self::TSConditionalType(_) => "TSConditionalType".into(),
Self::TSMappedType(_) => "TSMappedType".into(),
Self::TSConstructSignatureDeclaration(_) => "TSConstructSignatureDeclaration".into(),
}
}
}
Loading