@@ -5,7 +5,7 @@ use rustc_hash::FxHashMap;
55
66use super :: FunctionKind ;
77use crate :: {
8- Context , ParserImpl , diagnostics,
8+ Context , ParserImpl , StatementContext , diagnostics,
99 lexer:: Kind ,
1010 modifiers:: { Modifier , ModifierFlags , ModifierKind , Modifiers } ,
1111} ;
@@ -61,7 +61,11 @@ impl<'a> ParserImpl<'a> {
6161 }
6262
6363 /// Section 16.2.2 Import Declaration
64- pub ( crate ) fn parse_import_declaration ( & mut self , span : u32 ) -> Statement < ' a > {
64+ pub ( crate ) fn parse_import_declaration (
65+ & mut self ,
66+ span : u32 ,
67+ should_record_module_record : bool ,
68+ ) -> Statement < ' a > {
6569 let token_after_import = self . cur_token ( ) ;
6670 let mut identifier_after_import: Option < BindingIdentifier < ' _ > > =
6771 if self . cur_kind ( ) . is_binding_identifier ( ) {
@@ -186,16 +190,20 @@ impl<'a> ParserImpl<'a> {
186190 self . asi ( ) ;
187191 let span = self . end_span ( span) ;
188192
189- self . ast
190- . module_declaration_import_declaration (
191- span,
192- specifiers,
193- source,
194- phase,
195- with_clause,
196- import_kind,
197- )
198- . into ( )
193+ let import_decl = self . ast . alloc_import_declaration (
194+ span,
195+ specifiers,
196+ source,
197+ phase,
198+ with_clause,
199+ import_kind,
200+ ) ;
201+
202+ if should_record_module_record {
203+ self . module_record_builder . visit_import_declaration ( & import_decl) ;
204+ }
205+
206+ Statement :: ImportDeclaration ( import_decl)
199207 }
200208
201209 // Full Syntax: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#syntax>
@@ -322,21 +330,29 @@ impl<'a> ParserImpl<'a> {
322330 pub ( crate ) fn parse_ts_export_assignment_declaration (
323331 & mut self ,
324332 start_span : u32 ,
333+ stmt_ctx : StatementContext ,
325334 ) -> Box < ' a , TSExportAssignment < ' a > > {
326335 self . expect ( Kind :: Eq ) ;
327336 let expression = self . parse_assignment_expression_or_higher ( ) ;
328337 self . asi ( ) ;
338+ if stmt_ctx. is_top_level ( ) {
339+ self . module_record_builder . found_ts_export ( ) ;
340+ }
329341 self . ast . alloc_ts_export_assignment ( self . end_span ( start_span) , expression)
330342 }
331343
332344 pub ( crate ) fn parse_ts_export_namespace (
333345 & mut self ,
334346 start_span : u32 ,
347+ stmt_ctx : StatementContext ,
335348 ) -> Box < ' a , TSNamespaceExportDeclaration < ' a > > {
336349 self . expect ( Kind :: As ) ;
337350 self . expect ( Kind :: Namespace ) ;
338351 let id = self . parse_identifier_name ( ) ;
339352 self . asi ( ) ;
353+ if stmt_ctx. is_top_level ( ) {
354+ self . module_record_builder . found_ts_export ( ) ;
355+ }
340356 self . ast . alloc_ts_namespace_export_declaration ( self . end_span ( start_span) , id)
341357 }
342358
@@ -345,23 +361,31 @@ impl<'a> ParserImpl<'a> {
345361 & mut self ,
346362 span : u32 ,
347363 mut decorators : Vec < ' a , Decorator < ' a > > ,
364+ stmt_ctx : StatementContext ,
348365 ) -> Statement < ' a > {
349366 self . bump_any ( ) ; // bump `export`
350367 let decl = match self . cur_kind ( ) {
351368 // `export import A = B`
352369 Kind :: Import => {
353370 let import_span = self . start_span ( ) ;
354371 self . bump_any ( ) ;
355- let stmt = self . parse_import_declaration ( import_span) ;
372+ // Pass `should_record_module_record: false` to prevent an `import` module record
373+ // being created. It's an export not an import.
374+ let stmt = self . parse_import_declaration ( import_span, false ) ;
356375 if stmt. is_declaration ( ) {
357- self . ast . module_declaration_export_named_declaration (
376+ let export_named_decl = self . ast . alloc_export_named_declaration (
358377 self . end_span ( span) ,
359378 Some ( stmt. into_declaration ( ) ) ,
360379 self . ast . vec ( ) ,
361380 None ,
362381 ImportOrExportKind :: Value ,
363382 NONE ,
364- )
383+ ) ;
384+ if stmt_ctx. is_top_level ( ) {
385+ self . module_record_builder
386+ . visit_export_named_declaration ( & export_named_decl) ;
387+ }
388+ ModuleDeclaration :: ExportNamedDeclaration ( export_named_decl)
365389 } else {
366390 return self . fatal_error ( diagnostics:: unexpected_export ( stmt. span ( ) ) ) ;
367391 }
@@ -378,52 +402,56 @@ impl<'a> ParserImpl<'a> {
378402 let modifiers = self . parse_modifiers ( false , false ) ;
379403 let class_decl = self . parse_class_declaration ( class_span, & modifiers, decorators) ;
380404 let decl = Declaration :: ClassDeclaration ( class_decl) ;
381- self . ast . module_declaration_export_named_declaration (
405+ let export_named_decl = self . ast . alloc_export_named_declaration (
382406 self . end_span ( span) ,
383407 Some ( decl) ,
384408 self . ast . vec ( ) ,
385409 None ,
386410 ImportOrExportKind :: Value ,
387411 NONE ,
388- )
412+ ) ;
413+ if stmt_ctx. is_top_level ( ) {
414+ self . module_record_builder . visit_export_named_declaration ( & export_named_decl) ;
415+ }
416+ ModuleDeclaration :: ExportNamedDeclaration ( export_named_decl)
389417 }
390418 Kind :: Eq if self . is_ts => ModuleDeclaration :: TSExportAssignment (
391- self . parse_ts_export_assignment_declaration ( span) ,
419+ self . parse_ts_export_assignment_declaration ( span, stmt_ctx ) ,
392420 ) ,
393421 Kind :: As if self . is_ts && self . lexer . peek_token ( ) . kind ( ) == Kind :: Namespace => {
394422 // `export as namespace ...`
395423 ModuleDeclaration :: TSNamespaceExportDeclaration (
396- self . parse_ts_export_namespace ( span) ,
424+ self . parse_ts_export_namespace ( span, stmt_ctx ) ,
397425 )
398426 }
399427 Kind :: Default => ModuleDeclaration :: ExportDefaultDeclaration (
400- self . parse_export_default_declaration ( span, decorators) ,
428+ self . parse_export_default_declaration ( span, decorators, stmt_ctx) ,
429+ ) ,
430+ Kind :: Star => ModuleDeclaration :: ExportAllDeclaration (
431+ self . parse_export_all_declaration ( span, stmt_ctx) ,
432+ ) ,
433+ Kind :: LCurly => ModuleDeclaration :: ExportNamedDeclaration (
434+ self . parse_export_named_specifiers ( span, stmt_ctx) ,
401435 ) ,
402- Kind :: Star => {
403- ModuleDeclaration :: ExportAllDeclaration ( self . parse_export_all_declaration ( span) )
404- }
405- Kind :: LCurly => {
406- ModuleDeclaration :: ExportNamedDeclaration ( self . parse_export_named_specifiers ( span) )
407- }
408436 Kind :: Type if self . is_ts => {
409437 let next_kind = self . lexer . peek_token ( ) . kind ( ) ;
410438
411439 match next_kind {
412440 // `export type { ...`
413441 Kind :: LCurly => ModuleDeclaration :: ExportNamedDeclaration (
414- self . parse_export_named_specifiers ( span) ,
442+ self . parse_export_named_specifiers ( span, stmt_ctx ) ,
415443 ) ,
416444 // `export type * as ...`
417445 Kind :: Star => ModuleDeclaration :: ExportAllDeclaration (
418- self . parse_export_all_declaration ( span) ,
446+ self . parse_export_all_declaration ( span, stmt_ctx ) ,
419447 ) ,
420448 _ => ModuleDeclaration :: ExportNamedDeclaration (
421- self . parse_export_named_declaration ( span, decorators) ,
449+ self . parse_export_named_declaration ( span, decorators, stmt_ctx ) ,
422450 ) ,
423451 }
424452 }
425453 _ => ModuleDeclaration :: ExportNamedDeclaration (
426- self . parse_export_named_declaration ( span, decorators) ,
454+ self . parse_export_named_declaration ( span, decorators, stmt_ctx ) ,
427455 ) ,
428456 } ;
429457 Statement :: from ( decl)
@@ -440,7 +468,11 @@ impl<'a> ParserImpl<'a> {
440468 // ExportSpecifier :
441469 // ModuleExportName
442470 // ModuleExportName as ModuleExportName
443- fn parse_export_named_specifiers ( & mut self , span : u32 ) -> Box < ' a , ExportNamedDeclaration < ' a > > {
471+ fn parse_export_named_specifiers (
472+ & mut self ,
473+ span : u32 ,
474+ stmt_ctx : StatementContext ,
475+ ) -> Box < ' a , ExportNamedDeclaration < ' a > > {
444476 let export_kind = self . parse_import_or_export_kind ( ) ;
445477 self . expect ( Kind :: LCurly ) ;
446478 let ( mut specifiers, _) = self . context ( Context :: empty ( ) , self . ctx , |p| {
@@ -496,21 +528,26 @@ impl<'a> ParserImpl<'a> {
496528
497529 self . asi ( ) ;
498530 let span = self . end_span ( span) ;
499- self . ast . alloc_export_named_declaration (
531+ let export_named_decl = self . ast . alloc_export_named_declaration (
500532 span,
501533 None ,
502534 specifiers,
503535 source,
504536 export_kind,
505537 with_clause,
506- )
538+ ) ;
539+ if stmt_ctx. is_top_level ( ) {
540+ self . module_record_builder . visit_export_named_declaration ( & export_named_decl) ;
541+ }
542+ export_named_decl
507543 }
508544
509545 // export Declaration
510546 fn parse_export_named_declaration (
511547 & mut self ,
512548 span : u32 ,
513549 decorators : Vec < ' a , Decorator < ' a > > ,
550+ stmt_ctx : StatementContext ,
514551 ) -> Box < ' a , ExportNamedDeclaration < ' a > > {
515552 let decl_span = self . start_span ( ) ;
516553 let reserved_ctx = self . ctx ;
@@ -525,14 +562,18 @@ impl<'a> ParserImpl<'a> {
525562 ImportOrExportKind :: Value
526563 } ;
527564 self . ctx = reserved_ctx;
528- self . ast . alloc_export_named_declaration (
565+ let export_named_decl = self . ast . alloc_export_named_declaration (
529566 self . end_span ( span) ,
530567 Some ( declaration) ,
531568 self . ast . vec ( ) ,
532569 None ,
533570 export_kind,
534571 NONE ,
535- )
572+ ) ;
573+ if stmt_ctx. is_top_level ( ) {
574+ self . module_record_builder . visit_export_named_declaration ( & export_named_decl) ;
575+ }
576+ export_named_decl
536577 }
537578
538579 // export default HoistableDeclaration[~Yield, +Await, +Default]
@@ -542,12 +583,18 @@ impl<'a> ParserImpl<'a> {
542583 & mut self ,
543584 span : u32 ,
544585 decorators : Vec < ' a , Decorator < ' a > > ,
586+ stmt_ctx : StatementContext ,
545587 ) -> Box < ' a , ExportDefaultDeclaration < ' a > > {
546588 let exported = self . parse_keyword_identifier ( Kind :: Default ) ;
547589 let declaration = self . parse_export_default_declaration_kind ( decorators) ;
548590 let exported = ModuleExportName :: IdentifierName ( exported) ;
549591 let span = self . end_span ( span) ;
550- self . ast . alloc_export_default_declaration ( span, exported, declaration)
592+ let export_default_decl =
593+ self . ast . alloc_export_default_declaration ( span, exported, declaration) ;
594+ if stmt_ctx. is_top_level ( ) {
595+ self . module_record_builder . visit_export_default_declaration ( & export_default_decl) ;
596+ }
597+ export_default_decl
551598 }
552599
553600 fn parse_export_default_declaration_kind (
@@ -672,7 +719,11 @@ impl<'a> ParserImpl<'a> {
672719 // *
673720 // * as ModuleExportName
674721 // NamedExports
675- fn parse_export_all_declaration ( & mut self , span : u32 ) -> Box < ' a , ExportAllDeclaration < ' a > > {
722+ fn parse_export_all_declaration (
723+ & mut self ,
724+ span : u32 ,
725+ stmt_ctx : StatementContext ,
726+ ) -> Box < ' a , ExportAllDeclaration < ' a > > {
676727 let export_kind = self . parse_import_or_export_kind ( ) ;
677728 self . bump_any ( ) ; // bump `star`
678729 let exported = self . eat ( Kind :: As ) . then ( || self . parse_module_export_name ( ) ) ;
@@ -681,7 +732,12 @@ impl<'a> ParserImpl<'a> {
681732 let with_clause = self . parse_import_attributes ( ) ;
682733 self . asi ( ) ;
683734 let span = self . end_span ( span) ;
684- self . ast . alloc_export_all_declaration ( span, exported, source, with_clause, export_kind)
735+ let export_all_decl =
736+ self . ast . alloc_export_all_declaration ( span, exported, source, with_clause, export_kind) ;
737+ if stmt_ctx. is_top_level ( ) {
738+ self . module_record_builder . visit_export_all_declaration ( & export_all_decl) ;
739+ }
740+ export_all_decl
685741 }
686742
687743 // ImportSpecifier :
0 commit comments