Skip to content

Commit

Permalink
feat(ast): add TSModuleDeclaration.kind (#2487)
Browse files Browse the repository at this point in the history
closes #2395
  • Loading branch information
Boshen authored Feb 24, 2024
1 parent 5212f7b commit 7a796c4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
19 changes: 19 additions & 0 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,10 +703,29 @@ pub struct TSModuleDeclaration<'a> {
pub span: Span,
pub id: TSModuleDeclarationName,
pub body: TSModuleDeclarationBody<'a>,
/// The keyword used to define this module declaration
/// ```text
/// namespace Foo {}
/// ^^^^^^^^^
/// module 'foo' {}
/// ^^^^^^
/// declare global {}
/// ^^^^^^
/// ```
pub kind: TSModuleDeclarationKind,
/// Valid Modifiers: `declare`, `export`
pub modifiers: Modifiers<'a>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "lowercase"))]
#[cfg_attr(all(feature = "serde", feature = "wasm"), derive(tsify::Tsify))]
pub enum TSModuleDeclarationKind {
Global,
Module,
Namespace,
}

#[derive(Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(untagged))]
#[cfg_attr(all(feature = "serde", feature = "wasm"), derive(tsify::Tsify))]
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,9 +1212,10 @@ impl<'a> AstBuilder<'a> {
span: Span,
id: TSModuleDeclarationName,
body: TSModuleDeclarationBody<'a>,
kind: TSModuleDeclarationKind,
modifiers: Modifiers<'a>,
) -> Box<'a, TSModuleDeclaration<'a>> {
self.alloc(TSModuleDeclaration { span, id, body, modifiers })
self.alloc(TSModuleDeclaration { span, id, body, kind, modifiers })
}

pub fn ts_type_annotation(
Expand Down
51 changes: 21 additions & 30 deletions crates/oxc_parser/src/ts/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ impl<'a> ParserImpl<'a> {
pub(crate) fn parse_ts_namespace_or_module_declaration_body(
&mut self,
span: Span,
kind: TSModuleDeclarationKind,
modifiers: Modifiers<'a>,
) -> Result<Box<'a, TSModuleDeclaration<'a>>> {
let id = match self.cur_kind() {
Expand All @@ -260,32 +261,15 @@ impl<'a> ParserImpl<'a> {
let body = if self.eat(Kind::Dot) {
let span = self.start_span();
let decl =
self.parse_ts_namespace_or_module_declaration_body(span, Modifiers::empty())?;
self.parse_ts_namespace_or_module_declaration_body(span, kind, Modifiers::empty())?;
TSModuleDeclarationBody::TSModuleDeclaration(decl)
} else {
let block = self.parse_ts_module_block()?;
self.asi()?;
TSModuleDeclarationBody::TSModuleBlock(block)
};

Ok(self.ast.ts_module_declaration(self.end_span(span), id, body, modifiers))
}

pub(crate) fn parse_ts_namespace_or_module_declaration(
&mut self,
modifiers: Modifiers<'a>,
) -> Result<Box<'a, TSModuleDeclaration<'a>>> {
let span = self.start_span();
self.expect(Kind::Namespace).or_else(|_| self.expect(Kind::Module))?;
self.parse_ts_namespace_or_module_declaration_body(span, modifiers)
}

pub(crate) fn parse_ts_global_declaration(
&mut self,
start_span: Span,
modifiers: Modifiers<'a>,
) -> Result<Box<'a, TSModuleDeclaration<'a>>> {
self.parse_ts_namespace_or_module_declaration_body(start_span, modifiers)
Ok(self.ast.ts_module_declaration(self.end_span(span), id, body, kind, modifiers))
}

/** ----------------------- declare --------------------- */
Expand All @@ -308,18 +292,25 @@ impl<'a> ParserImpl<'a> {
modifiers: Modifiers<'a>,
) -> Result<Declaration<'a>> {
match self.cur_kind() {
Kind::Namespace | Kind::Module => self
.parse_ts_namespace_or_module_declaration(modifiers)
.map(Declaration::TSModuleDeclaration),
Kind::Namespace => {
let kind = TSModuleDeclarationKind::Namespace;
let span = self.start_span();
self.bump_any();
self.parse_ts_namespace_or_module_declaration_body(span, kind, modifiers)
.map(Declaration::TSModuleDeclaration)
}
Kind::Module => {
let kind = TSModuleDeclarationKind::Module;
let span = self.start_span();
self.bump_any();
self.parse_ts_namespace_or_module_declaration_body(span, kind, modifiers)
.map(Declaration::TSModuleDeclaration)
}
Kind::Global => {
let decl = if self.peek_at(Kind::LCurly) {
// valid syntax for
// declare global { }
self.parse_ts_namespace_or_module_declaration_body(start_span, modifiers)
} else {
self.parse_ts_global_declaration(start_span, modifiers)
}?;
Ok(Declaration::TSModuleDeclaration(decl))
// declare global { }
let kind = TSModuleDeclarationKind::Global;
self.parse_ts_namespace_or_module_declaration_body(start_span, kind, modifiers)
.map(Declaration::TSModuleDeclaration)
}
Kind::Type => self.parse_ts_type_alias_declaration(start_span, modifiers),
Kind::Enum => self.parse_ts_enum_declaration(start_span, modifiers),
Expand Down

0 comments on commit 7a796c4

Please sign in to comment.