From b21824475df31272f33fc7650beb8fec950a844f Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Tue, 30 Apr 2024 01:56:02 +0100 Subject: [PATCH] Box `IdentifierName` --- crates/oxc_ast/src/ast/js.rs | 12 +++---- crates/oxc_ast/src/ast/ts.rs | 12 +++---- crates/oxc_ast/src/ast_builder.rs | 10 ++++-- crates/oxc_parser/src/js/list.rs | 16 +++++---- crates/oxc_parser/src/js/module.rs | 33 ++++++++++--------- crates/oxc_parser/src/ts/list.rs | 21 +++++++----- crates/oxc_parser/src/ts/statement.rs | 13 ++++---- crates/oxc_parser/src/ts/types.rs | 2 +- .../src/helpers/module_imports.rs | 29 ++++++++-------- tasks/inspect_ast/lib/index.mjs | 2 +- 10 files changed, 83 insertions(+), 67 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 44874dbac3e81..6e53b6a7d73bf 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -871,7 +871,7 @@ pub struct StaticMemberExpression<'a> { #[cfg_attr(feature = "serialize", serde(flatten))] pub span: Span, pub object: Expression<'a>, - pub property: IdentifierName<'a>, + pub property: Box<'a, IdentifierName<'a>>, pub optional: bool, // for optional chaining } @@ -971,8 +971,8 @@ pub struct NewExpression<'a> { pub struct MetaProperty<'a> { #[cfg_attr(feature = "serialize", serde(flatten))] pub span: Span, - pub meta: IdentifierName<'a>, - pub property: IdentifierName<'a>, + pub meta: Box<'a, IdentifierName<'a>>, + pub property: Box<'a, IdentifierName<'a>>, } /// Spread Element @@ -2763,7 +2763,7 @@ pub struct ImportNamespaceSpecifier<'a> { pub struct WithClause<'a> { #[cfg_attr(feature = "serialize", serde(flatten))] pub span: Span, - pub attributes_keyword: IdentifierName<'a>, // `with` or `assert` + pub attributes_keyword: Box<'a, IdentifierName<'a>>, // `with` or `assert` pub with_entries: Vec<'a, ImportAttribute<'a>>, } @@ -2783,7 +2783,7 @@ pub struct ImportAttribute<'a> { #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] pub enum ImportAttributeKey<'a> { - Identifier(IdentifierName<'a>), + Identifier(Box<'a, IdentifierName<'a>>), StringLiteral(Box<'a, StringLiteral<'a>>), } @@ -2921,7 +2921,7 @@ impl<'a> ExportDefaultDeclarationKind<'a> { #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] pub enum ModuleExportName<'a> { - Identifier(IdentifierName<'a>), + Identifier(Box<'a, IdentifierName<'a>>), StringLiteral(Box<'a, StringLiteral<'a>>), } diff --git a/crates/oxc_ast/src/ast/ts.rs b/crates/oxc_ast/src/ast/ts.rs index fe5395899f0d0..50b76bace9eb2 100644 --- a/crates/oxc_ast/src/ast/ts.rs +++ b/crates/oxc_ast/src/ast/ts.rs @@ -36,7 +36,7 @@ export interface TSIndexSignatureName extends Span { pub struct TSThisParameter<'a> { #[cfg_attr(feature = "serialize", serde(flatten))] pub span: Span, - pub this: IdentifierName<'a>, + pub this: Box<'a, IdentifierName<'a>>, pub type_annotation: Option>>, } @@ -341,7 +341,7 @@ pub struct TSNamedTupleMember<'a> { #[cfg_attr(feature = "serialize", serde(flatten))] pub span: Span, pub element_type: TSType<'a>, - pub label: IdentifierName<'a>, + pub label: Box<'a, IdentifierName<'a>>, pub optional: bool, } @@ -583,7 +583,7 @@ pub struct TSQualifiedName<'a> { #[cfg_attr(feature = "serialize", serde(flatten))] pub span: Span, pub left: TSTypeName<'a>, - pub right: IdentifierName<'a>, + pub right: Box<'a, IdentifierName<'a>>, } #[ast_node] @@ -855,7 +855,7 @@ pub enum TSModuleDeclarationKind { #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] pub enum TSModuleDeclarationName<'a> { - Identifier(IdentifierName<'a>), + Identifier(Box<'a, IdentifierName<'a>>), StringLiteral(Box<'a, StringLiteral<'a>>), } @@ -974,7 +974,7 @@ pub struct TSImportAttribute<'a> { #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[cfg_attr(feature = "serialize", serde(untagged))] pub enum TSImportAttributeName<'a> { - Identifier(IdentifierName<'a>), + Identifier(Box<'a, IdentifierName<'a>>), StringLiteral(Box<'a, StringLiteral<'a>>), } @@ -1251,7 +1251,7 @@ pub struct TSExportAssignment<'a> { pub struct TSNamespaceExportDeclaration<'a> { #[cfg_attr(feature = "serialize", serde(flatten))] pub span: Span, - pub id: IdentifierName<'a>, + pub id: Box<'a, IdentifierName<'a>>, } #[ast_node] diff --git a/crates/oxc_ast/src/ast_builder.rs b/crates/oxc_ast/src/ast_builder.rs index 4793bad453fad..3b2b85bbb8e3a 100644 --- a/crates/oxc_ast/src/ast_builder.rs +++ b/crates/oxc_ast/src/ast_builder.rs @@ -442,7 +442,11 @@ impl<'a> AstBuilder<'a> { meta: IdentifierName<'a>, property: IdentifierName<'a>, ) -> Expression<'a> { - Expression::MetaProperty(self.alloc(MetaProperty { span, meta, property })) + Expression::MetaProperty(self.alloc(MetaProperty { + span, + meta: self.alloc(meta), + property: self.alloc(property), + })) } pub fn array_expression( @@ -652,7 +656,7 @@ impl<'a> AstBuilder<'a> { MemberExpression::StaticMemberExpression(self.alloc(StaticMemberExpression { span, object, - property, + property: self.alloc(property), optional, })) } @@ -873,7 +877,7 @@ impl<'a> AstBuilder<'a> { this: IdentifierName<'a>, type_annotation: Option>>, ) -> Box<'a, TSThisParameter<'a>> { - self.alloc(TSThisParameter { span, this, type_annotation }) + self.alloc(TSThisParameter { span, this: self.alloc(this), type_annotation }) } pub fn plain_function( diff --git a/crates/oxc_parser/src/js/list.rs b/crates/oxc_parser/src/js/list.rs index d337f1c256dc9..e95cc5639b3ba 100644 --- a/crates/oxc_parser/src/js/list.rs +++ b/crates/oxc_parser/src/js/list.rs @@ -315,12 +315,12 @@ impl<'a> SeparatedList<'a> for AssertEntries<'a> { fn parse_element(&mut self, p: &mut ParserImpl<'a>) -> Result<()> { let span = p.start_span(); - let key = match p.cur_kind() { - Kind::Str => { - let str_lit = p.parse_literal_string()?; - ImportAttributeKey::StringLiteral(p.ast.alloc(str_lit)) - } - _ => ImportAttributeKey::Identifier(p.parse_identifier_name()?), + let key = if p.cur_kind() == Kind::Str { + let str_lit = p.parse_literal_string()?; + ImportAttributeKey::StringLiteral(p.ast.alloc(str_lit)) + } else { + let id = p.parse_identifier_name()?; + ImportAttributeKey::Identifier(p.ast.alloc(id)) }; if let Some(old_span) = self.keys.get(&key.as_atom()) { @@ -387,7 +387,9 @@ impl<'a> SeparatedList<'a> for ExportNamedSpecifiers<'a> { p.parse_module_export_name()? } else { match &local { - ModuleExportName::Identifier(id) => ModuleExportName::Identifier(id.clone()), + ModuleExportName::Identifier(id) => { + ModuleExportName::Identifier(p.ast.alloc((**id).clone())) + } ModuleExportName::StringLiteral(str_lit) => { ModuleExportName::StringLiteral(p.ast.alloc((**str_lit).clone())) } diff --git a/crates/oxc_parser/src/js/module.rs b/crates/oxc_parser/src/js/module.rs index b77f080347eff..ec5442658367e 100644 --- a/crates/oxc_parser/src/js/module.rs +++ b/crates/oxc_parser/src/js/module.rs @@ -149,7 +149,7 @@ impl<'a> ParserImpl<'a> { Ok(Some(self.ast.alloc(WithClause { span: self.end_span(span), - attributes_keyword, + attributes_keyword: self.ast.alloc(attributes_keyword), with_entries, }))) } @@ -176,7 +176,10 @@ impl<'a> ParserImpl<'a> { let id = self.parse_identifier_name()?; self.asi()?; - Ok(self.ast.alloc(TSNamespaceExportDeclaration { span: self.end_span(span), id })) + Ok(self.ast.alloc(TSNamespaceExportDeclaration { + span: self.end_span(span), + id: self.ast.alloc(id), + })) } /// [Exports](https://tc39.es/ecma262/#sec-exports) @@ -359,7 +362,7 @@ impl<'a> ParserImpl<'a> { decl } }; - let exported = ModuleExportName::Identifier(exported); + let exported = ModuleExportName::Identifier(self.ast.alloc(exported)); let span = self.end_span(span); Ok(self.ast.export_default_declaration(span, declaration, exported)) } @@ -416,7 +419,7 @@ impl<'a> ParserImpl<'a> { } else { let local = self.parse_binding_identifier()?; let imported = IdentifierName { span: local.span, name: local.name.clone() }; - (ModuleExportName::Identifier(imported), local) + (ModuleExportName::Identifier(self.ast.alloc(imported)), local) }; Ok(self.ast.alloc(ImportSpecifier { span: self.end_span(specifier_span), @@ -430,17 +433,17 @@ impl<'a> ParserImpl<'a> { // IdentifierName // StringLiteral pub(crate) fn parse_module_export_name(&mut self) -> Result> { - match self.cur_kind() { - Kind::Str => { - let literal = self.parse_literal_string()?; - // ModuleExportName : StringLiteral - // It is a Syntax Error if IsStringWellFormedUnicode(the SV of StringLiteral) is false. - if !literal.is_string_well_formed_unicode() { - self.error(diagnostics::ExportLoneSurrogate(literal.span)); - }; - Ok(ModuleExportName::StringLiteral(self.ast.alloc(literal))) - } - _ => Ok(ModuleExportName::Identifier(self.parse_identifier_name()?)), + if self.cur_kind() == Kind::Str { + let literal = self.parse_literal_string()?; + // ModuleExportName : StringLiteral + // It is a Syntax Error if IsStringWellFormedUnicode(the SV of StringLiteral) is false. + if !literal.is_string_well_formed_unicode() { + self.error(diagnostics::ExportLoneSurrogate(literal.span)); + }; + Ok(ModuleExportName::StringLiteral(self.ast.alloc(literal))) + } else { + let id = self.parse_identifier_name()?; + Ok(ModuleExportName::Identifier(self.ast.alloc(id))) } } diff --git a/crates/oxc_parser/src/ts/list.rs b/crates/oxc_parser/src/ts/list.rs index 2b0ddfa410bdc..c8875ae37e8ce 100644 --- a/crates/oxc_parser/src/ts/list.rs +++ b/crates/oxc_parser/src/ts/list.rs @@ -62,7 +62,7 @@ impl<'a> SeparatedList<'a> for TSTupleElementList<'a> { type_annotation: TSType::TSNamedTupleMember(p.ast.alloc(TSNamedTupleMember { span: p.end_span(member_span), element_type, - label, + label: p.ast.alloc(label), optional: false, // A tuple member cannot be both optional and rest. (TS5085) })), }))); @@ -75,7 +75,12 @@ impl<'a> SeparatedList<'a> for TSTupleElementList<'a> { let element_type = p.parse_ts_type()?; self.elements.push(TSTupleElement::TSNamedTupleMember(p.ast.alloc( - TSNamedTupleMember { span: p.end_span(span), element_type, label, optional }, + TSNamedTupleMember { + span: p.end_span(span), + element_type, + label: p.ast.alloc(label), + optional, + }, ))); return Ok(()); @@ -195,12 +200,12 @@ impl<'a> SeparatedList<'a> for TSImportAttributeList<'a> { fn parse_element(&mut self, p: &mut ParserImpl<'a>) -> Result<()> { let span = p.start_span(); - let name = match p.cur_kind() { - Kind::Str => { - let str_lit = p.parse_literal_string()?; - TSImportAttributeName::StringLiteral(p.ast.alloc(str_lit)) - } - _ => TSImportAttributeName::Identifier(p.parse_identifier_name()?), + let name = if p.cur_kind() == Kind::Str { + let str_lit = p.parse_literal_string()?; + TSImportAttributeName::StringLiteral(p.ast.alloc(str_lit)) + } else { + let id = p.parse_identifier_name()?; + TSImportAttributeName::Identifier(p.ast.alloc(id)) }; p.expect(Kind::Colon)?; diff --git a/crates/oxc_parser/src/ts/statement.rs b/crates/oxc_parser/src/ts/statement.rs index c5c1022c58512..209a50d75e17c 100644 --- a/crates/oxc_parser/src/ts/statement.rs +++ b/crates/oxc_parser/src/ts/statement.rs @@ -240,12 +240,13 @@ impl<'a> ParserImpl<'a> { kind: TSModuleDeclarationKind, modifiers: Modifiers<'a>, ) -> Result>> { - let id = match self.cur_kind() { - Kind::Str => self - .parse_literal_string() - .map(|str_lit| TSModuleDeclarationName::StringLiteral(self.ast.alloc(str_lit))), - _ => self.parse_identifier_name().map(TSModuleDeclarationName::Identifier), - }?; + let id = if self.cur_kind() == Kind::Str { + let str_lit = self.parse_literal_string()?; + TSModuleDeclarationName::StringLiteral(self.ast.alloc(str_lit)) + } else { + let id = self.parse_identifier_name()?; + TSModuleDeclarationName::Identifier(self.ast.alloc(id)) + }; let body = if self.eat(Kind::Dot) { let span = self.start_span(); diff --git a/crates/oxc_parser/src/ts/types.rs b/crates/oxc_parser/src/ts/types.rs index 908d49c4d01df..801dc834c9109 100644 --- a/crates/oxc_parser/src/ts/types.rs +++ b/crates/oxc_parser/src/ts/types.rs @@ -500,7 +500,7 @@ impl<'a> ParserImpl<'a> { left = TSTypeName::QualifiedName(self.ast.alloc(TSQualifiedName { span: self.end_span(span), left, - right, + right: self.ast.alloc(right), })); } Ok(left) diff --git a/crates/oxc_transformer/src/helpers/module_imports.rs b/crates/oxc_transformer/src/helpers/module_imports.rs index 99f7c6575c54b..cacb2fda1ae17 100644 --- a/crates/oxc_transformer/src/helpers/module_imports.rs +++ b/crates/oxc_transformer/src/helpers/module_imports.rs @@ -84,20 +84,21 @@ impl<'a> ModuleImports<'a> { source: &CompactStr, names: std::vec::Vec, ) -> Statement<'a> { - let specifiers = self.ast.new_vec_from_iter(names.into_iter().map(|name| { - ImportDeclarationSpecifier::ImportSpecifier(self.ast.alloc(ImportSpecifier { - span: SPAN, - imported: ModuleExportName::Identifier(IdentifierName::new( - SPAN, - self.ast.new_atom(name.imported.as_str()), - )), - local: self.ast.alloc(BindingIdentifier::new( - SPAN, - self.ast.new_atom(name.local.unwrap_or(name.imported).as_str()), - )), - import_kind: ImportOrExportKind::Value, - })) - })); + let specifiers = + self.ast.new_vec_from_iter(names.into_iter().map(|name| { + ImportDeclarationSpecifier::ImportSpecifier(self.ast.alloc(ImportSpecifier { + span: SPAN, + imported: ModuleExportName::Identifier(self.ast.alloc(IdentifierName::new( + SPAN, + self.ast.new_atom(name.imported.as_str()), + ))), + local: self.ast.alloc(BindingIdentifier::new( + SPAN, + self.ast.new_atom(name.local.unwrap_or(name.imported).as_str()), + )), + import_kind: ImportOrExportKind::Value, + })) + })); let import_stmt = self.ast.import_declaration( SPAN, Some(specifiers), diff --git a/tasks/inspect_ast/lib/index.mjs b/tasks/inspect_ast/lib/index.mjs index 36003776b5132..1a3a3b4849b46 100644 --- a/tasks/inspect_ast/lib/index.mjs +++ b/tasks/inspect_ast/lib/index.mjs @@ -119,7 +119,7 @@ const structsThatNeedBoxing = Object.values(types).filter(type => ( ) && ![ 'Atom', 'Span', 'SourceType', 'ReferenceId', 'SymbolId', 'EmptyObject', 'RegExp', - 'TemplateElementValue', 'IdentifierName', 'Modifiers', 'JSXOpeningFragment', 'JSXClosingFragment' + 'TemplateElementValue', 'Modifiers', 'JSXOpeningFragment', 'JSXClosingFragment' ].includes(type.name) )).map(type => type.name).sort(); console.log(structsThatNeedBoxing.join('\n'));