Skip to content

Commit 98c5e0b

Browse files
Fix multiline doc comment alignment and remove empty ones entirely (#4135)
1 parent c1acd31 commit 98c5e0b

31 files changed

+156
-164
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
* Fixed enum variant name collisions with object prototype fields.
6565
[#4137](https://github.com/rustwasm/wasm-bindgen/pull/4137)
6666

67+
* Fixed multiline doc comment alignment and remove empty ones entirely.
68+
[#4135](https://github.com/rustwasm/wasm-bindgen/pull/4135)
69+
6770
--------------------------------------------------------------------------------
6871

6972
## [0.2.93](https://github.com/rustwasm/wasm-bindgen/compare/0.2.92...0.2.93)

crates/cli-support/src/js/mod.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4126,18 +4126,23 @@ fn check_duplicated_getter_and_setter_names(
41264126

41274127
fn format_doc_comments(comments: &str, js_doc_comments: Option<String>) -> String {
41284128
let body: String = comments.lines().fold(String::new(), |mut output, c| {
4129-
let _ = writeln!(output, "*{}", c);
4129+
let _ = writeln!(output, " *{}", c);
41304130
output
41314131
});
41324132
let doc = if let Some(docs) = js_doc_comments {
41334133
docs.lines().fold(String::new(), |mut output: String, l| {
4134-
let _ = writeln!(output, "* {}", l);
4134+
let _ = writeln!(output, " * {}", l);
41354135
output
41364136
})
41374137
} else {
41384138
String::new()
41394139
};
4140-
format!("/**\n{}{}*/\n", body, doc)
4140+
if body.is_empty() && doc.is_empty() {
4141+
// don't emit empty doc comments
4142+
String::new()
4143+
} else {
4144+
format!("/**\n{}{} */\n", body, doc)
4145+
}
41414146
}
41424147

41434148
fn require_class<'a>(
@@ -4222,7 +4227,13 @@ impl ExportedClass {
42224227
self.contents.push_str(js);
42234228
self.contents.push('\n');
42244229
if let Some(ts) = ts {
4225-
self.typescript.push_str(docs);
4230+
if !docs.is_empty() {
4231+
for line in docs.lines() {
4232+
self.typescript.push_str(" ");
4233+
self.typescript.push_str(line);
4234+
self.typescript.push('\n');
4235+
}
4236+
}
42264237
self.typescript.push_str(" ");
42274238
self.typescript.push_str(function_prefix);
42284239
self.typescript.push_str(function_name);

crates/cli-support/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,13 @@ fn reset_indentation(s: &str) -> String {
492492
let mut indent: u32 = 0;
493493
let mut dst = String::new();
494494

495+
fn is_doc_comment(line: &str) -> bool {
496+
line.starts_with("*")
497+
}
498+
495499
for line in s.lines() {
496500
let line = line.trim();
497-
if line.starts_with('}') || (line.ends_with('}') && !line.starts_with('*')) {
501+
if line.starts_with('}') || (line.ends_with('}') && !is_doc_comment(line)) {
498502
indent = indent.saturating_sub(1);
499503
}
500504
let extra = if line.starts_with(':') || line.starts_with('?') {
@@ -506,11 +510,14 @@ fn reset_indentation(s: &str) -> String {
506510
for _ in 0..indent + extra {
507511
dst.push_str(" ");
508512
}
513+
if is_doc_comment(line) {
514+
dst.push(' ');
515+
}
509516
dst.push_str(line);
510517
}
511518
dst.push('\n');
512519
// Ignore { inside of comments and if it's an exported enum
513-
if line.ends_with('{') && !line.starts_with('*') && !line.ends_with("Object.freeze({") {
520+
if line.ends_with('{') && !is_doc_comment(line) && !line.ends_with("Object.freeze({") {
514521
indent += 1;
515522
}
516523
}

crates/cli/tests/reference/add.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/* tslint:disable */
22
/* eslint-disable */
33
/**
4-
* @param {number} a
5-
* @param {number} b
6-
* @returns {number}
7-
*/
4+
* @param {number} a
5+
* @param {number} b
6+
* @returns {number}
7+
*/
88
export function add_u32(a: number, b: number): number;
99
/**
10-
* @param {number} a
11-
* @param {number} b
12-
* @returns {number}
13-
*/
10+
* @param {number} a
11+
* @param {number} b
12+
* @returns {number}
13+
*/
1414
export function add_i32(a: number, b: number): number;

crates/cli/tests/reference/add.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ export function __wbg_set_wasm(val) {
44
}
55

66
/**
7-
* @param {number} a
8-
* @param {number} b
9-
* @returns {number}
10-
*/
7+
* @param {number} a
8+
* @param {number} b
9+
* @returns {number}
10+
*/
1111
export function add_u32(a, b) {
1212
const ret = wasm.add_u32(a, b);
1313
return ret >>> 0;
1414
}
1515

1616
/**
17-
* @param {number} a
18-
* @param {number} b
19-
* @returns {number}
20-
*/
17+
* @param {number} a
18+
* @param {number} b
19+
* @returns {number}
20+
*/
2121
export function add_i32(a, b) {
2222
const ret = wasm.add_i32(a, b);
2323
return ret;
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
/* tslint:disable */
22
/* eslint-disable */
3-
/**
4-
*/
53
export function exported(): void;

crates/cli/tests/reference/anyref-import-catch.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ function takeFromExternrefTable0(idx) {
5353
wasm.__externref_table_dealloc(idx);
5454
return value;
5555
}
56-
/**
57-
*/
56+
5857
export function exported() {
5958
try {
6059
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
/* tslint:disable */
22
/* eslint-disable */
3-
/**
4-
*/
53
export function foo(): void;

crates/cli/tests/reference/anyref-nop.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ export function __wbg_set_wasm(val) {
33
wasm = val;
44
}
55

6-
/**
7-
*/
6+
87
export function foo() {
98
wasm.foo();
109
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* tslint:disable */
22
/* eslint-disable */
33
/**
4-
* @returns {Promise<number>}
5-
*/
4+
* @returns {Promise<number>}
5+
*/
66
export function foo(): Promise<number>;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* tslint:disable */
22
/* eslint-disable */
33
/**
4-
* @returns {Promise<void>}
5-
*/
4+
* @returns {Promise<void>}
5+
*/
66
export function foo(): Promise<void>;
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/* tslint:disable */
22
/* eslint-disable */
3-
/**
4-
*/
53
export class ClassBuilder {
64
free(): void;
7-
/**
8-
* @returns {ClassBuilder}
9-
*/
5+
/**
6+
* @returns {ClassBuilder}
7+
*/
108
static builder(): ClassBuilder;
119
}

crates/cli/tests/reference/builder.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ function getStringFromWasm0(ptr, len) {
2727
const ClassBuilderFinalization = (typeof FinalizationRegistry === 'undefined')
2828
? { register: () => {}, unregister: () => {} }
2929
: new FinalizationRegistry(ptr => wasm.__wbg_classbuilder_free(ptr >>> 0, 1));
30-
/**
31-
*/
30+
3231
export class ClassBuilder {
3332

3433
static __wrap(ptr) {
@@ -51,8 +50,8 @@ export class ClassBuilder {
5150
wasm.__wbg_classbuilder_free(ptr, 0);
5251
}
5352
/**
54-
* @returns {ClassBuilder}
55-
*/
53+
* @returns {ClassBuilder}
54+
*/
5655
static builder() {
5756
const ret = wasm.classbuilder_builder();
5857
return ClassBuilder.__wrap(ret);
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
/* tslint:disable */
22
/* eslint-disable */
3-
/**
4-
*/
53
export class ClassConstructor {
64
free(): void;
7-
/**
8-
*/
95
constructor();
106
}

crates/cli/tests/reference/constructor.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ function getStringFromWasm0(ptr, len) {
2727
const ClassConstructorFinalization = (typeof FinalizationRegistry === 'undefined')
2828
? { register: () => {}, unregister: () => {} }
2929
: new FinalizationRegistry(ptr => wasm.__wbg_classconstructor_free(ptr >>> 0, 1));
30-
/**
31-
*/
30+
3231
export class ClassConstructor {
3332

3433
__destroy_into_raw() {
@@ -42,8 +41,6 @@ export class ClassConstructor {
4241
const ptr = this.__destroy_into_raw();
4342
wasm.__wbg_classconstructor_free(ptr, 0);
4443
}
45-
/**
46-
*/
4744
constructor() {
4845
const ret = wasm.classconstructor_new();
4946
this.__wbg_ptr = ret >>> 0;

crates/cli/tests/reference/enums.d.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
/* tslint:disable */
22
/* eslint-disable */
33
/**
4-
* @param {Color} color
5-
* @returns {Color}
6-
*/
4+
* @param {Color} color
5+
* @returns {Color}
6+
*/
77
export function enum_echo(color: Color): Color;
88
/**
9-
* @param {Color | undefined} [color]
10-
* @returns {Color | undefined}
11-
*/
9+
* @param {Color | undefined} [color]
10+
* @returns {Color | undefined}
11+
*/
1212
export function option_enum_echo(color?: Color): Color | undefined;
1313
/**
14-
* @param {Color} color
15-
* @returns {ColorName}
16-
*/
14+
* @param {Color} color
15+
* @returns {ColorName}
16+
*/
1717
export function get_name(color: Color): ColorName;
1818
/**
19-
* @param {ColorName | undefined} [color]
20-
* @returns {ColorName | undefined}
21-
*/
19+
* @param {ColorName | undefined} [color]
20+
* @returns {ColorName | undefined}
21+
*/
2222
export function option_string_enum_echo(color?: ColorName): ColorName | undefined;
23-
/**
24-
*/
2523
export enum Color {
2624
Green = 0,
2725
Yellow = 1,

crates/cli/tests/reference/enums.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ function getStringFromWasm0(ptr, len) {
2424
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
2525
}
2626
/**
27-
* @param {Color} color
28-
* @returns {Color}
29-
*/
27+
* @param {Color} color
28+
* @returns {Color}
29+
*/
3030
export function enum_echo(color) {
3131
const ret = wasm.enum_echo(color);
3232
return ret;
@@ -36,34 +36,32 @@ function isLikeNone(x) {
3636
return x === undefined || x === null;
3737
}
3838
/**
39-
* @param {Color | undefined} [color]
40-
* @returns {Color | undefined}
41-
*/
39+
* @param {Color | undefined} [color]
40+
* @returns {Color | undefined}
41+
*/
4242
export function option_enum_echo(color) {
4343
const ret = wasm.option_enum_echo(isLikeNone(color) ? 3 : color);
4444
return ret === 3 ? undefined : ret;
4545
}
4646

4747
/**
48-
* @param {Color} color
49-
* @returns {ColorName}
50-
*/
48+
* @param {Color} color
49+
* @returns {ColorName}
50+
*/
5151
export function get_name(color) {
5252
const ret = wasm.get_name(color);
5353
return ["green","yellow","red",][ret];
5454
}
5555

5656
/**
57-
* @param {ColorName | undefined} [color]
58-
* @returns {ColorName | undefined}
59-
*/
57+
* @param {ColorName | undefined} [color]
58+
* @returns {ColorName | undefined}
59+
*/
6060
export function option_string_enum_echo(color) {
6161
const ret = wasm.option_string_enum_echo(color == undefined ? 4 : ((["green","yellow","red",].indexOf(color) + 1 || 4) - 1));
6262
return ["green","yellow","red",][ret];
6363
}
6464

65-
/**
66-
*/
6765
export const Color = Object.freeze({ Green:0,"0":"Green",Yellow:1,"1":"Yellow",Red:2,"2":"Red", });
6866

6967
export function __wbindgen_throw(arg0, arg1) {
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
/* tslint:disable */
22
/* eslint-disable */
3-
/**
4-
*/
53
export function exported(): void;

crates/cli/tests/reference/import-catch.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ function takeObject(idx) {
4949
dropObject(idx);
5050
return ret;
5151
}
52-
/**
53-
*/
52+
5453
export function exported() {
5554
try {
5655
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);

crates/cli/tests/reference/nop.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
/* tslint:disable */
22
/* eslint-disable */
3-
/**
4-
*/
53
export function nop(): void;

crates/cli/tests/reference/nop.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ export function __wbg_set_wasm(val) {
33
wasm = val;
44
}
55

6-
/**
7-
*/
6+
87
export function nop() {
98
wasm.nop();
109
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* tslint:disable */
22
/* eslint-disable */
33
/**
4-
* @param {number} input
5-
* @returns {number}
6-
*/
4+
* @param {number} input
5+
* @returns {number}
6+
*/
77
export function const_pointer(input: number): number;
88
/**
9-
* @param {number} input
10-
* @returns {number}
11-
*/
9+
* @param {number} input
10+
* @returns {number}
11+
*/
1212
export function mut_pointer(input: number): number;

0 commit comments

Comments
 (0)