Skip to content

Commit e82de9d

Browse files
committed
fix(codegen): wrap parens for TSUnionType
1 parent ce6eeee commit e82de9d

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

crates/oxc_codegen/src/gen.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,17 +3032,28 @@ impl Gen for TSTupleType<'_> {
30323032
}
30333033
}
30343034

3035+
fn parenthesize_check_type_of_conditional_type(ty: &TSType<'_>) -> bool {
3036+
matches!(
3037+
ty,
3038+
TSType::TSFunctionType(_) | TSType::TSConstructorType(_) | TSType::TSConditionalType(_)
3039+
)
3040+
}
3041+
30353042
impl Gen for TSUnionType<'_> {
30363043
fn r#gen(&self, p: &mut Codegen, ctx: Context) {
30373044
let Some((first, rest)) = self.types.split_first() else {
30383045
return;
30393046
};
3040-
first.print(p, ctx);
3047+
p.wrap(parenthesize_check_type_of_conditional_type(first), |p| {
3048+
first.print(p, ctx);
3049+
});
30413050
for item in rest {
30423051
p.print_soft_space();
30433052
p.print_str("|");
30443053
p.print_soft_space();
3045-
item.print(p, ctx);
3054+
p.wrap(parenthesize_check_type_of_conditional_type(item), |p| {
3055+
item.print(p, ctx);
3056+
});
30463057
}
30473058
}
30483059
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class Foo {
2+
constructor(
3+
public one?: () => void,
4+
protected two?: () => void,
5+
private three?: () => void,
6+
) {}
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: crates/oxc_isolated_declarations/tests/mod.rs
3+
input_file: crates/oxc_isolated_declarations/tests/fixtures/union-type.ts
4+
---
5+
```
6+
==================== .D.TS ====================
7+
8+
export declare class Foo {
9+
one?: (() => void) | undefined;
10+
protected two?: (() => void) | undefined;
11+
private three?;
12+
constructor(one?: (() => void) | undefined, two?: (() => void) | undefined, three?: (() => void) | undefined);
13+
}

tasks/coverage/snapshots/transpile.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ export class ClsWithRequiredInitializedParameter {
395395
//// [fnDecl.d.ts] ////
396396
type T = number[];
397397
export declare function fnDeclBasic1(p: number[] | string[] | [T] | undefined, rParam: string): void;
398-
export declare function fnDeclBasic2(p: (n: T) => T | undefined, rParam: string): void;
399-
export declare function fnDeclBasic3(p: new () => any | undefined, rParam: string): void;
398+
export declare function fnDeclBasic2(p: ((n: T) => T) | undefined, rParam: string): void;
399+
export declare function fnDeclBasic3(p: (new () => any) | undefined, rParam: string): void;
400400
export declare function fnDeclBasic4(p: [T] | undefined, rParam: string): void;
401401
export declare function fnDeclBasic5(p: {
402402
a: T;

0 commit comments

Comments
 (0)