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

Add JSDoc annotations to ensure enums type check even without .d.ts file #4157

Merged
merged 9 commits into from
Oct 16, 2024
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* Added bindings for [MathMLElement](https://www.w3.org/TR/MathML3).
[#4143](https://github.com/rustwasm/wasm-bindgen/pull/4143)

* Added JSDoc type annotations to C-style enums.
[#4192](https://github.com/rustwasm/wasm-bindgen/pull/4192)

### Changed

* String enums now generate private TypeScript types but only if used.
Expand Down
29 changes: 20 additions & 9 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3776,11 +3776,11 @@ __wbg_set_wasm(wasm);"
}

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

if enum_.generate_typescript {
self.typescript.push_str(&docs);
self.typescript
.push_str(&format_doc_comments(&enum_.comments, None));
self.typescript
.push_str(&format!("export enum {} {{", enum_.name));
}
Expand Down Expand Up @@ -3811,6 +3811,18 @@ __wbg_set_wasm(wasm);"
if enum_.generate_typescript {
self.typescript.push_str("\n}\n");
}

// add an `@enum {1 | 2 | 3}` to ensure that enums type-check even without .d.ts
let mut at_enum = "@enum {".to_string();
for (i, (_, value, _)) in enum_.variants.iter().enumerate() {
if i != 0 {
at_enum.push_str(" | ");
}
at_enum.push_str(&value.to_string());
}
at_enum.push('}');
let docs = format_doc_comments(&enum_.comments, Some(at_enum));

self.export(
&enum_.name,
&format!("Object.freeze({{ {} }})", variants),
Expand All @@ -3833,18 +3845,17 @@ __wbg_set_wasm(wasm);"
.contains(&TsReference::StringEnum(string_enum.name.clone()))
{
let docs = format_doc_comments(&string_enum.comments, None);
let type_expr = if variants.is_empty() {
"never".to_string()
} else {
variants.join(" | ")
};

self.typescript.push_str(&docs);
self.typescript.push_str("type ");
self.typescript.push_str(&string_enum.name);
self.typescript.push_str(" = ");

if variants.is_empty() {
self.typescript.push_str("never");
} else {
self.typescript.push_str(&variants.join(" | "));
}

self.typescript.push_str(&type_expr);
self.typescript.push_str(";\n");
}

Expand Down
5 changes: 4 additions & 1 deletion crates/cli/tests/reference/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function option_string_enum_echo(color) {

/**
* A color.
* @enum {0 | 1 | 2}
*/
export const Color = Object.freeze({
/**
Expand All @@ -78,7 +79,9 @@ Yellow:1,"1":"Yellow",
* Red as a rose.
*/
Red:2,"2":"Red", });

/**
* @enum {0 | 1 | 42 | 43}
*/
export const ImplicitDiscriminant = Object.freeze({ A:0,"0":"A",B:1,"1":"B",C:42,"42":"C",D:43,"43":"D", });

const __wbindgen_enum_ColorName = ["green", "yellow", "red"];
Expand Down