From 19ffc231fd4512ebacee00b1833e4753cde06338 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Mon, 24 Jun 2019 01:20:10 +0200 Subject: [PATCH] Reorganized example --- example/Main.elm | 82 +++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/example/Main.elm b/example/Main.elm index 4b3d827d..92bdeeb6 100644 --- a/example/Main.elm +++ b/example/Main.elm @@ -1,7 +1,5 @@ module Main exposing (main) --- Rules - import Browser import Html exposing (..) import Html.Attributes exposing (class, id, style) @@ -13,11 +11,10 @@ import Lint.Rule.NoExtraBooleanComparison import Lint.Rule.NoImportingEverything import Lint.Rule.NoUnusedVariables import Lint.RuleError exposing (RuleError) -import Result exposing (Result) -type Msg - = UserEditedSourceCode String + +-- LINT CONFIGURATION config : List ( Severity, Rule ) @@ -42,6 +39,16 @@ config = ] + +-- MODEL + + +type alias Model = + { sourceCode : String + , lintResult : Result (List String) (List ( Severity, RuleError )) + } + + init : Model init = let @@ -65,10 +72,12 @@ g n = n + 1 } -type alias Model = - { sourceCode : String - , lintResult : Result (List String) (List ( Severity, RuleError )) - } + +-- UPDATE + + +type Msg + = UserEditedSourceCode String update : Msg -> Model -> Model @@ -81,30 +90,8 @@ update action model = } -errorToString : RuleError -> String -errorToString { rule, message, range } = - rule ++ ": " ++ message ++ " (line " ++ String.fromInt range.start.row ++ ", column " ++ String.fromInt range.start.column ++ ")" - -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 +-- VIEW view : Model -> Html Msg @@ -133,6 +120,37 @@ view model = ] +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 : RuleError -> String +errorToString { rule, message, range } = + let + location : String + location = + "(line " ++ String.fromInt range.start.row ++ ", column " ++ String.fromInt range.start.column ++ ")" + in + rule ++ ": " ++ message ++ " " ++ location + + main : Program () Model Msg main = Browser.sandbox