1+ use bitflags:: bitflags;
12use oxc_allocator:: { Box , Vec } ;
23use oxc_ast:: ast:: * ;
34use oxc_span:: GetSpan ;
45
6+ bitflags ! {
7+ #[ derive( Debug , Clone , Copy ) ]
8+ pub struct ParseModuleDeclarationFlags : u8 {
9+ const Namespace = 1 << 0 ;
10+ const NestedNamespace = 1 << 1 ;
11+ }
12+ }
13+
514use crate :: {
615 ParserImpl , diagnostics,
716 js:: { FunctionKind , VariableDeclarationParent } ,
@@ -260,6 +269,56 @@ impl<'a> ParserImpl<'a> {
260269
261270 /* ----------------------- Namespace & Module ----------------------- */
262271
272+ fn parse_ts_module_declaration (
273+ & mut self ,
274+ span : u32 ,
275+ modifiers : & Modifiers < ' a > ,
276+ ) -> Box < ' a , TSModuleDeclaration < ' a > > {
277+ let mut flags = ParseModuleDeclarationFlags :: empty ( ) ;
278+ let kind;
279+ if self . at ( Kind :: Global ) {
280+ kind = TSModuleDeclarationKind :: Global ;
281+ return self . parse_ambient_external_module_declaration ( span, kind, modifiers) ;
282+ } else if self . eat ( Kind :: Namespace ) {
283+ kind = TSModuleDeclarationKind :: Namespace ;
284+ flags. insert ( ParseModuleDeclarationFlags :: Namespace ) ;
285+ } else {
286+ self . expect ( Kind :: Module ) ;
287+ kind = TSModuleDeclarationKind :: Module ;
288+ if self . at ( Kind :: Str ) {
289+ return self . parse_ambient_external_module_declaration ( span, kind, modifiers) ;
290+ }
291+ }
292+ self . parse_module_or_namespace_declaration ( span, kind, modifiers, flags)
293+ }
294+
295+ fn parse_ambient_external_module_declaration (
296+ & mut self ,
297+ span : u32 ,
298+ kind : TSModuleDeclarationKind ,
299+ modifiers : & Modifiers < ' a > ,
300+ ) -> Box < ' a , TSModuleDeclaration < ' a > > {
301+ let id = if self . at ( Kind :: Global ) {
302+ TSModuleDeclarationName :: Identifier ( self . parse_binding_identifier ( ) )
303+ } else {
304+ TSModuleDeclarationName :: StringLiteral ( self . parse_literal_string ( ) )
305+ } ;
306+ let body = if self . at ( Kind :: LCurly ) {
307+ let block = self . parse_ts_module_block ( ) ;
308+ Some ( TSModuleDeclarationBody :: TSModuleBlock ( block) )
309+ } else {
310+ self . asi ( ) ;
311+ None
312+ } ;
313+ self . ast . alloc_ts_module_declaration (
314+ self . end_span ( span) ,
315+ id,
316+ body,
317+ kind,
318+ modifiers. contains_declare ( ) ,
319+ )
320+ }
321+
263322 fn parse_ts_module_block ( & mut self ) -> Box < ' a , TSModuleBlock < ' a > > {
264323 let span = self . start_span ( ) ;
265324 self . expect ( Kind :: LCurly ) ;
@@ -269,45 +328,41 @@ impl<'a> ParserImpl<'a> {
269328 self . ast . alloc_ts_module_block ( self . end_span ( span) , directives, statements)
270329 }
271330
272- pub ( crate ) fn parse_ts_namespace_or_module_declaration_body (
331+ fn parse_module_or_namespace_declaration (
273332 & mut self ,
274333 span : u32 ,
275334 kind : TSModuleDeclarationKind ,
276335 modifiers : & Modifiers < ' a > ,
336+ flags : ParseModuleDeclarationFlags ,
277337 ) -> Box < ' a , TSModuleDeclaration < ' a > > {
278- self . verify_modifiers (
279- modifiers,
280- ModifierFlags :: DECLARE | ModifierFlags :: EXPORT ,
281- diagnostics:: modifier_cannot_be_used_here,
282- ) ;
283- let id = match self . cur_kind ( ) {
284- Kind :: Str => TSModuleDeclarationName :: StringLiteral ( self . parse_literal_string ( ) ) ,
285- _ => TSModuleDeclarationName :: Identifier ( self . parse_binding_identifier ( ) ) ,
286- } ;
287-
338+ let id = // if flags.intersects(ParseModuleDeclarationFlags::NestedNamespace) {
339+ // TODO: missing identifier name in AST.
340+ // TSModuleDeclarationName::IdentifierName(self.parse_identifier_name());
341+ // } else {
342+ TSModuleDeclarationName :: Identifier ( self . parse_binding_identifier ( ) ) ;
343+ // };
288344 let body = if self . eat ( Kind :: Dot ) {
289345 let span = self . start_span ( ) ;
290- let decl =
291- self . parse_ts_namespace_or_module_declaration_body ( span, kind, & Modifiers :: empty ( ) ) ;
292- Some ( TSModuleDeclarationBody :: TSModuleDeclaration ( decl) )
293- } else if self . at ( Kind :: LCurly ) {
294- let block = self . parse_ts_module_block ( ) ;
295- Some ( TSModuleDeclarationBody :: TSModuleBlock ( block) )
346+ let decl = self . parse_module_or_namespace_declaration (
347+ span,
348+ kind,
349+ & Modifiers :: empty ( ) ,
350+ flags. union ( ParseModuleDeclarationFlags :: NestedNamespace ) ,
351+ ) ;
352+ TSModuleDeclarationBody :: TSModuleDeclaration ( decl)
296353 } else {
297- self . asi ( ) ;
298- None
354+ let block = self . parse_ts_module_block ( ) ;
355+ TSModuleDeclarationBody :: TSModuleBlock ( block )
299356 } ;
300-
301357 self . verify_modifiers (
302358 modifiers,
303359 ModifierFlags :: DECLARE ,
304360 diagnostics:: modifier_cannot_be_used_here,
305361 ) ;
306-
307362 self . ast . alloc_ts_module_declaration (
308363 self . end_span ( span) ,
309364 id,
310- body,
365+ Some ( body) ,
311366 kind,
312367 modifiers. contains_declare ( ) ,
313368 )
@@ -340,25 +395,8 @@ impl<'a> ParserImpl<'a> {
340395 }
341396 }
342397 match kind {
343- Kind :: Namespace => {
344- let kind = TSModuleDeclarationKind :: Namespace ;
345- self . bump_any ( ) ;
346- let decl =
347- self . parse_ts_namespace_or_module_declaration_body ( start_span, kind, modifiers) ;
348- Declaration :: TSModuleDeclaration ( decl)
349- }
350- Kind :: Module => {
351- let kind = TSModuleDeclarationKind :: Module ;
352- self . bump_any ( ) ;
353- let decl =
354- self . parse_ts_namespace_or_module_declaration_body ( start_span, kind, modifiers) ;
355- Declaration :: TSModuleDeclaration ( decl)
356- }
357- Kind :: Global => {
358- // declare global { }
359- let kind = TSModuleDeclarationKind :: Global ;
360- let decl =
361- self . parse_ts_namespace_or_module_declaration_body ( start_span, kind, modifiers) ;
398+ Kind :: Global | Kind :: Module | Kind :: Namespace => {
399+ let decl = self . parse_ts_module_declaration ( start_span, modifiers) ;
362400 Declaration :: TSModuleDeclaration ( decl)
363401 }
364402 Kind :: Type => self . parse_ts_type_alias_declaration ( start_span, modifiers) ,
0 commit comments