Skip to content

Commit

Permalink
feat(codegen): print comments in TSTypeLiteral (#8679)
Browse files Browse the repository at this point in the history
fixes #8665
  • Loading branch information
Boshen committed Jan 23, 2025
1 parent 23b49a6 commit 99607d3
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 122 deletions.
3 changes: 3 additions & 0 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ impl Codegen<'_> {
}

pub(crate) fn print_leading_comments(&mut self, start: u32) {
if !self.print_comments {
return;
}
let Some(comments) = self.comments.remove(&start) else {
return;
};
Expand Down
92 changes: 42 additions & 50 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2350,9 +2350,7 @@ impl Gen for ClassBody<'_> {
p.print_curly_braces(self.span, self.body.is_empty(), |p| {
for item in &self.body {
p.print_semicolon_if_needed();
if p.print_comments {
p.print_leading_comments(item.span().start);
}
p.print_leading_comments(item.span().start);
p.print_indent();
item.print(p, ctx);
}
Expand Down Expand Up @@ -3296,21 +3294,15 @@ impl Gen for TSTemplateLiteralType<'_> {

impl Gen for TSTypeLiteral<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
let single_line = self.members.len() <= 1;
p.print_curly_braces(self.span, single_line, |p| {
p.print_curly_braces(self.span, self.members.is_empty(), |p| {
for item in &self.members {
if single_line {
p.print_soft_space();
} else {
p.print_indent();
}
p.print_leading_comments(item.span().start);
p.print_indent();
item.print(p, ctx);
if single_line {
p.print_soft_space();
} else {
if p.options.minify {
p.print_semicolon();
p.print_soft_newline();
}
p.print_soft_newline();
}
});
}
Expand Down Expand Up @@ -3399,36 +3391,7 @@ impl Gen for TSSignature<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
match self {
Self::TSIndexSignature(signature) => signature.print(p, ctx),
Self::TSPropertySignature(signature) => {
if signature.readonly {
p.print_str("readonly ");
}
if signature.computed {
p.print_ascii_byte(b'[');
signature.key.print(p, ctx);
p.print_ascii_byte(b']');
} else {
match &signature.key {
PropertyKey::StaticIdentifier(key) => {
key.print(p, ctx);
}
PropertyKey::PrivateIdentifier(key) => {
p.print_str(key.name.as_str());
}
key => {
key.to_expression().print_expr(p, Precedence::Comma, ctx);
}
}
}
if signature.optional {
p.print_str("?");
}
if let Some(type_annotation) = &signature.type_annotation {
p.print_colon();
p.print_soft_space();
type_annotation.print(p, ctx);
}
}
Self::TSPropertySignature(signature) => signature.r#gen(p, ctx),
Self::TSCallSignatureDeclaration(signature) => {
if let Some(type_parameters) = signature.type_parameters.as_ref() {
type_parameters.print(p, ctx);
Expand Down Expand Up @@ -3512,6 +3475,39 @@ impl Gen for TSSignature<'_> {
}
}

impl Gen for TSPropertySignature<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
if self.readonly {
p.print_str("readonly ");
}
if self.computed {
p.print_ascii_byte(b'[');
self.key.print(p, ctx);
p.print_ascii_byte(b']');
} else {
match &self.key {
PropertyKey::StaticIdentifier(key) => {
key.print(p, ctx);
}
PropertyKey::PrivateIdentifier(key) => {
p.print_str(key.name.as_str());
}
key => {
key.to_expression().print_expr(p, Precedence::Comma, ctx);
}
}
}
if self.optional {
p.print_str("?");
}
if let Some(type_annotation) = &self.type_annotation {
p.print_colon();
p.print_soft_space();
type_annotation.print(p, ctx);
}
}
}

impl Gen for TSTypeQuery<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
p.print_str("typeof ");
Expand Down Expand Up @@ -3742,9 +3738,7 @@ impl Gen for TSInterfaceDeclaration<'_> {
p.print_soft_space();
p.print_curly_braces(self.body.span, self.body.body.is_empty(), |p| {
for item in &self.body.body {
if p.print_comments {
p.print_leading_comments(item.span().start);
}
p.print_leading_comments(item.span().start);
p.print_indent();
item.print(p, ctx);
p.print_semicolon();
Expand Down Expand Up @@ -3778,9 +3772,7 @@ impl Gen for TSEnumDeclaration<'_> {
p.print_space_before_identifier();
p.print_curly_braces(self.span, self.members.is_empty(), |p| {
for member in &self.members {
if p.print_comments {
p.print_leading_comments(member.span().start);
}
p.print_leading_comments(member.span().start);
p.print_indent();
member.print(p, ctx);
p.print_comma();
Expand Down
5 changes: 2 additions & 3 deletions crates/oxc_codegen/tests/integration/snapshots/minify.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/oxc_codegen/tests/integration/main.rs
snapshot_kind: text
---
########## 0
let x: string = `\x01`;
Expand Down Expand Up @@ -79,11 +78,11 @@ class A{readonly type="frame"}
########## 17
let foo: { <T>(t: T): void }
----------
let foo:{<T>(t:T):void};
let foo:{<T>(t:T):void;};
########## 18
let foo: { new <T>(t: T): void }
----------
let foo:{new <T>(t:T):void};
let foo:{new <T>(t:T):void;};
########## 19
function <const T>(){}
----------
Expand Down
9 changes: 6 additions & 3 deletions crates/oxc_codegen/tests/integration/snapshots/ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/oxc_codegen/tests/integration/main.rs
snapshot_kind: text
---
########## 0
let x: string = `\x01`;
Expand Down Expand Up @@ -112,12 +111,16 @@ class A {
########## 17
let foo: { <T>(t: T): void }
----------
let foo: { <T>(t: T): void };
let foo: {
<T>(t: T): void
};

########## 18
let foo: { new <T>(t: T): void }
----------
let foo: { new <T>(t: T): void };
let foo: {
new <T>(t: T): void
};

########## 19
function <const T>(){}
Expand Down
11 changes: 10 additions & 1 deletion crates/oxc_isolated_declarations/tests/fixtures/signatures.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
export interface X {
/**
* Comment
*/
set value(_: string);
}

export type A = {
/**
* Comment
*/
set value({ a, b, c }: { a: string; b: string; c: string });
/**
* Comment
*/
get value();
};

Expand All @@ -24,4 +33,4 @@ export interface MultipleSetterAndGetter {
set ok(_: string)
get bad() // infer return type
set bad(_: string)
}
}
29 changes: 15 additions & 14 deletions crates/oxc_isolated_declarations/tests/snapshots/as-const.snap
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
---
source: crates/oxc_isolated_declarations/tests/mod.rs
input_file: crates/oxc_isolated_declarations/tests/fixtures/as-const.ts
snapshot_kind: text
---
```
==================== .D.TS ====================
declare const F: {
readonly string: "string";
readonly templateLiteral: "templateLiteral";
readonly number: 1.23;
readonly bigint: -123n;
readonly boolean: true;
readonly null: null;
readonly undefined: undefined;
readonly function: (a: string) => void;
readonly arrow: (a: string) => void;
readonly string: "string"
readonly templateLiteral: "templateLiteral"
readonly number: 1.23
readonly bigint: -123n
readonly boolean: true
readonly null: null
readonly undefined: undefined
readonly function: (a: string) => void
readonly arrow: (a: string) => void
readonly object: {
readonly a: "a";
readonly b: "b";
};
readonly array: readonly ["a", undefined, { readonly b: "\n" }];
readonly a: "a"
readonly b: "b"
}
readonly array: readonly ["a", undefined, {
readonly b: "\n"
}]
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/empty-export.ts
type A = string;
export declare function a(): A;
export declare const ShallowReactiveMarker: unique symbol;
export type ShallowReactive<T> = T & { [ShallowReactiveMarker]?: true };
export type ShallowReactive<T> = T & {
[ShallowReactiveMarker]?: true
};
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/infer-template-liter
export declare const CSS_VARS_HELPER = "useCssVars";
export declare function g(func?: string): void;
export declare const F: {
readonly a: "a";
readonly b: readonly ["b"];
readonly a: "a"
readonly b: readonly ["b"]
};
export declare let GOOD: string;
export declare const BAD: unknown;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/readonly.ts
```
==================== .D.TS ====================
export declare const EMPTY_OBJ: { readonly [key: string]: any };
export declare const EMPTY_OBJ: {
readonly [key: string]: any
};
export declare const EMPTY_ARR: readonly never[];
25 changes: 17 additions & 8 deletions crates/oxc_isolated_declarations/tests/snapshots/signatures.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/signatures.ts
==================== .D.TS ====================
export interface X {
/**
* Comment
*/
set value(_: string);
}
export type A = {
/**
* Comment
*/
set value({ a, b, c }: {
a: string;
b: string;
c: string;
});
a: string
b: string
c: string
})
/**
* Comment
*/
get value(): {
a: string;
b: string;
c: string;
};
a: string
b: string
c: string
}
};
export interface I {
set value(_: string);
Expand Down
Loading

0 comments on commit 99607d3

Please sign in to comment.