seaofvoices / darklua

A command line tool that transforms Lua code
https://darklua.com/
MIT License
71 stars 9 forks source link

Generic type parameter values with parenthese types cause invalid syntax #170

Open jeparlefrancais opened 8 months ago

jeparlefrancais commented 8 months ago

When processing a type like this:

type _fieldDefStack = Array<GraphQLField<any, any>? | NULL>

Darklua currently adds parentheses around GraphQLField<any, any>? which creates invalid syntax:

type _fieldDefStack = Array<(GraphQLField<any, any>?) | NULL> -- invalid in Luau!

This is odd because the syntax defines type params as:

TypeParams = (Type | TypePack | VariadicTypePack | GenericTypePack) [',' TypeParams]

Which makes `(GraphQLField<any, any>?) | NULL a valid type. Luau also agrees that this is correct syntax, as it accepts this type declaration:

type _fieldDefStack = (GraphQLField<any, any>?) | NULL

It seems like the fix for this would be to wrap the type params values in a parenthese when they contain a type union or intersection where the left side type is a parenthese type. Weirdly specific thing to add, but I think it'll do it.

jeparlefrancais commented 8 months ago

Another fix for this is also to not add parentheses around the left side of the union, but this only works if the inner type is compatible with the union (or intersection).