Skip to content

Commit

Permalink
Box IdentifierName
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Apr 30, 2024
1 parent 07598d3 commit ecb8159
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 67 deletions.
12 changes: 6 additions & 6 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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>>,
}

Expand All @@ -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>>),
}

Expand Down Expand Up @@ -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>>),
}

Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<'a, TSTypeAnnotation<'a>>>,
}

Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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>>),
}

Expand Down Expand Up @@ -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>>),
}

Expand Down Expand Up @@ -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]
Expand Down
10 changes: 7 additions & 3 deletions crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -652,7 +656,7 @@ impl<'a> AstBuilder<'a> {
MemberExpression::StaticMemberExpression(self.alloc(StaticMemberExpression {
span,
object,
property,
property: self.alloc(property),
optional,
}))
}
Expand Down Expand Up @@ -873,7 +877,7 @@ impl<'a> AstBuilder<'a> {
this: IdentifierName<'a>,
type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
) -> 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(
Expand Down
16 changes: 9 additions & 7 deletions crates/oxc_parser/src/js/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()))
}
Expand Down
33 changes: 18 additions & 15 deletions crates/oxc_parser/src/js/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})))
}
Expand All @@ -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)
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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),
Expand All @@ -430,17 +433,17 @@ impl<'a> ParserImpl<'a> {
// IdentifierName
// StringLiteral
pub(crate) fn parse_module_export_name(&mut self) -> Result<ModuleExportName<'a>> {
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)))
}
}

Expand Down
21 changes: 13 additions & 8 deletions crates/oxc_parser/src/ts/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})),
})));
Expand All @@ -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(());
Expand Down Expand Up @@ -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)?;
Expand Down
13 changes: 7 additions & 6 deletions crates/oxc_parser/src/ts/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,13 @@ impl<'a> ParserImpl<'a> {
kind: TSModuleDeclarationKind,
modifiers: Modifiers<'a>,
) -> Result<Box<'a, TSModuleDeclaration<'a>>> {
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();
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/ts/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 15 additions & 14 deletions crates/oxc_transformer/src/helpers/module_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,21 @@ impl<'a> ModuleImports<'a> {
source: &CompactStr,
names: std::vec::Vec<NamedImport>,
) -> 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),
Expand Down
2 changes: 1 addition & 1 deletion tasks/inspect_ast/lib/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down

0 comments on commit ecb8159

Please sign in to comment.