From f30326997818c82f11888b430895a31c77387df5 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Wed, 20 Nov 2019 00:26:31 +0100 Subject: [PATCH] Use Elm.Docs instead of custom made docs.json wrapper --- demo/elm.json | 6 +- elm.json | 3 +- src/Review/ModuleInterface.elm | 179 --------------------------------- src/Review/Project.elm | 18 ++-- 4 files changed, 10 insertions(+), 196 deletions(-) delete mode 100644 src/Review/ModuleInterface.elm diff --git a/demo/elm.json b/demo/elm.json index e2d8ae45..508d3fac 100644 --- a/demo/elm.json +++ b/demo/elm.json @@ -13,20 +13,18 @@ "elm/browser": "1.0.1", "elm/core": "1.0.2", "elm/html": "1.0.0", - "elm/json": "1.1.3", "elm/project-metadata-utils": "1.0.0", "jinjor/elm-diff": "1.0.6", "stil4m/elm-syntax": "7.1.0" }, "indirect": { + "elm/json": "1.1.3", "elm/parser": "1.1.0", - "elm/random": "1.0.0", "elm/time": "1.0.0", "elm/url": "1.0.0", "elm/virtual-dom": "1.0.2", "elm-community/json-extra": "4.0.0", "elm-community/list-extra": "8.2.2", - "elm-explorations/test": "1.2.2", "rtfeldman/elm-hex": "1.0.0", "rtfeldman/elm-iso8601-date-strings": "1.1.3", "stil4m/structured-writer": "1.0.2" @@ -36,4 +34,4 @@ "direct": {}, "indirect": {} } -} +} \ No newline at end of file diff --git a/elm.json b/elm.json index 44f69da4..889e1cce 100644 --- a/elm.json +++ b/elm.json @@ -15,10 +15,9 @@ "elm-version": "0.19.0 <= v < 0.20.0", "dependencies": { "elm/core": "1.0.2 <= v < 2.0.0", - "elm/json": "1.0.0 <= v < 2.0.0", "elm/project-metadata-utils": "1.0.0 <= v < 2.0.0", "elm-explorations/test": "1.2.2 <= v < 2.0.0", "stil4m/elm-syntax": "7.1.0 <= v < 8.0.0" }, "test-dependencies": {} -} +} \ No newline at end of file diff --git a/src/Review/ModuleInterface.elm b/src/Review/ModuleInterface.elm deleted file mode 100644 index 5aae7392..00000000 --- a/src/Review/ModuleInterface.elm +++ /dev/null @@ -1,179 +0,0 @@ -module Review.ModuleInterface exposing (Exposed(..), fromDocsJson) - -import Elm.Interface as Interface -import Elm.Syntax.Infix as Infix exposing (InfixDirection) -import Elm.Syntax.Node as Node exposing (Node) -import Elm.Syntax.Range as Range -import Json.Decode as Decode - - - --- DEFINITION - - -type Exposed - = CustomType - { name : String - , arguments : List String - , cases : List Case - } - | TypeAlias - { name : String - , arguments : List String - , type_ : String - } - | Value - { name : String - , type_ : String - } - | Operator - { name : String - , type_ : String - , precedence : Int - , associativity : InfixDirection - } - - -type alias Case = - { name : String - , arguments : List String - } - - - --- DECODE - - -fromDocsJson : Decode.Decoder (List ( String, List Exposed )) -fromDocsJson = - Decode.list decodeItem - - -decodeItem : Decode.Decoder ( String, List Exposed ) -decodeItem = - Decode.map5 - (\name customTypes aliases values operators -> - ( name, List.concat [ customTypes, aliases, values, operators ] ) - ) - (Decode.field "name" Decode.string) - (Decode.field "unions" (Decode.list decodeCustomType)) - (Decode.field "aliases" (Decode.list decodeTypeAlias)) - (Decode.field "values" (Decode.list decodeValue)) - (Decode.field "binops" (Decode.list decodeOperator)) - - -decodeCustomType : Decode.Decoder Exposed -decodeCustomType = - Decode.map3 - (\name arguments cases -> - CustomType - { name = name - , arguments = arguments - , cases = cases - } - ) - (Decode.field "name" Decode.string) - (Decode.field "args" (Decode.list Decode.string)) - (Decode.field "cases" (Decode.list decodeCase)) - - -decodeCase : Decode.Decoder Case -decodeCase = - Decode.map2 Case - (Decode.field "0" Decode.string) - (Decode.field "1" (Decode.list Decode.string)) - - -decodeTypeAlias : Decode.Decoder Exposed -decodeTypeAlias = - Decode.map3 - (\name arguments type_ -> - TypeAlias - { name = name - , arguments = arguments - , type_ = type_ - } - ) - (Decode.field "name" Decode.string) - (Decode.field "args" (Decode.list Decode.string)) - (Decode.field "type" Decode.string) - - -decodeValue : Decode.Decoder Exposed -decodeValue = - Decode.map2 - (\name type_ -> - Value - { name = name - , type_ = type_ - } - ) - (Decode.field "name" Decode.string) - (Decode.field "type" Decode.string) - - -decodeOperator : Decode.Decoder Exposed -decodeOperator = - Decode.map4 - (\name type_ precedence associativity -> - Operator - { name = name - , type_ = type_ - , precedence = precedence - , associativity = associativity - } - ) - (Decode.field "name" Decode.string) - (Decode.field "type" Decode.string) - (Decode.field "precedence" Decode.int) - (Decode.field "associativity" decodeAssociativity) - - -decodeAssociativity : Decode.Decoder InfixDirection -decodeAssociativity = - Decode.string - |> Decode.andThen - (\infix -> - case infix of - "left" -> - Decode.succeed Infix.Left - - "non" -> - Decode.succeed Infix.Non - - "right" -> - Decode.succeed Infix.Right - - _ -> - Decode.fail "Unknown associativity for operator" - ) - - - --- ELM-SYNTAX - - -toElmSyntaxInterface : List Exposed -> Interface.Interface -toElmSyntaxInterface exposedList = - List.map toElmSyntaxExposed exposedList - - -toElmSyntaxExposed : Exposed -> Interface.Exposed -toElmSyntaxExposed exposed = - case exposed of - CustomType { name, cases } -> - Interface.CustomType ( name, List.map .name cases ) - - TypeAlias { name } -> - Interface.Alias name - - Value { name } -> - Interface.Function name - - Operator { name, associativity, precedence } -> - Interface.Operator - { operator = Node.Node Range.emptyRange name - , direction = Node.Node Range.emptyRange associativity - , precedence = Node.Node Range.emptyRange precedence - , function = Node.Node Range.emptyRange name - } diff --git a/src/Review/Project.elm b/src/Review/Project.elm index 92244896..62db23f9 100644 --- a/src/Review/Project.elm +++ b/src/Review/Project.elm @@ -28,10 +28,10 @@ rules to have access to, to later pass it to the [`Review.review`](./Review#revi -} import Dict exposing (Dict) +import Elm.Docs import Elm.Interface exposing (Interface) import Elm.Project import Elm.Syntax.ModuleName exposing (ModuleName) -import Review.ModuleInterface as ModuleInterface @@ -44,8 +44,8 @@ the `elm.json` file. type Project = Project { elmJson : Maybe ElmJson - , interfaces : Dict ModuleName (List ModuleInterface.Exposed) - , moduleToDependency : Dict ModuleName String + , interfaces : Dict String Elm.Docs.Module + , moduleToDependency : Dict String String } @@ -81,7 +81,7 @@ package, so you will need to install and use it to gain access to the information inside the `elm.json` file. -} -interfaces : Project -> Dict ModuleName (List ModuleInterface.Exposed) +interfaces : Project -> Dict String Elm.Docs.Module interfaces (Project project) = project.interfaces @@ -110,22 +110,18 @@ withElmJson elmJson_ (Project project) = {-| Add a dependency to the project -} -withDependency : { packageName : String, interfaces : List ( String, List ModuleInterface.Exposed ) } -> Project -> Project +withDependency : { r | packageName : String, interfaces : List Elm.Docs.Module } -> Project -> Project withDependency dependency (Project project) = Project { project | interfaces = dependency.interfaces - |> List.map (Tuple.mapFirst (String.split ".")) + |> List.map (\module_ -> ( module_.name, module_ )) |> Dict.fromList |> Dict.union project.interfaces , moduleToDependency = dependency.interfaces - |> List.map - (Tuple.mapBoth - (String.split ".") - (always dependency.packageName) - ) + |> List.map (\module_ -> ( module_.name, dependency.packageName )) |> Dict.fromList |> Dict.union project.moduleToDependency }