mdgriffith / elm-codegen

https://package.elm-lang.org/packages/mdgriffith/elm-codegen/latest/
BSD 3-Clause "New" or "Revised" License
138 stars 16 forks source link

Fully resolve names when generating bindings #78

Closed miniBill closed 10 months ago

peteygao commented 10 months ago

I can confirm that this PR fixes an issue that I ran into using elm-generic-dict.

Let's say I have a type in the file Bridge.elm:

type Email =
    Email String

emailToString : Email -> String
emailToString (Email email) =
    email

Meant to be used as a tagged value, so I create Generated.elm as such:

main : Program {} () ()
main =
    Generate.run 
        [ emailDictFile ]

emailDictFile : Elm.File
emailDictFile =
    GenericDict.init
        { keyType = Gen.Bridge.annotation_.email
        , namespace = []
        , toComparable = Gen.Bridge.emailToString
        }
        |> GenericDict.withTypeName "EmailDict"
        |> GenericDict.generateFile

Executing elm-codegen run throws this error/warning in the terminal:

--ELM CODEGEN WARNING-----------------------------------------------------------
In the generated file: EmailDict.elm

    When trying to figure out the type for member, I ran into an issue

    I found

        Email

    But I was expecting:

        Bridge.Email

    I'm not as smart as the Elm compiler :/, but we're good friends.  I especially get confused when there are a lot of type aliases.
    If you need to, try using Elm.withType to tell me what the type should be!

And the resulting file is this mangled thing (which I can manually fix by replacing the types where the error messages are printed):

import Bridge
import Dict

type EmailDict 

I found

  was expecting:

 v
    = EmailDict
        (Dict.Dict (Err: 

I found

    Email

But I was expecting:

    Bridge.Email) ( Bridge.Email, v ))

{-... rest of the file is well-formed ...-}

The manually fixed file looks like this, and it works fine:

import Bridge
import Dict

type EmailDict v
    = EmailDict (Dict.Dict String ( Bridge.Email, v ))

This PR fixes it so that there's no mangled output in the resulting generated file, and that it properly resolves it as the base type String instead of Bridge.Email which is incorrect.

mdgriffith commented 10 months ago

Thank you both! Just published 4.4.1 on elm package. Thank you for both the work and the verification, that's a huge load of my brain 🙏

peteygao commented 10 months ago

@mdgriffith Thanks for publishing a new version! Minor issue with the new release: elm-codegen --version version 0.5.1 still reports itself as 0.5.0 since the CLI's version number is hardcoded in cli/bin.ts. This threw me for a loop initially after I updated the package using npm and checked to make sure I had the latest version, and still saw 0.5.0 😅

mdgriffith commented 10 months ago

@peteygao lol, oh no, lemme take a look

mdgriffith commented 10 months ago

Just published 0.5.2 with the only change being that --version reads as 0.5.2 😹