Skip to content

Commit 1976d8e

Browse files
authored
fix(ts/isolated-dts): Skip parameters without accessibility modifiers in private constructors (#10675)
**Related issue:** - Closes #10674
1 parent cac651b commit 1976d8e

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

.changeset/tidy-meals-share.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_typescript: patch
4+
---
5+
6+
fix(ts/isolated-dts): Skip parameters without accessibility modifiers in private constructors

crates/swc_typescript/src/fast_dts/class.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ impl FastDts {
3434
continue;
3535
}
3636

37+
let private_constructor =
38+
constructor.accessibility == Some(Accessibility::Private);
39+
3740
// Transform parameters
3841
class.body.splice(
3942
0..0,
40-
self.transform_constructor_params(&mut constructor.params),
43+
self.transform_constructor_params(
44+
&mut constructor.params,
45+
private_constructor,
46+
),
4147
);
4248

4349
if !(constructor.is_optional) && constructor.body.is_none() {
@@ -47,10 +53,7 @@ impl FastDts {
4753
continue;
4854
}
4955

50-
if constructor
51-
.accessibility
52-
.is_some_and(|accessibility| accessibility == Accessibility::Private)
53-
{
56+
if private_constructor {
5457
constructor.params.clear();
5558
}
5659

@@ -273,6 +276,7 @@ impl FastDts {
273276
pub(crate) fn transform_constructor_params(
274277
&mut self,
275278
params: &mut [ParamOrTsParamProp],
279+
private_constructor: bool,
276280
) -> Vec<ClassMember> {
277281
let mut is_required = false;
278282
let mut private_properties = Vec::new();
@@ -292,6 +296,10 @@ impl FastDts {
292296
ts_param_prop.accessibility = None;
293297
}
294298
ParamOrTsParamProp::Param(param) => {
299+
if private_constructor {
300+
continue;
301+
}
302+
295303
self.transform_fn_param(param, is_required);
296304
is_required |= match &param.pat {
297305
Pat::Ident(binding_ident) => !binding_ident.optional,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
```==================== .D.TS ====================
2+
3+
export declare class Foo {
4+
private constructor();
5+
}
6+
export declare class Bar {
7+
readonly x: number;
8+
private constructor();
9+
}
10+
11+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class Foo {
2+
private constructor(foo = new Set<string>()) {}
3+
}
4+
5+
export class Bar {
6+
private constructor(readonly x: number, foo = new Set<string>()) {}
7+
}

0 commit comments

Comments
 (0)