Skip to content

Commit 76776ef

Browse files
Add JSDoc type annotations to C-style enums (#4157)
1 parent 7c7cd51 commit 76776ef

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
* Added bindings for [MathMLElement](https://www.w3.org/TR/MathML3).
1212
[#4143](https://github.com/rustwasm/wasm-bindgen/pull/4143)
1313

14+
* Added JSDoc type annotations to C-style enums.
15+
[#4192](https://github.com/rustwasm/wasm-bindgen/pull/4192)
16+
1417
### Changed
1518

1619
* String enums now generate private TypeScript types but only if used.

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

+20-9
Original file line numberDiff line numberDiff line change
@@ -3773,11 +3773,11 @@ __wbg_set_wasm(wasm);"
37733773
}
37743774

37753775
fn generate_enum(&mut self, enum_: &AuxEnum) -> Result<(), Error> {
3776-
let docs = format_doc_comments(&enum_.comments, None);
37773776
let mut variants = String::new();
37783777

37793778
if enum_.generate_typescript {
3780-
self.typescript.push_str(&docs);
3779+
self.typescript
3780+
.push_str(&format_doc_comments(&enum_.comments, None));
37813781
self.typescript
37823782
.push_str(&format!("export enum {} {{", enum_.name));
37833783
}
@@ -3808,6 +3808,18 @@ __wbg_set_wasm(wasm);"
38083808
if enum_.generate_typescript {
38093809
self.typescript.push_str("\n}\n");
38103810
}
3811+
3812+
// add an `@enum {1 | 2 | 3}` to ensure that enums type-check even without .d.ts
3813+
let mut at_enum = "@enum {".to_string();
3814+
for (i, (_, value, _)) in enum_.variants.iter().enumerate() {
3815+
if i != 0 {
3816+
at_enum.push_str(" | ");
3817+
}
3818+
at_enum.push_str(&value.to_string());
3819+
}
3820+
at_enum.push('}');
3821+
let docs = format_doc_comments(&enum_.comments, Some(at_enum));
3822+
38113823
self.export(
38123824
&enum_.name,
38133825
&format!("Object.freeze({{ {} }})", variants),
@@ -3830,18 +3842,17 @@ __wbg_set_wasm(wasm);"
38303842
.contains(&TsReference::StringEnum(string_enum.name.clone()))
38313843
{
38323844
let docs = format_doc_comments(&string_enum.comments, None);
3845+
let type_expr = if variants.is_empty() {
3846+
"never".to_string()
3847+
} else {
3848+
variants.join(" | ")
3849+
};
38333850

38343851
self.typescript.push_str(&docs);
38353852
self.typescript.push_str("type ");
38363853
self.typescript.push_str(&string_enum.name);
38373854
self.typescript.push_str(" = ");
3838-
3839-
if variants.is_empty() {
3840-
self.typescript.push_str("never");
3841-
} else {
3842-
self.typescript.push_str(&variants.join(" | "));
3843-
}
3844-
3855+
self.typescript.push_str(&type_expr);
38453856
self.typescript.push_str(";\n");
38463857
}
38473858

crates/cli/tests/reference/enums.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export function option_string_enum_echo(color) {
6464

6565
/**
6666
* A color.
67+
* @enum {0 | 1 | 2}
6768
*/
6869
export const Color = Object.freeze({
6970
/**
@@ -78,7 +79,9 @@ Yellow:1,"1":"Yellow",
7879
* Red as a rose.
7980
*/
8081
Red:2,"2":"Red", });
81-
82+
/**
83+
* @enum {0 | 1 | 42 | 43}
84+
*/
8285
export const ImplicitDiscriminant = Object.freeze({ A:0,"0":"A",B:1,"1":"B",C:42,"42":"C",D:43,"43":"D", });
8386

8487
const __wbindgen_enum_ColorName = ["green", "yellow", "red"];

0 commit comments

Comments
 (0)