Skip to content

Commit 3ed7243

Browse files
authored
fix(es/flow): avoid restoring module context when flow syntax is enabled (#11819)
**Related issue:** - #11808 - #11817
1 parent 28a7fad commit 3ed7243

1 file changed

Lines changed: 9 additions & 21 deletions

File tree

crates/swc_ecma_transforms_typescript/src/typescript.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use swc_ecma_transforms_react::{parse_expr_for_jsx, JsxDirectives};
88
use swc_ecma_visit::{visit_mut_pass, VisitMut, VisitMutWith};
99

1010
pub use crate::config::*;
11-
use crate::{retain::IsConcrete, semantic::analyze_program, transform::transform};
11+
use crate::{semantic::analyze_program, transform::transform};
1212

1313
macro_rules! static_str {
1414
($s:expr) => {
@@ -41,7 +41,12 @@ pub(crate) struct TypeScript {
4141

4242
impl Pass for TypeScript {
4343
fn process(&mut self, n: &mut Program) {
44-
let was_module = n.as_module().and_then(|m| self.get_last_module_span(m));
44+
let last_module_span = n
45+
.as_module()
46+
// Flow does not need to restore module context
47+
.filter(|_| !self.config.flow_syntax)
48+
.and_then(|m| self.get_last_module_span(m));
49+
4550
let semantic = analyze_program(
4651
n,
4752
self.unresolved_mark,
@@ -61,7 +66,7 @@ impl Pass for TypeScript {
6166
self.config.flow_syntax,
6267
));
6368

64-
if let Some(span) = was_module {
69+
if let Some(span) = last_module_span {
6570
let module = n.as_mut_module().unwrap();
6671
Self::restore_esm_ctx(module, span);
6772
}
@@ -77,14 +82,7 @@ impl TypeScript {
7782
n.body
7883
.iter()
7984
.rev()
80-
.find(|module_item| {
81-
module_item.as_module_decl().is_some_and(|module_decl| {
82-
match self.config.flow_syntax {
83-
true => module_decl.is_runtime_esm_decl(),
84-
false => module_decl.is_es_module_decl(),
85-
}
86-
})
87-
})
85+
.find(|m| m.is_es_module_decl())
8886
.map(Spanned::span)
8987
}
9088

@@ -105,7 +103,6 @@ impl TypeScript {
105103

106104
trait EsModuleDecl {
107105
fn is_es_module_decl(&self) -> bool;
108-
fn is_runtime_esm_decl(&self) -> bool;
109106
}
110107

111108
impl EsModuleDecl for ModuleDecl {
@@ -127,22 +124,13 @@ impl EsModuleDecl for ModuleDecl {
127124
_ => panic!("unable to access unknown nodes"),
128125
}
129126
}
130-
131-
fn is_runtime_esm_decl(&self) -> bool {
132-
self.is_es_module_decl() && self.is_concrete()
133-
}
134127
}
135128

136129
impl EsModuleDecl for ModuleItem {
137130
fn is_es_module_decl(&self) -> bool {
138131
self.as_module_decl()
139132
.is_some_and(ModuleDecl::is_es_module_decl)
140133
}
141-
142-
fn is_runtime_esm_decl(&self) -> bool {
143-
self.as_module_decl()
144-
.is_some_and(ModuleDecl::is_runtime_esm_decl)
145-
}
146134
}
147135

148136
pub fn tsx<C>(

0 commit comments

Comments
 (0)