-
Notifications
You must be signed in to change notification settings - Fork 44
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
feat: Add fast-check based TypeScript type definition generation #379
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's looking good, but we should spend some time handling unknown variants a bit before landing this so that we don't swallow errors and can be alerted better to problems (either by debug panics or surfacing diagnostics that dts generation failed).
Co-authored-by: David Sherret <dsherret@users.noreply.github.com>
@dsherret This is ready to be reviewed now. Made an integration into jsr as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there!
fn gen_unique_name(&mut self) -> String { | ||
self.id_counter += 1; | ||
format!("_dts_{}", self.id_counter) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future we can make this better to actually avoid conflicts, but the chance of that happening are so slim (ex. do a pre-pass on the file and collect all the identifier names--including globals referenced in types and put them in a hashset, then here it would try something similar, but check for conflicts)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to fix that when it arises. It seems like super edge case and unlikely to happen in practice.
@dsherret addressed the feedback and it's ready for another review. |
} | ||
} | ||
|
||
Expr::TsAs(ts_as) => self.valid_enum_ts_type(*ts_as.type_ann), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get an error for this in declaration files
In ambient enum declarations member initializer must be constant expression.ts(1066)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to work for a very small subset:
export enum Foo {
A = 1 as number,
}
F = Foo1.A | Foo1.C, | ||
G = Foo1.A + Foo1.C, | ||
H, | ||
I = 1 + NUM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems this is also not allowed in dts files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah wanted to add a couple cases where TS strips the type but still generates the files even with a type error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably shouldn't follow TS here and instead just strip this with a warning diagnostic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Nice work!
This PR adds the ability to generate TypeScript definition files (
*.d.ts
) when fast check is enabled.