refactor(ast): remove unnecessary ModuleDeclarationKind

This commit is contained in:
Boshen 2023-05-05 21:55:44 +08:00
parent 8ca5c4ce2e
commit a2be3bd227
No known key found for this signature in database
GPG key ID: 9C7A8C8AB22BEBD1
13 changed files with 119 additions and 91 deletions

View file

@ -1604,19 +1604,9 @@ pub struct StaticBlock<'a> {
pub body: Vec<'a, Statement<'a>>,
}
/// Imports
#[derive(Debug, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct ModuleDeclaration<'a> {
#[cfg_attr(feature = "serde", serde(flatten))]
pub span: Span,
#[cfg_attr(feature = "serde", serde(flatten))]
pub kind: ModuleDeclarationKind<'a>,
}
#[derive(Debug, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(untagged))]
pub enum ModuleDeclarationKind<'a> {
pub enum ModuleDeclaration<'a> {
ImportDeclaration(Box<'a, ImportDeclaration<'a>>),
ExportAllDeclaration(Box<'a, ExportAllDeclaration<'a>>),
ExportDefaultDeclaration(Box<'a, ExportDefaultDeclaration<'a>>),
@ -1626,7 +1616,7 @@ pub enum ModuleDeclarationKind<'a> {
TSNamespaceExportDeclaration(Box<'a, TSNamespaceExportDeclaration>),
}
impl<'a> ModuleDeclarationKind<'a> {
impl<'a> ModuleDeclaration<'a> {
#[must_use]
pub fn is_export(&self) -> bool {
matches!(
@ -1663,6 +1653,8 @@ pub struct ImportExpression<'a> {
#[derive(Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type", rename_all = "camelCase"))]
pub struct ImportDeclaration<'a> {
#[cfg_attr(feature = "serde", serde(flatten))]
pub span: Span,
pub specifiers: Vec<'a, ImportDeclarationSpecifier>,
pub source: StringLiteral,
pub assertions: Option<Vec<'a, ImportAttribute>>, // Some(vec![]) for empty assertion
@ -1735,6 +1727,8 @@ impl ImportAttributeKey {
#[derive(Debug, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))]
pub struct ExportNamedDeclaration<'a> {
#[cfg_attr(feature = "serde", serde(flatten))]
pub span: Span,
pub declaration: Option<Declaration<'a>>,
pub specifiers: Vec<'a, ExportSpecifier>,
pub source: Option<StringLiteral>,
@ -1752,6 +1746,8 @@ impl<'a> ExportNamedDeclaration<'a> {
#[derive(Debug, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))]
pub struct ExportDefaultDeclaration<'a> {
#[cfg_attr(feature = "serde", serde(flatten))]
pub span: Span,
pub declaration: ExportDefaultDeclarationKind<'a>,
pub exported: ModuleExportName, // `default`
}
@ -1759,6 +1755,8 @@ pub struct ExportDefaultDeclaration<'a> {
#[derive(Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))]
pub struct ExportAllDeclaration<'a> {
#[cfg_attr(feature = "serde", serde(flatten))]
pub span: Span,
pub exported: Option<ModuleExportName>,
pub source: StringLiteral,
pub assertions: Option<Vec<'a, ImportAttribute>>, // Some(vec![]) for empty assertion

View file

@ -783,46 +783,50 @@ impl<'a> AstBuilder<'a> {
/* ---------- Modules ---------- */
pub fn module_declaration(&self, span: Span, kind: ModuleDeclarationKind<'a>) -> Statement<'a> {
Statement::ModuleDeclaration(self.alloc(ModuleDeclaration { span, kind }))
pub fn module_declaration(&self, decl: ModuleDeclaration<'a>) -> Statement<'a> {
Statement::ModuleDeclaration(self.alloc(decl))
}
pub fn import_declaration(
&self,
span: Span,
specifiers: Vec<'a, ImportDeclarationSpecifier>,
source: StringLiteral,
assertions: Option<Vec<'a, ImportAttribute>>,
import_kind: Option<ImportOrExportKind>,
) -> Box<'a, ImportDeclaration<'a>> {
self.alloc(ImportDeclaration { specifiers, source, assertions, import_kind })
self.alloc(ImportDeclaration { span, specifiers, source, assertions, import_kind })
}
pub fn export_all_declaration(
&self,
span: Span,
exported: Option<ModuleExportName>,
source: StringLiteral,
assertions: Option<Vec<'a, ImportAttribute>>, // Some(vec![]) for empty assertion
export_kind: Option<ImportOrExportKind>,
) -> Box<'a, ExportAllDeclaration<'a>> {
self.alloc(ExportAllDeclaration { exported, source, assertions, export_kind })
self.alloc(ExportAllDeclaration { span, exported, source, assertions, export_kind })
}
pub fn export_default_declaration(
&self,
span: Span,
declaration: ExportDefaultDeclarationKind<'a>,
exported: ModuleExportName,
) -> Box<'a, ExportDefaultDeclaration<'a>> {
self.alloc(ExportDefaultDeclaration { declaration, exported })
self.alloc(ExportDefaultDeclaration { span, declaration, exported })
}
pub fn export_named_declaration(
&self,
span: Span,
declaration: Option<Declaration<'a>>,
specifiers: Vec<'a, ExportSpecifier>,
source: Option<StringLiteral>,
export_kind: Option<ImportOrExportKind>, // `export type { foo }`
) -> Box<'a, ExportNamedDeclaration<'a>> {
self.alloc(ExportNamedDeclaration { declaration, specifiers, source, export_kind })
self.alloc(ExportNamedDeclaration { span, declaration, specifiers, source, export_kind })
}
/* ---------- JSX ----------------- */

View file

@ -336,7 +336,7 @@ impl<'a> GetSpan for AstKind<'a> {
Self::Decorator(x) => x.span,
Self::ModuleDeclaration(x) => x.span,
Self::ModuleDeclaration(x) => x.span(),
Self::JSXOpeningElement(x) => x.span,
Self::JSXElementName(x) => x.span(),

View file

@ -65,7 +65,7 @@ fn no_bloat_enum_sizes() {
assert_eq!(size_of::<Expression>(), 16);
assert_eq!(size_of::<Declaration>(), 16);
assert_eq!(size_of::<BindingPatternKind>(), 16);
assert_eq!(size_of::<ModuleDeclarationKind>(), 16);
assert_eq!(size_of::<ModuleDeclaration>(), 16);
assert_eq!(size_of::<ClassElement>(), 16);
assert_eq!(size_of::<ExportDefaultDeclarationKind>(), 16);
assert_eq!(size_of::<AssignmentTargetPattern>(), 16);

View file

@ -24,7 +24,7 @@ impl<'a> GetSpan for Statement<'a> {
Self::TryStatement(stmt) => stmt.span,
Self::WhileStatement(stmt) => stmt.span,
Self::WithStatement(stmt) => stmt.span,
Self::ModuleDeclaration(decl) => decl.span,
Self::ModuleDeclaration(decl) => decl.span(),
Self::Declaration(decl) => decl.span(),
}
}
@ -153,6 +153,19 @@ impl GetSpan for ModuleExportName {
}
}
impl<'a> GetSpan for ModuleDeclaration<'a> {
fn span(&self) -> Span {
match self {
Self::ImportDeclaration(decl) => decl.span,
Self::ExportAllDeclaration(decl) => decl.span,
Self::ExportDefaultDeclaration(decl) => decl.span,
Self::ExportNamedDeclaration(decl) => decl.span,
Self::TSExportAssignment(decl) => decl.span,
Self::TSNamespaceExportDeclaration(decl) => decl.span,
}
}
}
impl<'a> GetSpan for Declaration<'a> {
fn span(&self) -> Span {
match self {

View file

@ -130,7 +130,7 @@ impl<'a> BoundNames for FormalParameter<'a> {
impl<'a> BoundNames for ModuleDeclaration<'a> {
fn bound_names<F: FnMut(&BindingIdentifier)>(&self, f: &mut F) {
if let ModuleDeclarationKind::ImportDeclaration(decl) = &self.kind {
if let ModuleDeclaration::ImportDeclaration(decl) = &self {
decl.bound_names(f);
}
}

View file

@ -1147,23 +1147,23 @@ pub trait Visit<'a>: Sized {
fn visit_module_declaration(&mut self, decl: &'a ModuleDeclaration<'a>) {
let kind = AstKind::ModuleDeclaration(decl);
self.enter_node(kind);
match &decl.kind {
ModuleDeclarationKind::ImportDeclaration(decl) => {
match decl {
ModuleDeclaration::ImportDeclaration(decl) => {
self.visit_import_declaration(decl);
}
ModuleDeclarationKind::ExportAllDeclaration(decl) => {
ModuleDeclaration::ExportAllDeclaration(decl) => {
self.visit_export_all_declaration(decl);
}
ModuleDeclarationKind::ExportDefaultDeclaration(decl) => {
ModuleDeclaration::ExportDefaultDeclaration(decl) => {
self.visit_export_default_declaration(decl);
}
ModuleDeclarationKind::ExportNamedDeclaration(decl) => {
ModuleDeclaration::ExportNamedDeclaration(decl) => {
self.visit_export_named_declaration(decl);
}
ModuleDeclarationKind::TSExportAssignment(decl) => {
ModuleDeclaration::TSExportAssignment(decl) => {
self.visit_expression(&decl.expression);
}
ModuleDeclarationKind::TSNamespaceExportDeclaration(_) => {}
ModuleDeclaration::TSNamespaceExportDeclaration(_) => {}
}
self.leave_node(kind);
}

View file

@ -865,23 +865,23 @@ pub trait VisitMut<'a, 'b>: Sized {
/* ---------- Module ---------- */
fn visit_module_declaration(&mut self, decl: &'b mut ModuleDeclaration<'a>) {
match &mut decl.kind {
ModuleDeclarationKind::ImportDeclaration(decl) => {
match decl {
ModuleDeclaration::ImportDeclaration(decl) => {
self.visit_import_declaration(decl);
}
ModuleDeclarationKind::ExportAllDeclaration(decl) => {
ModuleDeclaration::ExportAllDeclaration(decl) => {
self.visit_export_all_declaration(decl);
}
ModuleDeclarationKind::ExportDefaultDeclaration(decl) => {
ModuleDeclaration::ExportDefaultDeclaration(decl) => {
self.visit_export_default_declaration(decl);
}
ModuleDeclarationKind::ExportNamedDeclaration(decl) => {
ModuleDeclaration::ExportNamedDeclaration(decl) => {
self.visit_export_named_declaration(decl);
}
ModuleDeclarationKind::TSExportAssignment(decl) => {
ModuleDeclaration::TSExportAssignment(decl) => {
self.visit_expression(&mut decl.expression);
}
ModuleDeclarationKind::TSNamespaceExportDeclaration(_) => {}
ModuleDeclaration::TSNamespaceExportDeclaration(_) => {}
}
}

View file

@ -58,16 +58,15 @@ impl<'a> Parser<'a> {
let source = self.parse_literal_string()?;
let assertions = self.parse_import_attributes()?;
self.asi()?;
let span = self.end_span(span);
let kind = ModuleDeclarationKind::ImportDeclaration(self.ast.import_declaration(
let decl = ModuleDeclaration::ImportDeclaration(self.ast.import_declaration(
span,
specifiers,
source,
assertions,
import_kind,
));
Ok(self.ast.module_declaration(span, kind))
Ok(self.ast.module_declaration(decl))
}
// Full Syntax: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#syntax>
@ -176,36 +175,36 @@ impl<'a> Parser<'a> {
/// [Exports](https://tc39.es/ecma262/#sec-exports)
pub(crate) fn parse_export_declaration(&mut self) -> Result<Statement<'a>> {
let start_span = self.start_span();
let span = self.start_span();
self.bump_any(); // advance `export`
let kind = match self.cur_kind() {
let decl = match self.cur_kind() {
Kind::Eq if self.ts_enabled() => self
.parse_ts_export_assignment_declaration()
.map(ModuleDeclarationKind::TSExportAssignment),
.map(ModuleDeclaration::TSExportAssignment),
Kind::As if self.peek_at(Kind::Namespace) && self.ts_enabled() => self
.parse_ts_export_namespace()
.map(ModuleDeclarationKind::TSNamespaceExportDeclaration),
.map(ModuleDeclaration::TSNamespaceExportDeclaration),
Kind::Default => self
.parse_export_default_declaration()
.map(ModuleDeclarationKind::ExportDefaultDeclaration),
.parse_export_default_declaration(span)
.map(ModuleDeclaration::ExportDefaultDeclaration),
Kind::Star => {
self.parse_export_all_declaration().map(ModuleDeclarationKind::ExportAllDeclaration)
self.parse_export_all_declaration(span).map(ModuleDeclaration::ExportAllDeclaration)
}
Kind::LCurly => self
.parse_export_named_specifiers()
.map(ModuleDeclarationKind::ExportNamedDeclaration),
.parse_export_named_specifiers(span)
.map(ModuleDeclaration::ExportNamedDeclaration),
Kind::Type if self.peek_at(Kind::LCurly) && self.ts_enabled() => self
.parse_export_named_specifiers()
.map(ModuleDeclarationKind::ExportNamedDeclaration),
.parse_export_named_specifiers(span)
.map(ModuleDeclaration::ExportNamedDeclaration),
Kind::Type if self.peek_at(Kind::Star) => {
self.parse_export_all_declaration().map(ModuleDeclarationKind::ExportAllDeclaration)
self.parse_export_all_declaration(span).map(ModuleDeclaration::ExportAllDeclaration)
}
_ => self
.parse_export_named_declaration()
.map(ModuleDeclarationKind::ExportNamedDeclaration),
.parse_export_named_declaration(span)
.map(ModuleDeclaration::ExportNamedDeclaration),
}?;
Ok(self.ast.module_declaration(self.end_span(start_span), kind))
Ok(self.ast.module_declaration(decl))
}
// export NamedExports ;
@ -219,7 +218,10 @@ impl<'a> Parser<'a> {
// ExportSpecifier :
// ModuleExportName
// ModuleExportName as ModuleExportName
fn parse_export_named_specifiers(&mut self) -> Result<Box<'a, ExportNamedDeclaration<'a>>> {
fn parse_export_named_specifiers(
&mut self,
span: Span,
) -> Result<Box<'a, ExportNamedDeclaration<'a>>> {
let export_kind = self.parse_import_or_export_kind();
let ctx = self.ctx;
@ -249,12 +251,16 @@ impl<'a> Parser<'a> {
}
self.asi()?;
Ok(self.ast.export_named_declaration(None, specifiers, source, export_kind))
let span = self.end_span(span);
Ok(self.ast.export_named_declaration(span, None, specifiers, source, export_kind))
}
// export Declaration
fn parse_export_named_declaration(&mut self) -> Result<Box<'a, ExportNamedDeclaration<'a>>> {
let start_span = self.start_span();
fn parse_export_named_declaration(
&mut self,
span: Span,
) -> Result<Box<'a, ExportNamedDeclaration<'a>>> {
let decl_span = self.start_span();
// For tc39/proposal-decorators
// For more information, please refer to <https://babeljs.io/docs/babel-plugin-proposal-decorators#decoratorsbeforeexport>
self.eat_decorators()?;
@ -264,8 +270,15 @@ impl<'a> Parser<'a> {
Modifiers::empty()
};
let declaration = self.parse_declaration(start_span, modifiers)?;
Ok(self.ast.export_named_declaration(Some(declaration), self.ast.new_vec(), None, None))
let declaration = self.parse_declaration(decl_span, modifiers)?;
let span = self.end_span(span);
Ok(self.ast.export_named_declaration(
span,
Some(declaration),
self.ast.new_vec(),
None,
None,
))
}
// export default HoistableDeclaration[~Yield, +Await, +Default]
@ -273,27 +286,28 @@ impl<'a> Parser<'a> {
// export default AssignmentExpression[+In, ~Yield, +Await] ;
fn parse_export_default_declaration(
&mut self,
span: Span,
) -> Result<Box<'a, ExportDefaultDeclaration<'a>>> {
let exported = self.parse_keyword_identifier(Kind::Default);
let start_span = self.start_span();
let decl_span = self.start_span();
// For tc39/proposal-decorators
// For more information, please refer to <https://babeljs.io/docs/babel-plugin-proposal-decorators#decoratorsbeforeexport>
self.eat_decorators()?;
let declaration = match self.cur_kind() {
Kind::Class => self
.parse_class_declaration(start_span, /* modifiers */ Modifiers::empty())
.parse_class_declaration(decl_span, /* modifiers */ Modifiers::empty())
.map(ExportDefaultDeclarationKind::ClassDeclaration)?,
_ if self.at(Kind::Abstract) && self.peek_at(Kind::Class) && self.ts_enabled() => {
// eat the abstract modifier
let (_, modifiers) = self.eat_modifiers_before_declaration();
self.parse_class_declaration(start_span, modifiers)
self.parse_class_declaration(decl_span, modifiers)
.map(ExportDefaultDeclarationKind::ClassDeclaration)?
}
_ if self.at(Kind::Interface)
&& !self.peek_token().is_on_new_line
&& self.ts_enabled() =>
{
self.parse_ts_interface_declaration(start_span, Modifiers::empty()).map(|decl| {
self.parse_ts_interface_declaration(decl_span, Modifiers::empty()).map(|decl| {
match decl {
Declaration::TSInterfaceDeclaration(decl) => {
ExportDefaultDeclarationKind::TSInterfaceDeclaration(decl)
@ -314,7 +328,8 @@ impl<'a> Parser<'a> {
}
};
let exported = ModuleExportName::Identifier(exported);
Ok(self.ast.export_default_declaration(declaration, exported))
let span = self.end_span(span);
Ok(self.ast.export_default_declaration(span, declaration, exported))
}
// export ExportFromClause FromClause ;
@ -322,7 +337,10 @@ impl<'a> Parser<'a> {
// *
// * as ModuleExportName
// NamedExports
fn parse_export_all_declaration(&mut self) -> Result<Box<'a, ExportAllDeclaration<'a>>> {
fn parse_export_all_declaration(
&mut self,
span: Span,
) -> Result<Box<'a, ExportAllDeclaration<'a>>> {
let export_kind = self.parse_import_or_export_kind();
self.bump_any(); // bump `star`
let exported = self.eat(Kind::As).then(|| self.parse_module_export_name()).transpose()?;
@ -330,7 +348,8 @@ impl<'a> Parser<'a> {
let source = self.parse_literal_string()?;
let assertions = self.parse_import_attributes()?;
self.asi()?;
Ok(self.ast.export_all_declaration(exported, source, assertions, export_kind))
let span = self.end_span(span);
Ok(self.ast.export_all_declaration(span, exported, source, assertions, export_kind))
}
// ImportSpecifier :

View file

@ -420,12 +420,6 @@ impl Gen for DebuggerStatement {
}
impl<'a> Gen for ModuleDeclaration<'a> {
fn gen(&self, p: &mut Printer) {
self.kind.gen(p);
}
}
impl<'a> Gen for ModuleDeclarationKind<'a> {
fn gen(&self, p: &mut Printer) {
match self {
Self::ImportDeclaration(decl) => decl.gen(p),

View file

@ -392,7 +392,7 @@ impl<'a> SemanticBuilder<'a> {
}
fn symbol_flag_from_module_declaration(module: &ModuleDeclaration) -> SymbolFlags {
if matches!(&module.kind, ModuleDeclarationKind::ImportDeclaration(_)) {
if matches!(module, ModuleDeclaration::ImportDeclaration(_)) {
SymbolFlags::Import
} else {
SymbolFlags::Export

View file

@ -41,7 +41,7 @@ impl EarlyErrorJavaScript {
AstKind::Directive(dir) => check_directive(dir, node, ctx),
AstKind::ModuleDeclaration(decl) => {
check_module_declaration(decl, node, ctx);
if let ModuleDeclarationKind::ImportDeclaration(import_decl) = &decl.kind {
if let ModuleDeclaration::ImportDeclaration(import_decl) = decl {
check_import_declaration(import_decl, ctx);
}
}
@ -452,15 +452,15 @@ fn check_module_declaration<'a>(
return;
}
let text = match decl.kind {
ModuleDeclarationKind::ImportDeclaration(_) => "import statement",
ModuleDeclarationKind::ExportAllDeclaration(_)
| ModuleDeclarationKind::ExportDefaultDeclaration(_)
| ModuleDeclarationKind::ExportNamedDeclaration(_)
| ModuleDeclarationKind::TSExportAssignment(_)
| ModuleDeclarationKind::TSNamespaceExportDeclaration(_) => "export statement",
let text = match decl {
ModuleDeclaration::ImportDeclaration(_) => "import statement",
ModuleDeclaration::ExportAllDeclaration(_)
| ModuleDeclaration::ExportDefaultDeclaration(_)
| ModuleDeclaration::ExportNamedDeclaration(_)
| ModuleDeclaration::TSExportAssignment(_)
| ModuleDeclaration::TSNamespaceExportDeclaration(_) => "export statement",
};
let span = Span::new(decl.span.start, decl.span.start + 6);
let span = Span::new(decl.span().start, decl.span().start + 6);
match ctx.source_type.module_kind() {
ModuleKind::Script => {
ctx.error(ModuleCode(text, span));

View file

@ -141,21 +141,21 @@ impl ModuleRecordBuilder {
}
pub fn visit_module_declaration(&mut self, module_decl: &ModuleDeclaration) {
match &module_decl.kind {
ModuleDeclarationKind::ImportDeclaration(import_decl) => {
match module_decl {
ModuleDeclaration::ImportDeclaration(import_decl) => {
self.visit_import_declaration(import_decl);
}
ModuleDeclarationKind::ExportAllDeclaration(export_all_decl) => {
ModuleDeclaration::ExportAllDeclaration(export_all_decl) => {
self.visit_export_all_declaration(export_all_decl);
}
ModuleDeclarationKind::ExportDefaultDeclaration(export_default_decl) => {
ModuleDeclaration::ExportDefaultDeclaration(export_default_decl) => {
self.visit_export_default_declaration(export_default_decl);
}
ModuleDeclarationKind::ExportNamedDeclaration(export_named_decl) => {
ModuleDeclaration::ExportNamedDeclaration(export_named_decl) => {
self.visit_export_named_declaration(export_named_decl);
}
ModuleDeclarationKind::TSExportAssignment(_)
| ModuleDeclarationKind::TSNamespaceExportDeclaration(_) => { /* noop */ }
ModuleDeclaration::TSExportAssignment(_)
| ModuleDeclaration::TSNamespaceExportDeclaration(_) => { /* noop */ }
}
}