elm-review/example/Main.elm

125 lines
3.2 KiB
Elm
Raw Normal View History

module Main exposing (main)
2018-11-05 20:59:42 +03:00
-- Rules
2018-11-05 21:06:03 +03:00
import Browser
2017-06-12 21:04:02 +03:00
import Html exposing (..)
2018-11-06 15:08:20 +03:00
import Html.Attributes exposing (class, id, style)
2018-11-05 21:30:47 +03:00
import Html.Events exposing (onInput)
2018-11-11 01:37:18 +03:00
import Lint exposing (Rule, Severity(..), lintSource)
2019-06-07 20:52:17 +03:00
import Lint.Rule.DefaultPatternPosition
2018-11-11 01:43:58 +03:00
import Lint.Rule.NoDebug
2019-06-03 01:30:24 +03:00
import Lint.Rule.NoImportingEverything
2018-11-22 21:19:19 +03:00
import Lint.Rule.NoUnusedVariables
import Lint.RuleError exposing (RuleError)
2018-11-05 21:30:47 +03:00
import Result exposing (Result)
type Msg
= Replace String
2018-11-11 01:37:18 +03:00
config : List ( Severity, Rule )
2017-06-17 12:56:34 +03:00
config =
2018-11-11 01:43:58 +03:00
[ ( Critical, Lint.Rule.NoDebug.rule )
2018-11-22 21:19:19 +03:00
, ( Critical, Lint.Rule.NoUnusedVariables.rule )
2019-06-03 01:30:24 +03:00
, ( Critical, Lint.Rule.NoImportingEverything.rule { exceptions = [ "Html" ] } )
2019-06-07 20:52:17 +03:00
, ( Critical, Lint.Rule.DefaultPatternPosition.rule { position = Lint.Rule.DefaultPatternPosition.Last } )
2018-11-11 01:43:58 +03:00
-- , ( Critical, Lint.Rule.NoConstantCondition.rule )
-- , ( Critical, Lint.Rule.NoDuplicateImports.rule )
-- , ( Critical, Lint.Rule.NoExposingEverything.rule )
-- , ( Critical, Lint.Rule.NoNestedLet.rule )
-- , ( Critical, Lint.Rule.NoUnannotatedFunction.rule )
-- , ( Critical, Lint.Rule.NoUselessIf.rule )
-- , ( Critical, Lint.Rule.NoUselessPatternMatching.rule )
-- , ( Warning, Lint.Rule.NoWarningComments.rule )
-- , ( Critical, Lint.Rule.SimplifyPiping.rule )
-- , ( Critical, Lint.Rule.SimplifyPropertyAccess.rule )
-- , ( Critical, Lint.Rule.ElmTest.NoDuplicateTestBodies.rule )
2017-01-20 01:31:55 +03:00
]
init : String
init =
2017-01-19 23:02:21 +03:00
"""module Main exposing (f)
2019-06-03 01:30:24 +03:00
import Html.Events exposing (..)
2017-01-20 00:27:11 +03:00
import Html exposing (..)
f : Int -> Int
2017-01-07 23:17:01 +03:00
f x = x Debug.log 1
2017-01-19 23:02:21 +03:00
g n = n + 1
"""
update : Msg -> String -> String
update action model =
case action of
Replace m ->
m
errorToString : RuleError -> String
errorToString { rule, message, range } =
rule ++ ": " ++ message ++ " (line " ++ String.fromInt range.start.row ++ ", column " ++ String.fromInt range.start.column ++ ")"
lint : String -> Html Msg
lint source =
2017-01-07 22:58:41 +03:00
let
lintResult : Result (List String) (List ( Severity, RuleError ))
2017-06-12 21:04:02 +03:00
lintResult =
2017-06-17 12:56:34 +03:00
lintSource config source
2018-11-05 21:30:47 +03:00
messages : List String
messages =
2017-06-12 21:04:02 +03:00
case lintResult of
Err errors ->
errors
Ok errors ->
if List.isEmpty errors then
[ "No errors." ]
2018-11-05 20:59:42 +03:00
2017-06-12 21:04:02 +03:00
else
List.map (Tuple.second >> errorToString) errors
2017-01-07 22:58:41 +03:00
in
2018-11-05 20:59:42 +03:00
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"
2018-11-05 21:30:47 +03:00
, onInput Replace
2018-11-06 15:08:20 +03:00
, style "height" "500px"
, style "width" "500px"
2017-01-16 00:57:03 +03:00
]
[ text model ]
, div []
[ p [ class "title" ] [ text "Linting errors" ]
2018-11-05 21:30:47 +03:00
, ul [ id "lint" ]
[ li [] [ lint model ]
2017-01-16 00:57:03 +03:00
]
]
]
]
2018-11-05 21:30:47 +03:00
main : Program () String Msg
main =
2018-11-05 21:06:03 +03:00
Browser.sandbox
2018-11-05 21:30:47 +03:00
{ init = init
, update = update
, view = view
}