Skip to content

Commit 6d15d9c

Browse files
authoredSep 20, 2024··
fix(es/isolated-dts): Preserve comments (#9572)
**Related issue:** - Closes #9550
1 parent 25d9e04 commit 6d15d9c

12 files changed

+68
-3
lines changed
 

‎.changeset/itchy-socks-marry.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc: patch
4+
---
5+
6+
fix(es/isolated-dts): Preserve comments

‎crates/swc/src/lib.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ pub extern crate swc_atoms as atoms;
113113
extern crate swc_common as common;
114114

115115
use std::{
116+
cell::RefCell,
116117
fs::{read_to_string, File},
117118
io::ErrorKind,
118119
path::{Path, PathBuf},
@@ -136,7 +137,7 @@ use swc_common::{
136137
pub use swc_compiler_base::{PrintArgs, TransformOutput};
137138
pub use swc_config::config_types::{BoolConfig, BoolOr, BoolOrDataConfig};
138139
use swc_ecma_ast::{EsVersion, Program};
139-
use swc_ecma_codegen::{to_code, Node};
140+
use swc_ecma_codegen::{to_code_with_comments, Node};
140141
use swc_ecma_loader::resolvers::{
141142
lru::CachingResolver, node::NodeModulesResolver, tsc::TsConfigResolver,
142143
};
@@ -724,7 +725,7 @@ impl Compiler {
724725
None
725726
};
726727

727-
self.apply_transforms(handler, fm.clone(), orig.as_ref(), config)
728+
self.apply_transforms(handler, comments.clone(), fm.clone(), orig.as_ref(), config)
728729
})
729730
}
730731

@@ -944,6 +945,7 @@ impl Compiler {
944945
fn apply_transforms(
945946
&self,
946947
handler: &Handler,
948+
comments: SingleThreadedComments,
947949
fm: Arc<SourceFile>,
948950
orig: Option<&sourcemap::SourceMap>,
949951
config: BuiltInput<impl swc_ecma_visit::Fold>,
@@ -971,6 +973,12 @@ impl Compiler {
971973
};
972974

973975
let dts_code = if emit_dts && program.is_module() {
976+
let (leading, trailing) = comments.borrow_all();
977+
978+
let leading = std::rc::Rc::new(RefCell::new(leading.clone()));
979+
let trailing = std::rc::Rc::new(RefCell::new(trailing.clone()));
980+
981+
let comments = SingleThreadedComments::from_leading_and_trailing(leading, trailing);
974982
let mut checker = FastDts::new(fm.name.clone());
975983
let mut module = program.clone().expect_module();
976984

@@ -983,7 +991,7 @@ impl Compiler {
983991
.struct_span_err(range.span, &issue.to_string())
984992
.emit();
985993
}
986-
let dts_code = to_code(&module);
994+
let dts_code = to_code_with_comments(Some(&comments), &module);
987995
Some(dts_code)
988996
} else {
989997
None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "typescript"
5+
},
6+
"experimental": {
7+
"emitIsolatedDts": true
8+
}
9+
},
10+
"isModule": true
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Sample JSDoc that I wish was in the swc.transform output
3+
* @param a - string param
4+
* @param b - number param
5+
* @returns - object with a and b
6+
*/
7+
function sampleFunc(a: string, b: number) {
8+
return { a, b };
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Sample JSDoc that I wish was in the swc.transform output
3+
* @param a - string param
4+
* @param b - number param
5+
* @returns - object with a and b
6+
*/ declare function sampleFunc(a: string, b: number);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Sample JSDoc that I wish was in the swc.transform output
3+
* @param a - string param
4+
* @param b - number param
5+
* @returns - object with a and b
6+
*/ function sampleFunc(a, b) {
7+
return {
8+
a: a,
9+
b: b
10+
};
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
// Correct
12
declare async function asyncFunctionGood(): Promise<number>;
23
declare const asyncFunctionGoo2: () => Promise<number>;
4+
// Need to explicit return type for async functions
5+
// Incorrect
36
declare async function asyncFunction();
47
declare const asyncFunction2: () => any;

‎crates/swc/tests/ts-isolated-declaration/oxc/output/function-parameters.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
// Correct
12
export declare function fnDeclGood(p?: T, rParam?: string): void;
23
export declare function fnDeclGood2(p?: T, rParam?: number): void;
34
export declare function fooGood([a, b]?: any[]): number;
45
export declare const fooGood2: (_dts_1: {
56
a: number;
67
b: number;
78
}) => number;
9+
// Incorrect
810
export declare function fnDeclBad<T>(p?: T, rParam?: T, r2: T): void;
911
export declare function fnDeclBad2<T>(p?: T, r2: T): void;
1012
export declare function fnDeclBad3<T>(p?: T, rParam?: T, r2: T): void;
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
// Correct
12
declare function* generatorGood(): Generator<number>;
3+
// Need to explicit return type for async functions
4+
// Incorrect
25
declare function* generatorGoodBad();

‎crates/swc/tests/ts-isolated-declaration/oxc/output/infer-expression.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// ParenthesizedExpression
12
declare const n: any;
23
declare const s: any;
34
declare const t: any;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
declare function foo();
2+
// inferred type is number
23
declare function bar();
4+
// inferred type is number | undefined
35
declare function baz();
6+
// We can't infer return type if there are multiple return statements with different types
47
declare function qux();
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
// Correct
12
declare const [A, B];
23
export declare function foo(): number;
4+
// Incorrect
35
declare const { c, d };
46
declare const [e];
57
export { c, d, e };

0 commit comments

Comments
 (0)
Please sign in to comment.