Vendorize Elm.Project.encode

This commit is contained in:
Jeroen Engels 2021-04-14 22:30:11 +02:00
parent 05d2622923
commit 47ff4a0f0f
2 changed files with 76 additions and 1 deletions

View File

@ -0,0 +1,74 @@
module Review.ElmProjectEncoder exposing (encode)
import Elm.Constraint as Constraint exposing (Constraint)
import Elm.License as License exposing (License)
import Elm.Module as Module
import Elm.Package as Package
import Elm.Project exposing (Deps, Exposed(..), Project(..))
import Elm.Version as Version exposing (Version)
import Json.Encode as E
{-| Turn a `Project` into the JSON that goes in `elm.json`
-}
encode : Project -> E.Value
encode project =
case project of
Application { elm, dirs, depsDirect, depsIndirect, testDepsDirect, testDepsIndirect } ->
E.object
[ ( "type", E.string "application" )
, ( "source-directories", E.list E.string dirs )
, ( "elm-version", Version.encode elm )
, ( "dependencies"
, E.object
[ ( "direct", encodeDeps Version.encode depsDirect )
, ( "indirect", encodeDeps Version.encode depsIndirect )
]
)
, ( "test-dependencies"
, E.object
[ ( "direct", encodeDeps Version.encode testDepsDirect )
, ( "indirect", encodeDeps Version.encode testDepsIndirect )
]
)
]
Package { name, summary, license, version, exposed, deps, testDeps, elm } ->
E.object
[ ( "type", E.string "package" )
, ( "name", Package.encode name )
, ( "summary", E.string summary )
, ( "license", License.encode license )
, ( "version", Version.encode version )
, ( "exposed-modules", encodeExposed exposed )
, ( "elm-version", Constraint.encode elm )
, ( "dependencies", encodeDeps Constraint.encode deps )
, ( "test-dependencies", encodeDeps Constraint.encode testDeps )
]
encodeExposed : Exposed -> E.Value
encodeExposed exposed =
case exposed of
ExposedList modules ->
E.list Module.encode modules
ExposedDict chunks ->
E.object (List.map encodeChunk chunks)
encodeChunk : ( String, List Module.Name ) -> ( String, E.Value )
encodeChunk ( header, list ) =
( header, E.list Module.encode list )
encodeDeps : (constraint -> E.Value) -> Deps constraint -> E.Value
encodeDeps encodeConstraint deps =
E.object <|
List.sortBy Tuple.first <|
List.map (encodeDep encodeConstraint) deps
encodeDep : (constraint -> E.Value) -> ( Package.Name, constraint ) -> ( String, E.Value )
encodeDep encodeConstraint ( name, constraint ) =
( Package.toString name, encodeConstraint constraint )

View File

@ -278,6 +278,7 @@ import Elm.Syntax.Type
import Elm.Syntax.TypeAnnotation as TypeAnnotation exposing (TypeAnnotation) import Elm.Syntax.TypeAnnotation as TypeAnnotation exposing (TypeAnnotation)
import Elm.Type import Elm.Type
import Json.Encode as Encode import Json.Encode as Encode
import Review.ElmProjectEncoder
import Review.Error exposing (InternalError) import Review.Error exposing (InternalError)
import Review.Exceptions as Exceptions exposing (Exceptions) import Review.Exceptions as Exceptions exposing (Exceptions)
import Review.Fix as Fix exposing (Fix) import Review.Fix as Fix exposing (Fix)
@ -3115,7 +3116,7 @@ errorForElmJsonWithFix (ElmJsonKey elmJson) getErrorInfo getFix =
encoded : String encoded : String
encoded = encoded =
updatedProject updatedProject
|> Elm.Project.encode |> Review.ElmProjectEncoder.encode
|> Encode.encode 4 |> Encode.encode 4
in in
[ Fix.replaceRangeBy [ Fix.replaceRangeBy