Skip to content

Commit 7284f16

Browse files
committed
fix(isolated-declarations): leading comments of ExportDefaultDeclaration and TSExportAssignment appear in incorrect places (#10559)
close: #10535 https://www.typescriptlang.org/play/?#code/PQKhCgAIUgTBTAZgQwK4BsAul4A8AOA9gE6ZQjDh5GlxJpaQCMADOOKBNJACoDKAUQIlMAQQDO4gJYBzAHYBbeHLLRKVYbQC8kRKjkBjTFMJzdhQgAoAlJADekYvEypiZlgF8gA
1 parent 2eb1f88 commit 7284f16

5 files changed

Lines changed: 33 additions & 11 deletions

File tree

crates/oxc_isolated_declarations/src/module.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,22 @@ impl<'a> IsolatedDeclarations<'a> {
5252
Some((None, decl.declaration.clone_in(self.ast.allocator)))
5353
}
5454
declaration @ match_expression!(ExportDefaultDeclarationKind) => self
55-
.transform_export_expression(declaration.to_expression())
55+
.transform_export_expression(decl.span, declaration.to_expression())
5656
.map(|(var_decl, expr)| (var_decl, ExportDefaultDeclarationKind::from(expr))),
5757
};
5858

5959
declaration.map(|(var_decl, declaration)| {
6060
let exported =
6161
ModuleExportName::IdentifierName(self.ast.identifier_name(SPAN, "default"));
62-
let declaration = self.ast.module_declaration_export_default_declaration(
63-
decl.span,
64-
exported,
65-
declaration,
66-
);
62+
let declaration =
63+
self.ast.module_declaration_export_default_declaration(SPAN, exported, declaration);
6764
(var_decl, Statement::from(declaration))
6865
})
6966
}
7067

7168
fn transform_export_expression(
7269
&self,
70+
decl_span: Span,
7371
expr: &Expression<'a>,
7472
) -> Option<(Option<Statement<'a>>, Expression<'a>)> {
7573
if matches!(expr, Expression::Identifier(_)) {
@@ -92,7 +90,7 @@ impl<'a> IsolatedDeclarations<'a> {
9290
self.ast.vec1(self.ast.variable_declarator(SPAN, kind, id, None, false));
9391

9492
let variable_statement = Statement::from(self.ast.declaration_variable(
95-
SPAN,
93+
decl_span,
9694
kind,
9795
declarations,
9896
self.is_declare(),
@@ -105,10 +103,10 @@ impl<'a> IsolatedDeclarations<'a> {
105103
&self,
106104
decl: &TSExportAssignment<'a>,
107105
) -> Option<(Option<Statement<'a>>, Statement<'a>)> {
108-
self.transform_export_expression(&decl.expression).map(|(var_decl, expr)| {
106+
self.transform_export_expression(decl.span, &decl.expression).map(|(var_decl, expr)| {
109107
(
110108
var_decl,
111-
Statement::from(self.ast.module_declaration_ts_export_assignment(decl.span, expr)),
109+
Statement::from(self.ast.module_declaration_ts_export_assignment(SPAN, expr)),
112110
)
113111
})
114112
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* comment should be a leading comment of the arrow function
3+
*/
4+
export default () => {
5+
return 0;
6+
};
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const Res = 0;
22

3+
/**
4+
* comment should be a leading comment of the function Foo
5+
*/
36
export = function Foo(): typeof Res {
4-
return Res;
5-
}
7+
return Res;
8+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/oxc_isolated_declarations/tests/mod.rs
3+
input_file: crates/oxc_isolated_declarations/tests/fixtures/export-default2.ts
4+
---
5+
```
6+
==================== .D.TS ====================
7+
8+
/**
9+
* comment should be a leading comment of the arrow function
10+
*/
11+
declare const _default: () => number;
12+
export default _default;

crates/oxc_isolated_declarations/tests/snapshots/ts-export-assignment.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/ts-export-assignment
66
==================== .D.TS ====================
77
88
declare const Res = 0;
9+
/**
10+
* comment should be a leading comment of the function Foo
11+
*/
912
declare const _default: () => typeof Res;
1013
export = _default;

0 commit comments

Comments
 (0)