Skip to content

elm-format breaks code that contain duplicate imports #834

@EvansJahja

Description

@EvansJahja

elm-format 0.8.8

Description:
A file containing multiple imports with different import alias, such as import Json.Encode and import Json.Encode as Encode will mistakenly have import Json.Encode being removed, but forgot to update the usages in code

Here's a minimal code:

Sample.elm

module Sample exposing (..)
import Json.Encode
import Json.Encode as Encode
import Browser exposing (sandbox)
import Html exposing (Html)

view : () -> Html.Html msg
view model =
    let
        _ = Json.Encode.object [ ("key", Json.Encode.string "value") ]
    in
        Html.div [] [ Html.text "Hello, Elm!" ]

main : Program () () ()
main =
    sandbox
        { init = ()
        , update = \_ model -> model
        , view = view
        }
    

npx elm-format .\src\Sample.elm

Output

module Sample exposing (..)

import Browser exposing (sandbox)
import Html exposing (Html)
import Json.Encode as Encode


view : () -> Html.Html msg
view model =
    let
        _ =
            Json.Encode.object [ ( "key", Json.Encode.string "value" ) ]
    in
    Html.div [] [ Html.text "Hello, Elm!" ]


main : Program () () ()
main =
    sandbox
        { init = ()
        , update = \_ model -> model
        , view = view
        }

The output now contains error, as it only keeps import Json.Encode as Encode but keep using Json.Encode in the code

I expect the code to convert those instances to Encode.object instead of Json.Encode.object

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions