-
Notifications
You must be signed in to change notification settings - Fork 54
Check modelDef.getType()
is defined before use
#353
Check modelDef.getType()
is defined before use
#353
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.
Thanks @blakeembrey!
- Could you give me a bit more hint on how to repro
- Could you add a test case for this in
common.spec.ts
- Also would like the static types improved so the evident nullability is modelled
I'll try to contribute commits helping each point when I find time. Feel free to do it sooner if you wish
: )
@jasonkuhrt It's replicated like in the PR description. I narrowed it down to |
This will just fix the crashing behavior, not actually add support for type alias syntax for models, right? I had to add this fix inline when first playing with the tool, but my aliased model (in OP example Relevant #282 |
@ksaldana1 Yes, exactly. I think solving that might be a much more complex problem. I believe the current TS implementation is a parser and not a full language services which means it doesn't support following and resolving imports (correct me if I'm wrong). |
Yes that seems to be correct @blakeembrey . It is using @babel/parser and the AST utils around that. I'm not as familiar with it as I am with the TS compiler, so I'll have to look into it a bit. |
) { | ||
const interfaceDef = (modelDef as TypeAliasDefinition).getType() as AnonymousInterfaceAnnotation | ||
const interfaceDef = modelDef.getType() as AnonymousInterfaceAnnotation |
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.
Thanks for the cleanup! I probably should have looked a little higher level than my as
bike-sheds. I think this code should probably look something more like:
// we already know that modelDef.kind === 'TypeAliasDefinition' here
const interfaceDef = modelDef.getType()
if (interfaceDef && interfaceDef.kind === 'AnonymousInterfaceAnnotation') {
return interfaceDef.fields
}
return []
Kinda irrelevant though because it seems these types aren't fully capturing everything being passed in at runtime. Don't think it's worth hanging up on code hygiene until that's sorted out.
Thanks @blakeembrey, @ksaldana1 I better understand what's going on now. This PR achieves its direct goal as well as removing some useless/unsafe type casting. Let's ship this. I am keen to dig into the underlying issues in further PRs, namely:
|
@jasonkuhrt The runtime issue comes from: if (type.kind === 'TypeReferenceAnnotation') {
return () => filesToTypesMap[filePath][type.referenceType]
} else {
return () => type
} Specifically, |
It was crashing otherwise. The type format used was directly an alias: