elm-review/example/Main.elm

150 lines
3.0 KiB
Elm
Raw Normal View History

module Main exposing (main)
2017-01-07 22:58:41 +03:00
import Result
import Ast
import Ast.Expression exposing (..)
import Ast.Statement exposing (..)
2017-01-07 22:58:41 +03:00
import Html exposing (Html, p, div, li, ul, pre, textarea, text)
2017-01-16 00:57:03 +03:00
import Html.Attributes exposing (id, class)
import Html.Events exposing (..)
import Json.Decode as JD
import Types
2017-01-08 13:44:31 +03:00
-- Rules
import NoUnannotatedFunction
2017-01-08 13:44:31 +03:00
import NoDebug
import NoExposingEverything
2017-01-17 01:32:06 +03:00
import NoUnusedVariables
type Msg
= Replace String
init : String
init =
"""module Main exposing (..)
f : Int -> Int
2017-01-07 23:17:01 +03:00
f x = x Debug.log 1
2017-01-07 23:17:01 +03:00
a : a -> a
a = Debug.log "foo" x
2017-01-07 23:17:01 +03:00
h = f << Debug.log
"""
update : Msg -> String -> String
update action model =
case action of
Replace m ->
m
withChild : a -> List (Html Msg) -> Html Msg
withChild title children =
li []
[ pre [] [ text <| toString title ]
, ul [] children
]
expression : Expression -> Html Msg
expression e =
case e of
List es ->
withChild e (List.map expression es)
Application e1 e2 ->
withChild e
[ expression e1
, expression e2
]
e ->
li [] [ pre [] [ text <| toString e ] ]
statement : Statement -> Html Msg
statement s =
case s of
FunctionDeclaration _ _ e ->
withChild s [ expression e ]
s ->
li [] [ pre [] [ text <| toString s ] ]
2017-01-07 22:58:41 +03:00
tree : Result a ( b, c, List Statement ) -> Html Msg
tree ast =
case ast of
Ok ( _, _, statements ) ->
ul [] (List.map statement statements)
err ->
div [] [ text <| toString err ]
rules : List (String -> List Types.Error)
rules =
[ NoDebug.rule
, NoExposingEverything.rule
, NoUnannotatedFunction.rule
2017-01-17 01:32:06 +03:00
, NoUnusedVariables.rule
]
lint : String -> Html Msg
lint source =
2017-01-07 22:58:41 +03:00
let
errors =
List.concatMap (\rule -> rule source) rules
messages =
if List.isEmpty errors then
[ "No issues here." ]
else
List.map (\err -> err.rule ++ ": " ++ err.message) errors
2017-01-07 22:58:41 +03:00
in
div []
(List.map
(\message -> p [] [ text message ])
messages
)
2017-01-07 22:58:41 +03:00
view : String -> Html Msg
view model =
div [ id "wrapper" ]
2017-01-16 00:57:03 +03:00
[ div [ id "left" ]
[ p [ class "title" ] [ text "Source code" ]
, textarea
[ id "input"
, on "input" (JD.map Replace targetValue)
]
[ text model ]
, div []
[ p [ class "title" ] [ text "Linting errors" ]
, div [ id "lint" ]
[ lint model
]
]
]
, div [ id "right" ]
2017-01-16 00:57:03 +03:00
[ p [ class "title" ] [ text "AST" ]
, p [ id "ast" ] [ tree <| Ast.parse model ]
2017-01-07 22:58:41 +03:00
]
]
main : Program Never String Msg
main =
Html.beginnerProgram
{ model = init
, update = update
, view = view
}