Skip to content

Commit

Permalink
feat(codegen): add new lines to TSTypeParameterDeclaration (#5853)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Sep 18, 2024
1 parent b9c4564 commit 9f6696a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
26 changes: 23 additions & 3 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2790,9 +2790,29 @@ impl<'a> Gen for TSClassImplements<'a> {

impl<'a> Gen for TSTypeParameterDeclaration<'a> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
p.print_str("<");
p.print_list(&self.params, ctx);
p.print_str(">");
let is_multi_line = self.params.len() >= 2;
p.print_char(b'<');
if is_multi_line {
p.indent();
}
for (index, item) in self.params.iter().enumerate() {
if index != 0 {
p.print_comma();
}
if is_multi_line {
p.print_soft_newline();
p.print_indent();
} else if index != 0 {
p.print_soft_space();
}
item.print(p, ctx);
}
if is_multi_line {
p.print_soft_newline();
p.dedent();
p.print_indent();
}
p.print_char(b'>');
}
}

Expand Down
30 changes: 30 additions & 0 deletions crates/oxc_codegen/tests/integration/snapshots/ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,33 @@ export @x declare abstract class C {}
div<T>``
----------
div<T>``;

########## 30
export type Component<Props = any> = Foo;
----------
export type Component<Props = any> = Foo;

########## 31

export type Component<
Props = any,
RawBindings = any,
D = any,
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions,
E extends EmitsOptions | Record<string, any[]> = {},
S extends Record<string, any> = any,
> =
| ConcreteComponent<Props, RawBindings, D, C, M, E, S>
| ComponentPublicInstanceConstructor<Props>

----------
export type Component<
Props = any,
RawBindings = any,
D = any,
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions,
E extends EmitsOptions | Record<string, any[]> = {},
S extends Record<string, any> = any
> = ConcreteComponent<Props, RawBindings, D, C, M, E, S> | ComponentPublicInstanceConstructor<Props>;
14 changes: 14 additions & 0 deletions crates/oxc_codegen/tests/integration/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ fn ts() {
"d = x satisfies y;",
"export @x declare abstract class C {}",
"div<T>``",
"export type Component<Props = any> = Foo;",
"
export type Component<
Props = any,
RawBindings = any,
D = any,
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions,
E extends EmitsOptions | Record<string, any[]> = {},
S extends Record<string, any> = any,
> =
| ConcreteComponent<Props, RawBindings, D, C, M, E, S>
| ComponentPublicInstanceConstructor<Props>
"
];

snapshot("ts", &cases);
Expand Down

0 comments on commit 9f6696a

Please sign in to comment.