mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
perf(ast): box typescript enum variants. (#3065)
Similar to #3058 and #3061 it is a continuation of #3047. Handles these enum types: > TSEnumMemberName > Variant sizes: 16, 24, 24, 40 > Unboxed variants: IdentifierName (struct), StringLiteral (struct), NumericLiteral (struct) > Dependents: TSEnumMember (struct) > => Box all variants. > > TSModuleReference > Variant sizes: 16, 32 > Unboxed variants: TSExternalModuleReference (struct) > Dependents: Box<TSModuleReference> in TSImportEqualsDeclaration > => Box all variants. Replace Box<TSModuleReference> with TSModuleReference in TSImportEqualsDeclaration. > > TSTypePredicateName > Variant sizes: 8, 24 > Unboxed variants: IdentifierName (struct), TSThisType (struct) > Dependents: TSTypePredicate (struct) > => Box Identifier variant. Do not box This variant as only 8 bytes (just contains Span). > > TSTypeQueryExprName > Variant sizes: 16, 88 > Unboxed variants: TSImportType (struct) > Dependents: TSTypeQuery (struct) > => Box TSImportType variant. Do not box TSTypeName variant, as is another enum.
This commit is contained in:
parent
48e20880d4
commit
6c8296164e
6 changed files with 99 additions and 21 deletions
|
|
@ -64,12 +64,12 @@ pub struct TSEnumMember<'a> {
|
||||||
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
|
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
|
||||||
#[cfg_attr(feature = "serialize", serde(untagged))]
|
#[cfg_attr(feature = "serialize", serde(untagged))]
|
||||||
pub enum TSEnumMemberName<'a> {
|
pub enum TSEnumMemberName<'a> {
|
||||||
Identifier(IdentifierName<'a>),
|
Identifier(Box<'a, IdentifierName<'a>>),
|
||||||
StringLiteral(StringLiteral<'a>),
|
StringLiteral(Box<'a, StringLiteral<'a>>),
|
||||||
// Invalid Grammar `enum E { [computed] }`
|
// Invalid Grammar `enum E { [computed] }`
|
||||||
ComputedPropertyName(Expression<'a>),
|
ComputedPropertyName(Expression<'a>),
|
||||||
// Invalid Grammar `enum E { 1 }`
|
// Invalid Grammar `enum E { 1 }`
|
||||||
NumericLiteral(NumericLiteral<'a>),
|
NumericLiteral(Box<'a, NumericLiteral<'a>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Hash)]
|
#[derive(Debug, Hash)]
|
||||||
|
|
@ -686,7 +686,7 @@ pub struct TSTypePredicate<'a> {
|
||||||
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
|
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
|
||||||
#[cfg_attr(feature = "serialize", serde(untagged, rename_all = "camelCase"))]
|
#[cfg_attr(feature = "serialize", serde(untagged, rename_all = "camelCase"))]
|
||||||
pub enum TSTypePredicateName<'a> {
|
pub enum TSTypePredicateName<'a> {
|
||||||
Identifier(IdentifierName<'a>),
|
Identifier(Box<'a, IdentifierName<'a>>),
|
||||||
This(TSThisType),
|
This(TSThisType),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -788,7 +788,7 @@ pub struct TSTypeQuery<'a> {
|
||||||
#[cfg_attr(feature = "serialize", serde(untagged))]
|
#[cfg_attr(feature = "serialize", serde(untagged))]
|
||||||
pub enum TSTypeQueryExprName<'a> {
|
pub enum TSTypeQueryExprName<'a> {
|
||||||
TSTypeName(TSTypeName<'a>),
|
TSTypeName(TSTypeName<'a>),
|
||||||
TSImportType(TSImportType<'a>),
|
TSImportType(Box<'a, TSImportType<'a>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Hash)]
|
#[derive(Debug, Hash)]
|
||||||
|
|
@ -926,7 +926,7 @@ pub struct TSImportEqualsDeclaration<'a> {
|
||||||
#[cfg_attr(feature = "serialize", serde(flatten))]
|
#[cfg_attr(feature = "serialize", serde(flatten))]
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
pub id: BindingIdentifier<'a>,
|
pub id: BindingIdentifier<'a>,
|
||||||
pub module_reference: Box<'a, TSModuleReference<'a>>,
|
pub module_reference: TSModuleReference<'a>,
|
||||||
pub import_kind: ImportOrExportKind,
|
pub import_kind: ImportOrExportKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -935,7 +935,7 @@ pub struct TSImportEqualsDeclaration<'a> {
|
||||||
#[cfg_attr(feature = "serialize", serde(untagged, rename_all = "camelCase"))]
|
#[cfg_attr(feature = "serialize", serde(untagged, rename_all = "camelCase"))]
|
||||||
pub enum TSModuleReference<'a> {
|
pub enum TSModuleReference<'a> {
|
||||||
TypeName(TSTypeName<'a>),
|
TypeName(TSTypeName<'a>),
|
||||||
ExternalModuleReference(TSExternalModuleReference<'a>),
|
ExternalModuleReference(Box<'a, TSExternalModuleReference<'a>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Hash)]
|
#[derive(Debug, Hash)]
|
||||||
|
|
|
||||||
|
|
@ -1630,7 +1630,7 @@ impl<'a> AstBuilder<'a> {
|
||||||
Declaration::TSImportEqualsDeclaration(self.alloc(TSImportEqualsDeclaration {
|
Declaration::TSImportEqualsDeclaration(self.alloc(TSImportEqualsDeclaration {
|
||||||
span,
|
span,
|
||||||
id,
|
id,
|
||||||
module_reference: self.alloc(module_reference),
|
module_reference,
|
||||||
import_kind,
|
import_kind,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
@ -1870,6 +1870,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 */
|
/* JSDoc */
|
||||||
pub fn js_doc_nullable_type(
|
pub fn js_doc_nullable_type(
|
||||||
&self,
|
&self,
|
||||||
|
|
|
||||||
|
|
@ -134,7 +134,7 @@ impl Rule for TripleSlashReference {
|
||||||
for stmt in &program.body {
|
for stmt in &program.body {
|
||||||
match stmt {
|
match stmt {
|
||||||
Statement::Declaration(Declaration::TSImportEqualsDeclaration(decl)) => {
|
Statement::Declaration(Declaration::TSImportEqualsDeclaration(decl)) => {
|
||||||
match *decl.module_reference {
|
match decl.module_reference {
|
||||||
TSModuleReference::ExternalModuleReference(ref mod_ref) => {
|
TSModuleReference::ExternalModuleReference(ref mod_ref) => {
|
||||||
if let Some(v) =
|
if let Some(v) =
|
||||||
refs_for_import.get(mod_ref.expression.value.as_str())
|
refs_for_import.get(mod_ref.expression.value.as_str())
|
||||||
|
|
|
||||||
|
|
@ -51,13 +51,21 @@ impl<'a> ParserImpl<'a> {
|
||||||
fn parse_ts_enum_member_name(&mut self) -> Result<TSEnumMemberName<'a>> {
|
fn parse_ts_enum_member_name(&mut self) -> Result<TSEnumMemberName<'a>> {
|
||||||
match self.cur_kind() {
|
match self.cur_kind() {
|
||||||
Kind::LBrack => {
|
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() => {
|
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()?)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -374,12 +382,13 @@ impl<'a> ParserImpl<'a> {
|
||||||
self.expect(Kind::LParen)?;
|
self.expect(Kind::LParen)?;
|
||||||
let expression = self.parse_literal_string()?;
|
let expression = self.parse_literal_string()?;
|
||||||
self.expect(Kind::RParen)?;
|
self.expect(Kind::RParen)?;
|
||||||
TSModuleReference::ExternalModuleReference(TSExternalModuleReference {
|
self.ast.ts_module_reference_external_module_reference(TSExternalModuleReference {
|
||||||
span: self.end_span(reference_span),
|
span: self.end_span(reference_span),
|
||||||
expression,
|
expression,
|
||||||
})
|
})
|
||||||
} else {
|
} 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()?;
|
self.asi()?;
|
||||||
|
|
|
||||||
|
|
@ -761,9 +761,11 @@ impl<'a> ParserImpl<'a> {
|
||||||
let span = self.start_span();
|
let span = self.start_span();
|
||||||
self.expect(Kind::Typeof)?;
|
self.expect(Kind::Typeof)?;
|
||||||
let expr_name: TSTypeQueryExprName = if self.at(Kind::Import) {
|
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 {
|
} 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()?;
|
let type_parameters = self.parse_ts_type_arguments()?;
|
||||||
Ok(self.ast.ts_type_query_type(self.end_span(span), expr_name, type_parameters))
|
Ok(self.ast.ts_type_query_type(self.end_span(span), expr_name, type_parameters))
|
||||||
|
|
@ -900,9 +902,10 @@ impl<'a> ParserImpl<'a> {
|
||||||
let parameter_name = if self.at(Kind::This) {
|
let parameter_name = if self.at(Kind::This) {
|
||||||
let span = self.start_span();
|
let span = self.start_span();
|
||||||
self.bump_any();
|
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 {
|
} else {
|
||||||
TSTypePredicateName::Identifier(self.parse_identifier_name()?)
|
let node = self.parse_identifier_name()?;
|
||||||
|
self.ast.ts_type_predicate_name_identifier(node)
|
||||||
};
|
};
|
||||||
|
|
||||||
if !asserts {
|
if !asserts {
|
||||||
|
|
|
||||||
|
|
@ -44,8 +44,10 @@ impl<'a> TypeScript<'a> {
|
||||||
let binding = self.ctx.ast.binding_pattern(binding_pattern_kind, None, false);
|
let binding = self.ctx.ast.binding_pattern(binding_pattern_kind, None, false);
|
||||||
let decl_span = decl.span;
|
let decl_span = decl.span;
|
||||||
|
|
||||||
let init = match &mut *decl.module_reference {
|
let init = match &mut decl.module_reference {
|
||||||
TSModuleReference::TypeName(type_name) => self.transform_ts_type_name(type_name),
|
TSModuleReference::TypeName(type_name) => {
|
||||||
|
self.transform_ts_type_name(&mut *type_name)
|
||||||
|
}
|
||||||
TSModuleReference::ExternalModuleReference(reference) => {
|
TSModuleReference::ExternalModuleReference(reference) => {
|
||||||
if self.ctx.source_type().is_module() {
|
if self.ctx.source_type().is_module() {
|
||||||
self.ctx.error(ImportEqualsRequireUnsupported(decl_span));
|
self.ctx.error(ImportEqualsRequireUnsupported(decl_span));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue