refactor(ast): remove Option around all ImportOrExportKind

This commit is contained in:
Boshen 2023-05-05 22:39:29 +08:00
parent a2be3bd227
commit 7e130e56f8
No known key found for this signature in database
GPG key ID: 9C7A8C8AB22BEBD1
4 changed files with 16 additions and 17 deletions

View file

@ -1658,7 +1658,7 @@ pub struct ImportDeclaration<'a> {
pub specifiers: Vec<'a, ImportDeclarationSpecifier>,
pub source: StringLiteral,
pub assertions: Option<Vec<'a, ImportAttribute>>, // Some(vec![]) for empty assertion
pub import_kind: Option<ImportOrExportKind>, // `import type { foo } from 'bar'`
pub import_kind: ImportOrExportKind, // `import type { foo } from 'bar'`
}
#[derive(Debug, PartialEq, Eq, Hash)]
@ -1732,13 +1732,13 @@ pub struct ExportNamedDeclaration<'a> {
pub declaration: Option<Declaration<'a>>,
pub specifiers: Vec<'a, ExportSpecifier>,
pub source: Option<StringLiteral>,
pub export_kind: Option<ImportOrExportKind>, // `export type { foo }`
pub export_kind: ImportOrExportKind, // `export type { foo }`
}
impl<'a> ExportNamedDeclaration<'a> {
#[must_use]
pub fn is_typescript_syntax(&self) -> bool {
self.export_kind == Some(ImportOrExportKind::Type)
self.export_kind == ImportOrExportKind::Type
|| self.declaration.as_ref().map_or(false, Declaration::is_typescript_syntax)
}
}
@ -1760,7 +1760,7 @@ pub struct ExportAllDeclaration<'a> {
pub exported: Option<ModuleExportName>,
pub source: StringLiteral,
pub assertions: Option<Vec<'a, ImportAttribute>>, // Some(vec![]) for empty assertion
pub export_kind: Option<ImportOrExportKind>, // `export type *`
pub export_kind: ImportOrExportKind, // `export type *`
}
#[derive(Debug, PartialEq, Eq, Hash)]

View file

@ -793,7 +793,7 @@ impl<'a> AstBuilder<'a> {
specifiers: Vec<'a, ImportDeclarationSpecifier>,
source: StringLiteral,
assertions: Option<Vec<'a, ImportAttribute>>,
import_kind: Option<ImportOrExportKind>,
import_kind: ImportOrExportKind,
) -> Box<'a, ImportDeclaration<'a>> {
self.alloc(ImportDeclaration { span, specifiers, source, assertions, import_kind })
}
@ -803,8 +803,8 @@ impl<'a> AstBuilder<'a> {
span: Span,
exported: Option<ModuleExportName>,
source: StringLiteral,
assertions: Option<Vec<'a, ImportAttribute>>, // Some(vec![]) for empty assertion
export_kind: Option<ImportOrExportKind>,
assertions: Option<Vec<'a, ImportAttribute>>,
export_kind: ImportOrExportKind,
) -> Box<'a, ExportAllDeclaration<'a>> {
self.alloc(ExportAllDeclaration { span, exported, source, assertions, export_kind })
}
@ -824,7 +824,7 @@ impl<'a> AstBuilder<'a> {
declaration: Option<Declaration<'a>>,
specifiers: Vec<'a, ExportSpecifier>,
source: Option<StringLiteral>,
export_kind: Option<ImportOrExportKind>, // `export type { foo }`
export_kind: ImportOrExportKind,
) -> Box<'a, ExportNamedDeclaration<'a>> {
self.alloc(ExportNamedDeclaration { span, declaration, specifiers, source, export_kind })
}

View file

@ -277,7 +277,7 @@ impl<'a> Parser<'a> {
Some(declaration),
self.ast.new_vec(),
None,
None,
ImportOrExportKind::Value,
))
}
@ -407,11 +407,10 @@ impl<'a> Parser<'a> {
}
}
fn parse_import_or_export_kind(&mut self) -> Option<ImportOrExportKind> {
fn parse_import_or_export_kind(&mut self) -> ImportOrExportKind {
if !self.ts_enabled() {
return None;
return ImportOrExportKind::Value;
}
// import type { bar } from 'foo';
// import type * as React from 'react';
// import type ident from 'foo';
@ -419,9 +418,9 @@ impl<'a> Parser<'a> {
if matches!(self.peek_kind(), Kind::LCurly | Kind::Star | Kind::Ident)
&& self.eat(Kind::Type)
{
return Some(ImportOrExportKind::Type);
ImportOrExportKind::Type
} else {
ImportOrExportKind::Value
}
Some(ImportOrExportKind::Value)
}
}

View file

@ -160,7 +160,7 @@ impl ModuleRecordBuilder {
}
fn visit_import_declaration(&mut self, decl: &ImportDeclaration) {
if decl.import_kind.map_or(false, |kind| kind.is_type()) {
if decl.import_kind.is_type() {
return;
}
let module_request = NameSpan::new(decl.source.value.clone(), decl.source.span);
@ -242,7 +242,7 @@ impl ModuleRecordBuilder {
}
fn visit_export_named_declaration(&mut self, decl: &ExportNamedDeclaration) {
if decl.export_kind.map_or(false, |kind| kind.is_type()) {
if decl.export_kind.is_type() {
return;
}
// ignore all TypeScript syntax as they overload