Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved doc comment formatting #4135

Merged
merged 3 commits into from
Oct 7, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
* Fixed enum variant name collisions with object prototype fields.
[#4137](https://github.com/rustwasm/wasm-bindgen/pull/4137)

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

--------------------------------------------------------------------------------

## [0.2.93](https://github.com/rustwasm/wasm-bindgen/compare/0.2.92...0.2.93)
Expand Down
19 changes: 15 additions & 4 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4126,18 +4126,23 @@ fn check_duplicated_getter_and_setter_names(

fn format_doc_comments(comments: &str, js_doc_comments: Option<String>) -> String {
let body: String = comments.lines().fold(String::new(), |mut output, c| {
let _ = writeln!(output, "*{}", c);
let _ = writeln!(output, " *{}", c);
output
});
let doc = if let Some(docs) = js_doc_comments {
docs.lines().fold(String::new(), |mut output: String, l| {
let _ = writeln!(output, "* {}", l);
let _ = writeln!(output, " * {}", l);
output
})
} else {
String::new()
};
format!("/**\n{}{}*/\n", body, doc)
if body.is_empty() && doc.is_empty() {
// don't emit empty doc comments
String::new()
} else {
format!("/**\n{}{} */\n", body, doc)
}
}

fn require_class<'a>(
Expand Down Expand Up @@ -4222,7 +4227,13 @@ impl ExportedClass {
self.contents.push_str(js);
self.contents.push('\n');
if let Some(ts) = ts {
self.typescript.push_str(docs);
if !docs.is_empty() {
for line in docs.lines() {
self.typescript.push_str(" ");
self.typescript.push_str(line);
self.typescript.push('\n');
}
}
self.typescript.push_str(" ");
self.typescript.push_str(function_prefix);
self.typescript.push_str(function_name);
Expand Down
11 changes: 9 additions & 2 deletions crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,13 @@ fn reset_indentation(s: &str) -> String {
let mut indent: u32 = 0;
let mut dst = String::new();

fn is_doc_comment(line: &str) -> bool {
line.starts_with("*")
}

for line in s.lines() {
let line = line.trim();
if line.starts_with('}') || (line.ends_with('}') && !line.starts_with('*')) {
if line.starts_with('}') || (line.ends_with('}') && !is_doc_comment(line)) {
indent = indent.saturating_sub(1);
}
let extra = if line.starts_with(':') || line.starts_with('?') {
Expand All @@ -506,11 +510,14 @@ fn reset_indentation(s: &str) -> String {
for _ in 0..indent + extra {
dst.push_str(" ");
}
if is_doc_comment(line) {
dst.push(' ');
}
dst.push_str(line);
}
dst.push('\n');
// Ignore { inside of comments and if it's an exported enum
if line.ends_with('{') && !line.starts_with('*') && !line.ends_with("Object.freeze({") {
if line.ends_with('{') && !is_doc_comment(line) && !line.ends_with("Object.freeze({") {
indent += 1;
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/cli/tests/reference/add.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* tslint:disable */
/* eslint-disable */
/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
* @param {number} a
* @param {number} b
* @returns {number}
*/
export function add_u32(a: number, b: number): number;
/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
* @param {number} a
* @param {number} b
* @returns {number}
*/
export function add_i32(a: number, b: number): number;
16 changes: 8 additions & 8 deletions crates/cli/tests/reference/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ export function __wbg_set_wasm(val) {
}

/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
* @param {number} a
* @param {number} b
* @returns {number}
*/
export function add_u32(a, b) {
const ret = wasm.add_u32(a, b);
return ret >>> 0;
}

/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
* @param {number} a
* @param {number} b
* @returns {number}
*/
export function add_i32(a, b) {
const ret = wasm.add_i32(a, b);
return ret;
Expand Down
2 changes: 0 additions & 2 deletions crates/cli/tests/reference/anyref-import-catch.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* tslint:disable */
/* eslint-disable */
/**
*/
export function exported(): void;
3 changes: 1 addition & 2 deletions crates/cli/tests/reference/anyref-import-catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ function takeFromExternrefTable0(idx) {
wasm.__externref_table_dealloc(idx);
return value;
}
/**
*/

export function exported() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
Expand Down
2 changes: 0 additions & 2 deletions crates/cli/tests/reference/anyref-nop.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* tslint:disable */
/* eslint-disable */
/**
*/
export function foo(): void;
3 changes: 1 addition & 2 deletions crates/cli/tests/reference/anyref-nop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ export function __wbg_set_wasm(val) {
wasm = val;
}

/**
*/

export function foo() {
wasm.foo();
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/tests/reference/async-number.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* tslint:disable */
/* eslint-disable */
/**
* @returns {Promise<number>}
*/
* @returns {Promise<number>}
*/
export function foo(): Promise<number>;
4 changes: 2 additions & 2 deletions crates/cli/tests/reference/async-void.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* tslint:disable */
/* eslint-disable */
/**
* @returns {Promise<void>}
*/
* @returns {Promise<void>}
*/
export function foo(): Promise<void>;
8 changes: 3 additions & 5 deletions crates/cli/tests/reference/builder.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* tslint:disable */
/* eslint-disable */
/**
*/
export class ClassBuilder {
free(): void;
/**
* @returns {ClassBuilder}
*/
/**
* @returns {ClassBuilder}
*/
static builder(): ClassBuilder;
}
7 changes: 3 additions & 4 deletions crates/cli/tests/reference/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ function getStringFromWasm0(ptr, len) {
const ClassBuilderFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_classbuilder_free(ptr >>> 0, 1));
/**
*/

export class ClassBuilder {

static __wrap(ptr) {
Expand All @@ -51,8 +50,8 @@ export class ClassBuilder {
wasm.__wbg_classbuilder_free(ptr, 0);
}
/**
* @returns {ClassBuilder}
*/
* @returns {ClassBuilder}
*/
static builder() {
const ret = wasm.classbuilder_builder();
return ClassBuilder.__wrap(ret);
Expand Down
4 changes: 0 additions & 4 deletions crates/cli/tests/reference/constructor.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* tslint:disable */
/* eslint-disable */
/**
*/
export class ClassConstructor {
free(): void;
/**
*/
constructor();
}
5 changes: 1 addition & 4 deletions crates/cli/tests/reference/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ function getStringFromWasm0(ptr, len) {
const ClassConstructorFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_classconstructor_free(ptr >>> 0, 1));
/**
*/

export class ClassConstructor {

__destroy_into_raw() {
Expand All @@ -42,8 +41,6 @@ export class ClassConstructor {
const ptr = this.__destroy_into_raw();
wasm.__wbg_classconstructor_free(ptr, 0);
}
/**
*/
constructor() {
const ret = wasm.classconstructor_new();
this.__wbg_ptr = ret >>> 0;
Expand Down
26 changes: 12 additions & 14 deletions crates/cli/tests/reference/enums.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
/* tslint:disable */
/* eslint-disable */
/**
* @param {Color} color
* @returns {Color}
*/
* @param {Color} color
* @returns {Color}
*/
export function enum_echo(color: Color): Color;
/**
* @param {Color | undefined} [color]
* @returns {Color | undefined}
*/
* @param {Color | undefined} [color]
* @returns {Color | undefined}
*/
export function option_enum_echo(color?: Color): Color | undefined;
/**
* @param {Color} color
* @returns {ColorName}
*/
* @param {Color} color
* @returns {ColorName}
*/
export function get_name(color: Color): ColorName;
/**
* @param {ColorName | undefined} [color]
* @returns {ColorName | undefined}
*/
* @param {ColorName | undefined} [color]
* @returns {ColorName | undefined}
*/
export function option_string_enum_echo(color?: ColorName): ColorName | undefined;
/**
*/
export enum Color {
Green = 0,
Yellow = 1,
Expand Down
26 changes: 12 additions & 14 deletions crates/cli/tests/reference/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}
/**
* @param {Color} color
* @returns {Color}
*/
* @param {Color} color
* @returns {Color}
*/
export function enum_echo(color) {
const ret = wasm.enum_echo(color);
return ret;
Expand All @@ -36,34 +36,32 @@ function isLikeNone(x) {
return x === undefined || x === null;
}
/**
* @param {Color | undefined} [color]
* @returns {Color | undefined}
*/
* @param {Color | undefined} [color]
* @returns {Color | undefined}
*/
export function option_enum_echo(color) {
const ret = wasm.option_enum_echo(isLikeNone(color) ? 3 : color);
return ret === 3 ? undefined : ret;
}

/**
* @param {Color} color
* @returns {ColorName}
*/
* @param {Color} color
* @returns {ColorName}
*/
export function get_name(color) {
const ret = wasm.get_name(color);
return ["green","yellow","red",][ret];
}

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

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

export function __wbindgen_throw(arg0, arg1) {
Expand Down
2 changes: 0 additions & 2 deletions crates/cli/tests/reference/import-catch.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* tslint:disable */
/* eslint-disable */
/**
*/
export function exported(): void;
3 changes: 1 addition & 2 deletions crates/cli/tests/reference/import-catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ function takeObject(idx) {
dropObject(idx);
return ret;
}
/**
*/

export function exported() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
Expand Down
2 changes: 0 additions & 2 deletions crates/cli/tests/reference/nop.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* tslint:disable */
/* eslint-disable */
/**
*/
export function nop(): void;
3 changes: 1 addition & 2 deletions crates/cli/tests/reference/nop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ export function __wbg_set_wasm(val) {
wasm = val;
}

/**
*/

export function nop() {
wasm.nop();
}
Expand Down
12 changes: 6 additions & 6 deletions crates/cli/tests/reference/pointers.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* tslint:disable */
/* eslint-disable */
/**
* @param {number} input
* @returns {number}
*/
* @param {number} input
* @returns {number}
*/
export function const_pointer(input: number): number;
/**
* @param {number} input
* @returns {number}
*/
* @param {number} input
* @returns {number}
*/
export function mut_pointer(input: number): number;
Loading
Loading