Skip to content

Commit

Permalink
fix(es/isolated-dts): Preserve comments (#9572)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #9550
  • Loading branch information
kdy1 authored Sep 20, 2024
1 parent 25d9e04 commit 6d15d9c
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/itchy-socks-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc: patch
---

fix(es/isolated-dts): Preserve comments
14 changes: 11 additions & 3 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub extern crate swc_atoms as atoms;
extern crate swc_common as common;

use std::{
cell::RefCell,
fs::{read_to_string, File},
io::ErrorKind,
path::{Path, PathBuf},
Expand All @@ -136,7 +137,7 @@ use swc_common::{
pub use swc_compiler_base::{PrintArgs, TransformOutput};
pub use swc_config::config_types::{BoolConfig, BoolOr, BoolOrDataConfig};
use swc_ecma_ast::{EsVersion, Program};
use swc_ecma_codegen::{to_code, Node};
use swc_ecma_codegen::{to_code_with_comments, Node};
use swc_ecma_loader::resolvers::{
lru::CachingResolver, node::NodeModulesResolver, tsc::TsConfigResolver,
};
Expand Down Expand Up @@ -724,7 +725,7 @@ impl Compiler {
None
};

self.apply_transforms(handler, fm.clone(), orig.as_ref(), config)
self.apply_transforms(handler, comments.clone(), fm.clone(), orig.as_ref(), config)
})
}

Expand Down Expand Up @@ -944,6 +945,7 @@ impl Compiler {
fn apply_transforms(
&self,
handler: &Handler,
comments: SingleThreadedComments,
fm: Arc<SourceFile>,
orig: Option<&sourcemap::SourceMap>,
config: BuiltInput<impl swc_ecma_visit::Fold>,
Expand Down Expand Up @@ -971,6 +973,12 @@ impl Compiler {
};

let dts_code = if emit_dts && program.is_module() {
let (leading, trailing) = comments.borrow_all();

let leading = std::rc::Rc::new(RefCell::new(leading.clone()));
let trailing = std::rc::Rc::new(RefCell::new(trailing.clone()));

let comments = SingleThreadedComments::from_leading_and_trailing(leading, trailing);
let mut checker = FastDts::new(fm.name.clone());
let mut module = program.clone().expect_module();

Expand All @@ -983,7 +991,7 @@ impl Compiler {
.struct_span_err(range.span, &issue.to_string())
.emit();
}
let dts_code = to_code(&module);
let dts_code = to_code_with_comments(Some(&comments), &module);
Some(dts_code)
} else {
None
Expand Down
11 changes: 11 additions & 0 deletions crates/swc/tests/ts-isolated-declaration/issues/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"experimental": {
"emitIsolatedDts": true
}
},
"isModule": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Sample JSDoc that I wish was in the swc.transform output
* @param a - string param
* @param b - number param
* @returns - object with a and b
*/
function sampleFunc(a: string, b: number) {
return { a, b };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Sample JSDoc that I wish was in the swc.transform output
* @param a - string param
* @param b - number param
* @returns - object with a and b
*/ declare function sampleFunc(a: string, b: number);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Sample JSDoc that I wish was in the swc.transform output
* @param a - string param
* @param b - number param
* @returns - object with a and b
*/ function sampleFunc(a, b) {
return {
a: a,
b: b
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Correct
declare async function asyncFunctionGood(): Promise<number>;
declare const asyncFunctionGoo2: () => Promise<number>;
// Need to explicit return type for async functions
// Incorrect
declare async function asyncFunction();
declare const asyncFunction2: () => any;
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Correct
export declare function fnDeclGood(p?: T, rParam?: string): void;
export declare function fnDeclGood2(p?: T, rParam?: number): void;
export declare function fooGood([a, b]?: any[]): number;
export declare const fooGood2: (_dts_1: {
a: number;
b: number;
}) => number;
// Incorrect
export declare function fnDeclBad<T>(p?: T, rParam?: T, r2: T): void;
export declare function fnDeclBad2<T>(p?: T, r2: T): void;
export declare function fnDeclBad3<T>(p?: T, rParam?: T, r2: T): void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// Correct
declare function* generatorGood(): Generator<number>;
// Need to explicit return type for async functions
// Incorrect
declare function* generatorGoodBad();
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ParenthesizedExpression
declare const n: any;
declare const s: any;
declare const t: any;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
declare function foo();
// inferred type is number
declare function bar();
// inferred type is number | undefined
declare function baz();
// We can't infer return type if there are multiple return statements with different types
declare function qux();
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Correct
declare const [A, B];
export declare function foo(): number;
// Incorrect
declare const { c, d };
declare const [e];
export { c, d, e };

0 comments on commit 6d15d9c

Please sign in to comment.