feat(ast): add TSModuleDeclaration.kind (#2487)

closes #2395
This commit is contained in:
Boshen 2024-02-24 17:09:31 +08:00 committed by GitHub
parent 5212f7b51e
commit 7a796c4b5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 31 deletions

View file

@ -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))]

View file

@ -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(

View file

@ -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() {
@ -260,7 +261,7 @@ 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()?;
@ -268,24 +269,7 @@ impl<'a> ParserImpl<'a> {
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 --------------------- */
@ -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),