Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3094,7 +3094,9 @@ impl Gen for TSType<'_> {

impl Gen for TSArrayType<'_> {
fn r#gen(&self, p: &mut Codegen, ctx: Context) {
self.element_type.print(p, ctx);
p.wrap(parenthesize_check_type_of_postfix_type(&self.element_type), |p| {
self.element_type.print(p, ctx);
});
p.print_str("[]");
}
}
Expand All @@ -3107,10 +3109,30 @@ impl Gen for TSTupleType<'_> {
}
}

fn parenthesize_check_type_of_conditional_type(ty: &TSType<'_>) -> bool {
fn parenthesize_check_type_of_union_type(ty: &TSType<'_>) -> bool {
matches!(
ty,
TSType::TSFunctionType(_) | TSType::TSConstructorType(_) | TSType::TSConditionalType(_)
TSType::TSFunctionType(_)
| TSType::TSConstructorType(_)
| TSType::TSConditionalType(_)
| TSType::TSInferType(_)
)
}

fn parenthesize_check_type_of_intersection_type(ty: &TSType<'_>) -> bool {
matches!(ty, TSType::TSUnionType(_)) || parenthesize_check_type_of_union_type(ty)
}

fn parenthesize_check_type_of_postfix_type(ty: &TSType<'_>) -> bool {
matches!(
ty,
TSType::TSUnionType(_)
| TSType::TSIntersectionType(_)
| TSType::TSFunctionType(_)
| TSType::TSConstructorType(_)
| TSType::TSConditionalType(_)
| TSType::TSInferType(_)
| TSType::TSTypeOperatorType(_)
)
}

Expand All @@ -3119,14 +3141,14 @@ impl Gen for TSUnionType<'_> {
let Some((first, rest)) = self.types.split_first() else {
return;
};
p.wrap(parenthesize_check_type_of_conditional_type(first), |p| {
p.wrap(parenthesize_check_type_of_union_type(first), |p| {
first.print(p, ctx);
});
for item in rest {
p.print_soft_space();
p.print_ascii_byte(b'|');
p.print_soft_space();
p.wrap(parenthesize_check_type_of_conditional_type(item), |p| {
p.wrap(parenthesize_check_type_of_union_type(item), |p| {
item.print(p, ctx);
});
}
Expand All @@ -3146,12 +3168,16 @@ impl Gen for TSIntersectionType<'_> {
let Some((first, rest)) = self.types.split_first() else {
return;
};
first.print(p, ctx);
p.wrap(parenthesize_check_type_of_intersection_type(first), |p| {
first.print(p, ctx);
});
for item in rest {
p.print_soft_space();
p.print_ascii_byte(b'&');
p.print_soft_space();
item.print(p, ctx);
p.wrap(parenthesize_check_type_of_intersection_type(item), |p| {
item.print(p, ctx);
});
}
}
}
Expand All @@ -3177,7 +3203,9 @@ impl Gen for TSInferType<'_> {

impl Gen for TSIndexedAccessType<'_> {
fn r#gen(&self, p: &mut Codegen, ctx: Context) {
self.object_type.print(p, ctx);
p.wrap(parenthesize_check_type_of_postfix_type(&self.object_type), |p| {
self.object_type.print(p, ctx);
});
p.print_ascii_byte(b'[');
self.index_type.print(p, ctx);
p.print_ascii_byte(b']');
Expand Down
12 changes: 12 additions & 0 deletions crates/oxc_codegen/tests/integration/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ fn cases() {
test_same("({ foo(): string {} });\n");
test_same("({ method(this: Foo): void {} });\n");
test_same("({ methodWithParam(this: Foo, bar: string): void {} });\n");
test_same("type T = (A | B)[];\n");
test_same("type T = (A & B)[];\n");
test_same("type T = (keyof A)[];\n");
test_same("type T = (() => A)[];\n");
test_same("type T = (new () => A)[];\n");
test_same("type T = (A extends B ? C : D)[];\n");
test_same("type T = (A | B)[K];\n");
test_same("type T = (A & B)[K];\n");
test_same("type T = (keyof A)[K];\n");
test_same("type T = (A extends B ? C : D)[K];\n");
test_same("type T = A & (B extends C ? D : E);\n");
test_same("type T = (A | B) & C;\n");
test_same("interface I<in out T,> {}\n");
test_same("function F<const in out T,>() {}\n");
test_same("class C {\n\tp = await(0);\n}\n");
Expand Down
Loading