darklua icon indicating copy to clipboard operation
darklua copied to clipboard

Generic type parameter values with parenthese types cause invalid syntax

Open jeparlefrancais opened this issue 2 years ago • 1 comments

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 avatar Dec 22 '23 00:12 jeparlefrancais

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).

jeparlefrancais avatar Dec 22 '23 00:12 jeparlefrancais