Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(ast): box typescript enum variants. #3065

Merged
merged 2 commits into from
Apr 22, 2024
Merged
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
14 changes: 7 additions & 7 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ pub struct TSEnumMember<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(untagged))]
pub enum TSEnumMemberName<'a> {
Identifier(IdentifierName<'a>),
StringLiteral(StringLiteral<'a>),
Identifier(Box<'a, IdentifierName<'a>>),
StringLiteral(Box<'a, StringLiteral<'a>>),
// Invalid Grammar `enum E { [computed] }`
ComputedPropertyName(Expression<'a>),
// Invalid Grammar `enum E { 1 }`
NumericLiteral(NumericLiteral<'a>),
NumericLiteral(Box<'a, NumericLiteral<'a>>),
}

#[derive(Debug, Hash)]
Expand Down Expand Up @@ -686,7 +686,7 @@ pub struct TSTypePredicate<'a> {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(untagged, rename_all = "camelCase"))]
pub enum TSTypePredicateName<'a> {
Identifier(IdentifierName<'a>),
Identifier(Box<'a, IdentifierName<'a>>),
This(TSThisType),
}

Expand Down Expand Up @@ -788,7 +788,7 @@ pub struct TSTypeQuery<'a> {
#[cfg_attr(feature = "serialize", serde(untagged))]
pub enum TSTypeQueryExprName<'a> {
TSTypeName(TSTypeName<'a>),
TSImportType(TSImportType<'a>),
TSImportType(Box<'a, TSImportType<'a>>),
}

#[derive(Debug, Hash)]
Expand Down Expand Up @@ -926,7 +926,7 @@ pub struct TSImportEqualsDeclaration<'a> {
#[cfg_attr(feature = "serialize", serde(flatten))]
pub span: Span,
pub id: BindingIdentifier<'a>,
pub module_reference: Box<'a, TSModuleReference<'a>>,
pub module_reference: TSModuleReference<'a>,
pub import_kind: ImportOrExportKind,
}

Expand All @@ -935,7 +935,7 @@ pub struct TSImportEqualsDeclaration<'a> {
#[cfg_attr(feature = "serialize", serde(untagged, rename_all = "camelCase"))]
pub enum TSModuleReference<'a> {
TypeName(TSTypeName<'a>),
ExternalModuleReference(TSExternalModuleReference<'a>),
ExternalModuleReference(Box<'a, TSExternalModuleReference<'a>>),
}

#[derive(Debug, Hash)]
Expand Down
66 changes: 65 additions & 1 deletion crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,7 @@ impl<'a> AstBuilder<'a> {
Declaration::TSImportEqualsDeclaration(self.alloc(TSImportEqualsDeclaration {
span,
id,
module_reference: self.alloc(module_reference),
module_reference,
import_kind,
}))
}
Expand Down Expand Up @@ -1864,6 +1864,70 @@ impl<'a> AstBuilder<'a> {
}))
}

pub fn ts_enum_member_name_identifier(
&self,
ident: IdentifierName<'a>,
) -> TSEnumMemberName<'a> {
TSEnumMemberName::Identifier(self.alloc(ident))
}

pub fn ts_enum_member_name_string_literal(
&self,
lit: StringLiteral<'a>,
) -> TSEnumMemberName<'a> {
TSEnumMemberName::StringLiteral(self.alloc(lit))
}

pub fn ts_enum_member_name_computed_property_name(
&self,
expr: Expression<'a>,
) -> TSEnumMemberName<'a> {
TSEnumMemberName::ComputedPropertyName(expr)
}

pub fn ts_enum_member_name_number_literal(
&self,
lit: NumericLiteral<'a>,
) -> TSEnumMemberName<'a> {
TSEnumMemberName::NumericLiteral(self.alloc(lit))
}

pub fn ts_module_reference_external_module_reference(
&self,
reference: TSExternalModuleReference<'a>,
) -> TSModuleReference<'a> {
TSModuleReference::ExternalModuleReference(self.alloc(reference))
}

pub fn ts_module_reference_type_name(
&self,
reference: TSTypeName<'a>,
) -> TSModuleReference<'a> {
TSModuleReference::TypeName(reference)
}

pub fn ts_type_predicate_name_this(&self, ty: TSThisType) -> TSTypePredicateName<'a> {
TSTypePredicateName::This(ty)
}

pub fn ts_type_predicate_name_identifier(
&self,
ident: IdentifierName<'a>,
) -> TSTypePredicateName<'a> {
TSTypePredicateName::Identifier(self.alloc(ident))
}

pub fn ts_type_query_expr_name_import_type(
&self,
ty: TSImportType<'a>,
) -> TSTypeQueryExprName<'a> {
TSTypeQueryExprName::TSImportType(self.alloc(ty))
}

pub fn ts_type_query_expr_name_type_name(&self, ty: TSTypeName<'a>) -> TSTypeQueryExprName<'a> {
TSTypeQueryExprName::TSTypeName(ty)
}

/* JSDoc */
pub fn js_doc_nullable_type(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Rule for TripleSlashReference {
for stmt in &program.body {
match stmt {
Statement::Declaration(Declaration::TSImportEqualsDeclaration(decl)) => {
match *decl.module_reference {
match decl.module_reference {
TSModuleReference::ExternalModuleReference(ref mod_ref) => {
if let Some(v) =
refs_for_import.get(mod_ref.expression.value.as_str())
Expand Down
21 changes: 15 additions & 6 deletions crates/oxc_parser/src/ts/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,21 @@ impl<'a> ParserImpl<'a> {
fn parse_ts_enum_member_name(&mut self) -> Result<TSEnumMemberName<'a>> {
match self.cur_kind() {
Kind::LBrack => {
Ok(TSEnumMemberName::ComputedPropertyName(self.parse_computed_property_name()?))
let node = self.parse_computed_property_name()?;
Ok(self.ast.ts_enum_member_name_computed_property_name(node))
}
Kind::Str => {
let node = self.parse_literal_string()?;
Ok(self.ast.ts_enum_member_name_string_literal(node))
}
Kind::Str => Ok(TSEnumMemberName::StringLiteral(self.parse_literal_string()?)),
kind if kind.is_number() => {
Ok(TSEnumMemberName::NumericLiteral(self.parse_literal_number()?))
let node = self.parse_literal_number()?;
Ok(self.ast.ts_enum_member_name_number_literal(node))
}
_ => {
let node = self.parse_identifier_name()?;
Ok(self.ast.ts_enum_member_name_identifier(node))
}
_ => Ok(TSEnumMemberName::Identifier(self.parse_identifier_name()?)),
}
}

Expand Down Expand Up @@ -374,12 +382,13 @@ impl<'a> ParserImpl<'a> {
self.expect(Kind::LParen)?;
let expression = self.parse_literal_string()?;
self.expect(Kind::RParen)?;
TSModuleReference::ExternalModuleReference(TSExternalModuleReference {
self.ast.ts_module_reference_external_module_reference(TSExternalModuleReference {
span: self.end_span(reference_span),
expression,
})
} else {
TSModuleReference::TypeName(self.parse_ts_type_name()?)
let node = self.parse_ts_type_name()?;
self.ast.ts_module_reference_type_name(node)
};

self.asi()?;
Expand Down
11 changes: 7 additions & 4 deletions crates/oxc_parser/src/ts/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,11 @@ impl<'a> ParserImpl<'a> {
let span = self.start_span();
self.expect(Kind::Typeof)?;
let expr_name: TSTypeQueryExprName = if self.at(Kind::Import) {
TSTypeQueryExprName::TSImportType(self.parse_ts_import_type()?)
let node = self.parse_ts_import_type()?;
self.ast.ts_type_query_expr_name_import_type(node)
} else {
TSTypeQueryExprName::TSTypeName(self.parse_ts_type_name()?)
let node = self.parse_ts_type_name()?;
self.ast.ts_type_query_expr_name_type_name(node)
};
let type_parameters = self.parse_ts_type_arguments()?;
Ok(self.ast.ts_type_query_type(self.end_span(span), expr_name, type_parameters))
Expand Down Expand Up @@ -902,9 +904,10 @@ impl<'a> ParserImpl<'a> {
let parameter_name = if self.at(Kind::This) {
let span = self.start_span();
self.bump_any();
TSTypePredicateName::This(TSThisType { span: self.end_span(span) })
self.ast.ts_type_predicate_name_this(TSThisType { span: self.end_span(span) })
} else {
TSTypePredicateName::Identifier(self.parse_identifier_name()?)
let node = self.parse_identifier_name()?;
self.ast.ts_type_predicate_name_identifier(node)
};

if !asserts {
Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_transformer/src/typescript/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ impl<'a> TypeScript<'a> {
let binding = self.ctx.ast.binding_pattern(binding_pattern_kind, None, false);
let decl_span = decl.span;

let init = match &mut *decl.module_reference {
TSModuleReference::TypeName(type_name) => self.transform_ts_type_name(type_name),
let init = match &mut decl.module_reference {
TSModuleReference::TypeName(type_name) => {
self.transform_ts_type_name(&mut *type_name)
}
TSModuleReference::ExternalModuleReference(reference) => {
if self.ctx.source_type().is_module() {
self.ctx.error(ImportEqualsRequireUnsupported(decl_span));
Expand Down
Loading