Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
enum
s caused two issues (1 and 2 below), the fix for one of which (2) led to another issue (3)1. Singleton
enum
members are desugared toval
s instead ofobject
sThis breaks the way that we generate references to ADT members in TypeScript code. Calling
.getClass.getName
shows that it's an anonymous instance of the class, and.getClass.getSimpleName
returns an empty string.The fix
871b32b
If the value we're referencing is a
Product
, we can use itsproductPrefix
instead ofgetClass.getSimpleName
. This produces the same result and works properly forenum
members.2. Getting the type of a
val
defined in anenum
member didn’t workWe previously got the type of a
val
using itsValDef
tree, and expected that tree to have a right-hand side (which is whatSome(term)
is in that code). This doesn't work withenum
s, theValDef
trees haveNone
for the RHS.The fix
We can get the type of the
val
usingtypeRepr.memberType(sym)
wheretypeRepr
is theTypeRepr
that represents theenum
member andsym
is theSymbol
that represents the theval
.This has one benefit: we no longer require the
-Yretain-trees
compiler option, as we're no longer using theValDef
tree.It also has one major drawback: the result of
typeRepr.memberType(sym)
is a less specific type than what we get now (see below).3. The type we can determine for
enum
members'val
s is less specificOur old approach (described in issue 2 above) would produce the most specific type possible, whereas the new approach produces the type defined in the member's parent.
This ultimately causes issues with ADTs that contain references to themselves, or to other types being generated in the same TypeScript file.
Our previous type sorting logic relies on an instance of
ReferencedTypes
to know which types reference others, but with the less specific types in place, these references weren't reliable. For example:The fix
f53c463
Instead of producing a
ReferencedTypes
based on theTypeName
s contained within theTsModel
s being generated, we now sort definitions by inspecting the generated code. We first find the names of all definedconst
s,function
s, andtype
s. We then look for references to those names in other definitions, and sort the definitions accordingly.