Skip to content

Commit

Permalink
derive-typescript: fix codegen for empty enums
Browse files Browse the repository at this point in the history
We saw a derivation that failed validation because the generated Typescript
code was syntactically invalid. This was caused by the inferred `Shape` having
an empty set of enum variants (`enum_: Some([])`).  The empty enum variants
cause the generated code to omit any sort of type, which causes Deno to fail to
parse it.

I'm not completely certain whether the fault is with the Typescript code
generation or the Shape inferrence, but it's pretty easy to filter out empty
enums in the code generation, which results in generated code correctly having
the `never` type.

I also added a debug log of the generated types file, so that this type of
thing is easier to debug in the future.
  • Loading branch information
psFried committed Jan 7, 2025
1 parent b91302b commit 7d11f2d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/derive-typescript/src/codegen/mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Mapper {
return AST::Unknown;
}
// Is this an enum? Just emit the variants.
if let Some(enum_) = &shape.enum_ {
if let Some(enum_) = shape.enum_.as_ref().filter(|e| !e.is_empty()) {
return AST::Union {
variants: enum_
.iter()
Expand Down
1 change: 1 addition & 0 deletions crates/derive-typescript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ fn validate(validate: derive::request::Validate) -> anyhow::Result<derive::respo

let types_url = format!("{project_root}/{GENERATED_PREFIX}/{}.ts", collection.name);
let types_content = codegen::types_ts(&collection, &transforms);
tracing::debug!(%types_content, "generated TS types");

let mut generated_files: Vec<(String, String)> = vec![
(types_url.clone(), types_content.clone()),
Expand Down

0 comments on commit 7d11f2d

Please sign in to comment.