Skip to content

Commit 8f35951

Browse files
authored
fix: add space at the end of the doc comment (#63)
Additionally, simplifies the logic to create a doc comment.
1 parent b14db46 commit 8f35951

File tree

5 files changed

+69
-76
lines changed

5 files changed

+69
-76
lines changed

src/core/generate/rs/src/bindings/typescript_native/comments.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::conversion_functions_generator::TopLevelNodes;
22
use swc_core::common::{
33
BytePos, DUMMY_SP, Span,
4-
comments::{Comment, CommentKind},
4+
comments::{Comment, CommentKind, Comments},
55
};
66

77
// Simple monotonic position source for synthetic spans
@@ -24,19 +24,13 @@ fn make_comment(docs: &[String]) -> Option<Comment> {
2424
None
2525
} else {
2626
// Join all doc lines into a single block comment, with each line prefixed by a space
27-
let mut comment_text = String::new();
28-
comment_text.push_str("*\n");
27+
let mut comment_text = String::from("*\n");
2928
for line in docs {
30-
comment_text.push(' ');
31-
comment_text.push_str(&format!("* {}", line));
32-
comment_text.push('\n');
33-
}
34-
// Remove trailing newline
35-
if comment_text.ends_with('\n') {
36-
comment_text.pop();
29+
comment_text.push_str(&format!(" * {}\n", line));
3730
}
3831

39-
comment_text.push('\n');
32+
// Add a space at the end to align the block comment final line ("*/") properly
33+
comment_text.push(' ');
4034

4135
Some(Comment {
4236
span: DUMMY_SP,
@@ -47,7 +41,6 @@ fn make_comment(docs: &[String]) -> Option<Comment> {
4741
}
4842

4943
pub fn add_comments(top_level_nodes: &mut TopLevelNodes, docs: &[String]) -> Span {
50-
use swc_core::common::comments::Comments;
5144
let (_, comments, cursor) = top_level_nodes;
5245
match docs.len() {
5346
0 => DUMMY_SP,

tests/snapshots/generate/example/example.d.ts.snapshot

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ export type my_variant = {
2121
__kind__: "a";
2222
/**
2323
* Doc comment for my_variant field a
24-
*/
24+
*/
2525
a: {
2626
/**
2727
* Doc comment for my_variant field a field b
28-
*/
28+
*/
2929
b: string;
3030
};
3131
} | {
3232
__kind__: "c";
3333
/**
3434
* Doc comment for my_variant field c
35-
*/
35+
*/
3636
c: {
3737
d: string;
3838
e: Array<{
@@ -45,7 +45,7 @@ export interface nested {
4545
_1_: bigint;
4646
/**
4747
* Doc comment for nested record
48-
*/
48+
*/
4949
_2_: [bigint, bigint];
5050
_3_: {
5151
_0_: bigint;
@@ -60,18 +60,18 @@ export type res = {
6060
__kind__: "Ok";
6161
/**
6262
* Doc comment for Ok variant
63-
*/
63+
*/
6464
Ok: [bigint, bigint];
6565
} | {
6666
__kind__: "Err";
6767
/**
6868
* Doc comment for Err variant
69-
*/
69+
*/
7070
Err: {
7171
/**
7272
* Doc comment for error field in Err variant,
7373
* on multiple lines
74-
*/
74+
*/
7575
error: string;
7676
};
7777
};
@@ -89,15 +89,15 @@ export type nested_res = {
8989
__kind__: "Ok";
9090
/**
9191
* Doc comment for Ok in nested variant
92-
*/
92+
*/
9393
Ok: {
9494
content: string;
9595
};
9696
} | {
9797
__kind__: "Err";
9898
/**
9999
* Doc comment for Err in nested variant
100-
*/
100+
*/
101101
Err: [bigint];
102102
};
103103
};
@@ -116,7 +116,7 @@ export type tree = {
116116
export interface nested_records {
117117
/**
118118
* Doc comment for nested_records field nested
119-
*/
119+
*/
120120
nested?: {
121121
nested_field: string;
122122
};
@@ -128,11 +128,11 @@ export interface brokerInterface {
128128
export type list = node | null;
129129
/**
130130
* Doc comment for prim type
131-
*/
131+
*/
132132
export type my_type = Principal;
133133
/**
134134
* Doc comment for List
135-
*/
135+
*/
136136
export type List = {
137137
head: bigint;
138138
tail: List;
@@ -170,11 +170,11 @@ export enum Variant_a_b {
170170
}
171171
/**
172172
* Doc comment for service
173-
*/
173+
*/
174174
export interface exampleInterface {
175175
/**
176176
* Doc comment for f1 method of service
177-
*/
177+
*/
178178
f1(arg0: list, test: Uint8Array | number[], arg2: boolean | null): Promise<void>;
179179
g1(arg0: my_type, arg1: List, arg2: List | null, arg3: nested): Promise<[bigint, Principal, nested_res]>;
180180
h(arg0: Array<string | null>, arg1: {
@@ -190,7 +190,7 @@ export interface exampleInterface {
190190
}>;
191191
/**
192192
* Doc comment for i method of service
193-
*/
193+
*/
194194
i: f;
195195
x(arg0: a, arg1: b): Promise<[a | null, b | null, {
196196
__kind__: "Ok";
@@ -206,7 +206,7 @@ export interface exampleInterface {
206206
g(arg0: list): Promise<[B, tree, stream]>;
207207
/**
208208
* Doc comment for imported bbbbb service method
209-
*/
209+
*/
210210
bbbbb(arg0: b): Promise<void>;
211211
}
212212
export type ProcessErrorFn = (error: unknown) => never;

tests/snapshots/generate/example/example.ts.snapshot

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ export type my_variant = {
5555
__kind__: "a";
5656
/**
5757
* Doc comment for my_variant field a
58-
*/
58+
*/
5959
a: {
6060
/**
6161
* Doc comment for my_variant field a field b
62-
*/
62+
*/
6363
b: string;
6464
};
6565
} | {
6666
__kind__: "c";
6767
/**
6868
* Doc comment for my_variant field c
69-
*/
69+
*/
7070
c: {
7171
d: string;
7272
e: Array<{
@@ -79,7 +79,7 @@ export interface nested {
7979
_1_: bigint;
8080
/**
8181
* Doc comment for nested record
82-
*/
82+
*/
8383
_2_: [bigint, bigint];
8484
_3_: {
8585
_0_: bigint;
@@ -94,18 +94,18 @@ export type res = {
9494
__kind__: "Ok";
9595
/**
9696
* Doc comment for Ok variant
97-
*/
97+
*/
9898
Ok: [bigint, bigint];
9999
} | {
100100
__kind__: "Err";
101101
/**
102102
* Doc comment for Err variant
103-
*/
103+
*/
104104
Err: {
105105
/**
106106
* Doc comment for error field in Err variant,
107107
* on multiple lines
108-
*/
108+
*/
109109
error: string;
110110
};
111111
};
@@ -123,15 +123,15 @@ export type nested_res = {
123123
__kind__: "Ok";
124124
/**
125125
* Doc comment for Ok in nested variant
126-
*/
126+
*/
127127
Ok: {
128128
content: string;
129129
};
130130
} | {
131131
__kind__: "Err";
132132
/**
133133
* Doc comment for Err in nested variant
134-
*/
134+
*/
135135
Err: [bigint];
136136
};
137137
};
@@ -150,7 +150,7 @@ export type tree = {
150150
export interface nested_records {
151151
/**
152152
* Doc comment for nested_records field nested
153-
*/
153+
*/
154154
nested?: {
155155
nested_field: string;
156156
};
@@ -162,11 +162,11 @@ export interface brokerInterface {
162162
export type list = node | null;
163163
/**
164164
* Doc comment for prim type
165-
*/
165+
*/
166166
export type my_type = Principal;
167167
/**
168168
* Doc comment for List
169-
*/
169+
*/
170170
export type List = {
171171
head: bigint;
172172
tail: List;
@@ -205,11 +205,11 @@ export enum Variant_a_b {
205205
export type ProcessErrorFn = (error: unknown) => never;
206206
/**
207207
* Doc comment for service
208-
*/
208+
*/
209209
export interface exampleInterface {
210210
/**
211211
* Doc comment for f1 method of service
212-
*/
212+
*/
213213
f1(arg0: list, test: Uint8Array | number[], arg2: boolean | null): Promise<void>;
214214
g1(arg0: my_type, arg1: List, arg2: List | null, arg3: nested): Promise<[bigint, Principal, nested_res]>;
215215
h(arg0: Array<string | null>, arg1: {
@@ -225,7 +225,7 @@ export interface exampleInterface {
225225
}>;
226226
/**
227227
* Doc comment for i method of service
228-
*/
228+
*/
229229
i: f;
230230
x(arg0: a, arg1: b): Promise<[a | null, b | null, {
231231
__kind__: "Ok";
@@ -241,7 +241,7 @@ export interface exampleInterface {
241241
g(arg0: list): Promise<[B, tree, stream]>;
242242
/**
243243
* Doc comment for imported bbbbb service method
244-
*/
244+
*/
245245
bbbbb(arg0: b): Promise<void>;
246246
}
247247
import type { A as _A, B as _B, List as _List, a as _a, b as _b, list as _list, my_variant as _my_variant, nested as _nested, nested_records as _nested_records, nested_res as _nested_res, node as _node, res as _res, stream as _stream, tree as _tree } from "./declarations/example.did.d.ts";

0 commit comments

Comments
 (0)