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
36 changes: 27 additions & 9 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3121,17 +3121,21 @@ impl Gen for TSTupleType<'_> {
}

fn parenthesize_check_type_of_union_type(ty: &TSType<'_>) -> bool {
matches!(
ty,
match ty {
TSType::TSUnionType(ty) => ty.types.len() > 1,
TSType::TSFunctionType(_)
| TSType::TSConstructorType(_)
| TSType::TSConditionalType(_)
| TSType::TSInferType(_)
)
| TSType::TSConstructorType(_)
| TSType::TSConditionalType(_)
| TSType::TSInferType(_) => true,
_ => false,
}
}

fn parenthesize_check_type_of_intersection_type(ty: &TSType<'_>) -> bool {
matches!(ty, TSType::TSUnionType(_)) || parenthesize_check_type_of_union_type(ty)
match ty {
TSType::TSIntersectionType(ty) => ty.types.len() > 1,
_ => parenthesize_check_type_of_union_type(ty),
}
}

fn parenthesize_check_type_of_postfix_type(ty: &TSType<'_>) -> bool {
Expand Down Expand Up @@ -3283,7 +3287,19 @@ impl Gen for TSTypeOperator<'_> {
fn r#gen(&self, p: &mut Codegen, ctx: Context) {
p.print_str(self.operator.to_str());
p.print_hard_space();
self.type_annotation.print(p, ctx);
p.wrap(
matches!(
&self.type_annotation,
TSType::TSUnionType(_)
| TSType::TSIntersectionType(_)
| TSType::TSFunctionType(_)
| TSType::TSConstructorType(_)
| TSType::TSConditionalType(_)
),
|p| {
self.type_annotation.print(p, ctx);
},
);
}
}

Expand Down Expand Up @@ -3681,7 +3697,9 @@ impl Gen for TSTupleElement<'_> {
match self {
match_ts_type!(TSTupleElement) => self.to_ts_type().print(p, ctx),
TSTupleElement::TSOptionalType(ts_type) => {
ts_type.type_annotation.print(p, ctx);
p.wrap(parenthesize_check_type_of_postfix_type(&ts_type.type_annotation), |p| {
ts_type.type_annotation.print(p, ctx);
});
p.print_ascii_byte(b'?');
}
TSTupleElement::TSRestType(ts_type) => {
Expand Down
29 changes: 28 additions & 1 deletion crates/oxc_codegen/tests/integration/ts.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use oxc_codegen::CodegenOptions;
use oxc_parser::ParseOptions;

use crate::{
snapshot, snapshot_options,
tester::{test_idempotency, test_same, test_tsx},
tester::{test_idempotency, test_same, test_tsx, test_with_parse_options},
};

#[test]
Expand Down Expand Up @@ -191,3 +192,29 @@ fn ts_satisfies_expression() {
test_idempotency("x satisfies Y && z");
test_idempotency("x satisfies Y === z");
}

#[test]
fn type_codegen_with_preserve_parens_off() {
let parse_options = ParseOptions { preserve_parens: false, ..Default::default() };

test_with_parse_options(
"type T = keyof (EventMap & Extra);",
"type T = keyof (EventMap & Extra);\n",
parse_options,
);
test_with_parse_options(
"type T = [(Anno | undefined)?];",
"type T = [(Anno | undefined)?];\n",
parse_options,
);
test_with_parse_options(
"type T = (Out & (Step extends A ? B : C)) & (Step extends D ? E : F);",
"type T = (Out & (Step extends A ? B : C)) & (Step extends D ? E : F);\n",
parse_options,
);
test_with_parse_options(
"type T = ({ [K in keyof Obj]: Obj[K] } & { a: 1 }) & { b: 2 };",
"type T = ({ [K in keyof Obj] : Obj[K] } & {\n\ta: 1;\n}) & {\n\tb: 2;\n};\n",
parse_options,
);
}
Loading