Skip to content

Commit 94202de

Browse files
committed
feat(isolated-declarations): add export {} when needed (#3754)
microsoft/TypeScript#58912
1 parent d29316a commit 94202de

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

crates/oxc_isolated_declarations/src/lib.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ impl<'a> IsolatedDeclarations<'a> {
111111
&mut self,
112112
stmts: &oxc_allocator::Vec<'a, Statement<'a>>,
113113
) -> oxc_allocator::Vec<'a, Statement<'a>> {
114+
// https://github.com/microsoft/TypeScript/pull/58912
115+
let mut need_empty_export_marker = true;
116+
114117
let mut new_stmts = Vec::new();
115118
let mut variables_declarations = VecDeque::new();
116119
let mut variable_transformed_indexes = VecDeque::new();
@@ -140,13 +143,14 @@ impl<'a> IsolatedDeclarations<'a> {
140143
new_stmts.push(stmt);
141144
}
142145
match_module_declaration!(Statement) => {
143-
transformed_indexes.push(new_stmts.len());
144146
match stmt.to_module_declaration() {
145147
ModuleDeclaration::ExportDefaultDeclaration(decl) => {
148+
transformed_indexes.push(new_stmts.len());
146149
if let Some((var_decl, new_decl)) =
147150
self.transform_export_default_declaration(decl)
148151
{
149152
if let Some(var_decl) = var_decl {
153+
need_empty_export_marker = false;
150154
self.scope.visit_variable_declaration(&var_decl);
151155
new_stmts.push(Statement::VariableDeclaration(
152156
self.ast.alloc(var_decl),
@@ -161,10 +165,12 @@ impl<'a> IsolatedDeclarations<'a> {
161165
continue;
162166
}
163167

168+
need_empty_export_marker = false;
164169
self.scope.visit_export_default_declaration(decl);
165170
}
166171

167172
ModuleDeclaration::ExportNamedDeclaration(decl) => {
173+
transformed_indexes.push(new_stmts.len());
168174
if let Some(new_decl) = self.transform_export_named_declaration(decl) {
169175
self.scope.visit_declaration(
170176
new_decl.declaration.as_ref().unwrap_or_else(|| unreachable!()),
@@ -175,10 +181,14 @@ impl<'a> IsolatedDeclarations<'a> {
175181
));
176182
continue;
177183
}
178-
184+
need_empty_export_marker = false;
179185
self.scope.visit_export_named_declaration(decl);
180186
}
187+
ModuleDeclaration::ImportDeclaration(_) => {
188+
// We must transform this in the end, because we need to know all references
189+
}
181190
module_declaration => {
191+
transformed_indexes.push(new_stmts.len());
182192
self.scope.visit_module_declaration(module_declaration);
183193
}
184194
}
@@ -190,6 +200,7 @@ impl<'a> IsolatedDeclarations<'a> {
190200
}
191201

192202
// 5. Transform statements until no more transformation can be done
203+
let last_transformed_len = transformed_indexes.len();
193204
let mut last_reference_len = 0;
194205
while last_reference_len != self.scope.references_len() {
195206
last_reference_len = self.scope.references_len();
@@ -259,6 +270,7 @@ impl<'a> IsolatedDeclarations<'a> {
259270
),
260271
);
261272
new_ast_stmts.push(Statement::VariableDeclaration(variables_declaration));
273+
transformed_indexes.push(index);
262274
}
263275
}
264276
Statement::UsingDeclaration(decl) => {
@@ -280,6 +292,7 @@ impl<'a> IsolatedDeclarations<'a> {
280292
),
281293
);
282294
new_ast_stmts.push(Statement::VariableDeclaration(variable_declaration));
295+
transformed_indexes.push(index);
283296
}
284297
}
285298
Statement::ImportDeclaration(decl) => {
@@ -294,6 +307,19 @@ impl<'a> IsolatedDeclarations<'a> {
294307
}
295308
}
296309

310+
if last_transformed_len == transformed_indexes.len() {
311+
need_empty_export_marker = false;
312+
}
313+
314+
if need_empty_export_marker {
315+
let specifiers = self.ast.new_vec();
316+
let kind = ImportOrExportKind::Value;
317+
let empty_export =
318+
self.ast.export_named_declaration(SPAN, None, specifiers, None, kind, None);
319+
new_ast_stmts
320+
.push(Statement::from(ModuleDeclaration::ExportNamedDeclaration(empty_export)));
321+
}
322+
297323
new_ast_stmts
298324
}
299325

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type A = string;
2+
export function a(): A {
3+
return ""
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: crates/oxc_isolated_declarations/tests/mod.rs
3+
input_file: crates/oxc_isolated_declarations/tests/fixtures/empty-export.ts
4+
---
5+
==================== .D.TS ====================
6+
7+
type A = string;
8+
export declare function a(): A;
9+
export {};

0 commit comments

Comments
 (0)