elm-review/example/Main.elm

161 lines
4.0 KiB
Elm
Raw Normal View History

module Main exposing (main)
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)
import Lint exposing (LintError, Severity(..), lintSource)
2019-06-24 23:55:16 +03:00
import Lint.Rule exposing (Rule)
import Lint.Rule.DefaultPatternPosition as DefaultPatternPosition
2018-11-11 01:43:58 +03:00
import Lint.Rule.NoDebug
2019-06-16 16:31:40 +03:00
import Lint.Rule.NoExtraBooleanComparison
2019-06-03 01:30:24 +03:00
import Lint.Rule.NoImportingEverything
2018-11-22 21:19:19 +03:00
import Lint.Rule.NoUnusedVariables
2019-06-24 02:20:10 +03:00
-- LINT CONFIGURATION
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" ] } )
, ( Critical, DefaultPatternPosition.rule DefaultPatternPosition.ShouldBeLast )
2019-06-16 16:31:40 +03:00
, ( Critical, Lint.Rule.NoExtraBooleanComparison.rule )
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
]
2019-06-24 02:20:10 +03:00
-- MODEL
type alias Model =
{ sourceCode : String
, lintResult : Result (List String) (List ( Severity, LintError ))
2019-06-24 02:20:10 +03:00
}
2019-06-24 02:00:22 +03:00
init : Model
init =
2019-06-24 02:00:22 +03:00
let
sourceCode : String
sourceCode =
"""module Main exposing (f)
2017-01-19 23:02:21 +03:00
2019-06-03 01:30:24 +03:00
import Html.Events exposing (..)
2017-01-20 00:27:11 +03:00
import Html exposing (..)
2019-06-24 02:00:22 +03:00
import NotUsed
import SomeModule exposing (notUsed)
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
"""
2019-06-24 02:00:22 +03:00
in
{ sourceCode = sourceCode
, lintResult = lintSource config sourceCode
}
2019-06-24 02:20:10 +03:00
-- UPDATE
type Msg
= UserEditedSourceCode String
2019-06-24 02:00:22 +03:00
update : Msg -> Model -> Model
update action model =
case action of
2019-06-24 02:11:50 +03:00
UserEditedSourceCode sourceCode ->
2019-06-24 02:00:22 +03:00
{ model
| sourceCode = sourceCode
, lintResult = lintSource config sourceCode
}
2019-06-24 02:20:10 +03:00
-- VIEW
2017-01-07 22:58:41 +03:00
2019-06-24 02:00:22 +03:00
view : Model -> Html Msg
view model =
div [ id "wrapper" ]
2017-01-16 00:57:03 +03:00
[ div [ id "left" ]
[ p [ class "title" ] [ text "Source code" ]
2019-06-24 02:11:18 +03:00
, div
[ style "display" "flex"
, style "flex-direction" "row"
2017-01-16 00:57:03 +03:00
]
2019-06-24 02:11:18 +03:00
[ textarea
[ id "input"
2019-06-24 02:11:50 +03:00
, onInput UserEditedSourceCode
2019-06-24 02:11:18 +03:00
, style "height" "500px"
, style "width" "60%"
]
[ text model.sourceCode ]
, div [ style "margin-left" "2rem" ]
[ p [ class "title" ] [ text "Linting errors" ]
, ul [ id "lint" ]
(lintErrors model)
]
2017-01-16 00:57:03 +03:00
]
]
]
2019-06-24 02:20:10 +03:00
lintErrors : Model -> List (Html Msg)
lintErrors model =
let
messages : List String
messages =
case model.lintResult of
Err errors ->
errors
Ok errors ->
if List.isEmpty errors then
[ "No errors." ]
else
List.map (Tuple.second >> errorToString) errors
in
List.map
(\message -> li [] [ text message ])
messages
errorToString : LintError -> String
errorToString { ruleName, message, range } =
2019-06-24 02:20:10 +03:00
let
location : String
location =
"(line " ++ String.fromInt range.start.row ++ ", column " ++ String.fromInt range.start.column ++ ")"
in
ruleName ++ ": " ++ message ++ " " ++ location
2019-06-24 02:20:10 +03:00
2019-06-24 02:00:22 +03:00
main : Program () Model 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
}