Allow the configuration of the rules in the example

This commit is contained in:
Jeroen Engels 2019-07-03 14:45:05 +02:00
parent 56b4ad084e
commit a67ff9f6b8

View File

@ -2,11 +2,11 @@ module Main exposing (main)
import Browser import Browser
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (class, id, style) import Html.Attributes as Attr
import Html.Events exposing (onInput) import Html.Events as Events
import Lint exposing (LintError, Severity(..), lintSource) import Lint exposing (LintError, Severity(..), lintSource)
import Lint.Rule exposing (Rule) import Lint.Rule exposing (Rule)
import Lint.Rule.DefaultPatternPosition as DefaultPatternPosition import Lint.Rule.DefaultPatternPosition as DefaultPatternPosition exposing (PatternPosition)
import Lint.Rule.NoDebug import Lint.Rule.NoDebug
import Lint.Rule.NoExtraBooleanComparison import Lint.Rule.NoExtraBooleanComparison
import Lint.Rule.NoImportingEverything import Lint.Rule.NoImportingEverything
@ -17,13 +17,13 @@ import Lint.Rule.NoUnusedVariables
-- LINT CONFIGURATION -- LINT CONFIGURATION
config : List ( Severity, Rule ) config : Model -> List ( Severity, Rule )
config = config model =
[ ( Critical, Lint.Rule.NoDebug.rule ) [ ( model.noDebugEnabled, ( Critical, Lint.Rule.NoDebug.rule ) )
, ( Critical, Lint.Rule.NoUnusedVariables.rule ) , ( model.noUnusedVariablesEnabled, ( Critical, Lint.Rule.NoUnusedVariables.rule ) )
, ( Critical, Lint.Rule.NoImportingEverything.rule { exceptions = [ "Html" ] } ) , ( model.noImportingEverythingEnabled, ( Critical, Lint.Rule.NoImportingEverything.rule { exceptions = [ "Html" ] } ) )
, ( Critical, DefaultPatternPosition.rule DefaultPatternPosition.ShouldBeLast ) , ( model.defaultPatternPositionEnabled, ( Critical, DefaultPatternPosition.rule model.defaultPatternPositionPattern ) )
, ( Critical, Lint.Rule.NoExtraBooleanComparison.rule ) , ( model.noExtraBooleanComparisonEnabled, ( Critical, Lint.Rule.NoExtraBooleanComparison.rule ) )
-- , ( Critical, Lint.Rule.NoConstantCondition.rule ) -- , ( Critical, Lint.Rule.NoConstantCondition.rule )
-- , ( Critical, Lint.Rule.NoDuplicateImports.rule ) -- , ( Critical, Lint.Rule.NoDuplicateImports.rule )
@ -37,6 +37,14 @@ config =
-- , ( Critical, Lint.Rule.SimplifyPropertyAccess.rule ) -- , ( Critical, Lint.Rule.SimplifyPropertyAccess.rule )
-- , ( Critical, Lint.Rule.ElmTest.NoDuplicateTestBodies.rule ) -- , ( Critical, Lint.Rule.ElmTest.NoDuplicateTestBodies.rule )
] ]
|> List.filterMap
(\( bool, ruleAndConfig ) ->
if bool then
Just ruleAndConfig
else
Nothing
)
@ -46,6 +54,12 @@ config =
type alias Model = type alias Model =
{ sourceCode : String { sourceCode : String
, lintResult : Result (List String) (List ( Severity, LintError )) , lintResult : Result (List String) (List ( Severity, LintError ))
, noDebugEnabled : Bool
, noUnusedVariablesEnabled : Bool
, noImportingEverythingEnabled : Bool
, defaultPatternPositionEnabled : Bool
, defaultPatternPositionPattern : PatternPosition
, noExtraBooleanComparisonEnabled : Bool
} }
@ -66,10 +80,20 @@ f x = x Debug.log 1
g n = n + 1 g n = n + 1
""" """
tmpModel : Model
tmpModel =
{ sourceCode = sourceCode
, lintResult = Result.Ok []
, noDebugEnabled = True
, noUnusedVariablesEnabled = True
, noImportingEverythingEnabled = True
, defaultPatternPositionEnabled = True
, defaultPatternPositionPattern = DefaultPatternPosition.ShouldBeLast
, noExtraBooleanComparisonEnabled = True
}
in in
{ sourceCode = sourceCode { tmpModel | lintResult = lintSource (config tmpModel) sourceCode }
, lintResult = lintSource config sourceCode
}
@ -78,6 +102,12 @@ g n = n + 1
type Msg type Msg
= UserEditedSourceCode String = UserEditedSourceCode String
| UserToggledNoDebugRule
| UserToggledNoUnusedVariablesRule
| UserToggledNoImportingEverythingRule
| UserToggledDefaultPatternPositionRule
| UserToggledNoExtraBooleanComparisonRule
| UserChangedDefaultPatternSetting PatternPosition
update : Msg -> Model -> Model update : Msg -> Model -> Model
@ -86,9 +116,38 @@ update action model =
UserEditedSourceCode sourceCode -> UserEditedSourceCode sourceCode ->
{ model { model
| sourceCode = sourceCode | sourceCode = sourceCode
, lintResult = lintSource config sourceCode , lintResult = lintSource (config model) sourceCode
} }
UserToggledNoDebugRule ->
{ model | noDebugEnabled = not model.noDebugEnabled }
|> rerunLinting
UserToggledNoUnusedVariablesRule ->
{ model | noUnusedVariablesEnabled = not model.noUnusedVariablesEnabled }
|> rerunLinting
UserToggledNoImportingEverythingRule ->
{ model | noImportingEverythingEnabled = not model.noImportingEverythingEnabled }
|> rerunLinting
UserToggledDefaultPatternPositionRule ->
{ model | defaultPatternPositionEnabled = not model.defaultPatternPositionEnabled }
|> rerunLinting
UserChangedDefaultPatternSetting patternPosition ->
{ model | defaultPatternPositionPattern = patternPosition }
|> rerunLinting
UserToggledNoExtraBooleanComparisonRule ->
{ model | noExtraBooleanComparisonEnabled = not model.noExtraBooleanComparisonEnabled }
|> rerunLinting
rerunLinting : Model -> Model
rerunLinting model =
{ model | lintResult = lintSource (config model) model.sourceCode }
-- VIEW -- VIEW
@ -96,23 +155,24 @@ update action model =
view : Model -> Html Msg view : Model -> Html Msg
view model = view model =
div [ id "wrapper" ] div [ Attr.id "wrapper" ]
[ div [ id "left" ] [ div [ Attr.id "left" ]
[ p [ class "title" ] [ text "Source code" ] [ p [ Attr.class "title" ] [ text "Source code" ]
, div , div
[ style "display" "flex" [ Attr.style "display" "flex"
, style "flex-direction" "row" , Attr.style "flex-direction" "row"
] ]
[ textarea [ textarea
[ id "input" [ Attr.id "input"
, onInput UserEditedSourceCode , Events.onInput UserEditedSourceCode
, style "height" "500px" , Attr.style "height" "500px"
, style "width" "60%" , Attr.style "width" "60%"
] ]
[ text model.sourceCode ] [ text model.sourceCode ]
, div [ style "margin-left" "2rem" ] , div [ Attr.style "margin-left" "2rem" ]
[ p [ class "title" ] [ text "Linting errors" ] [ viewConfigurationPanel model
, ul [ id "lint" ] , p [ Attr.class "title" ] [ text "Linting errors" ]
, ul [ Attr.id "lint" ]
(lintErrors model) (lintErrors model)
] ]
] ]
@ -120,6 +180,68 @@ view model =
] ]
viewConfigurationPanel : Model -> Html Msg
viewConfigurationPanel model =
div []
[ p [ Attr.class "title" ] [ text "Configuration" ]
, div
[ Attr.style "display" "flex"
, Attr.style "flex-direction" "column"
]
[ viewCheckbox UserToggledNoDebugRule "NoDebug" model.noDebugEnabled
, viewCheckbox UserToggledNoUnusedVariablesRule "NoUnusedVariables" model.noUnusedVariablesEnabled
, viewCheckbox UserToggledNoImportingEverythingRule "NoImportingEverything" model.noImportingEverythingEnabled
, form [ Attr.action "" ]
[ viewCheckbox UserToggledDefaultPatternPositionRule "DefaultPatternPosition" model.defaultPatternPositionEnabled
, viewRadioButton
UserChangedDefaultPatternSetting
DefaultPatternPosition.ShouldBeLast
"Should be last"
model.defaultPatternPositionEnabled
model.defaultPatternPositionPattern
, viewRadioButton
UserChangedDefaultPatternSetting
DefaultPatternPosition.ShouldBeFirst
"Should be first"
model.defaultPatternPositionEnabled
model.defaultPatternPositionPattern
]
, viewCheckbox UserToggledNoExtraBooleanComparisonRule "NoExtraBooleanComparison" model.noExtraBooleanComparisonEnabled
]
]
viewCheckbox : Msg -> String -> Bool -> Html Msg
viewCheckbox onClick name checked =
label
[]
[ input
[ Attr.type_ "checkbox"
, Attr.checked checked
, Events.onClick onClick
]
[]
, text name
]
viewRadioButton : (PatternPosition -> Msg) -> PatternPosition -> String -> Bool -> PatternPosition -> Html Msg
viewRadioButton onClick patternPosition name enabled selectedPatternPosition =
label
[]
[ input
[ Attr.type_ "radio"
, Attr.checked (patternPosition == selectedPatternPosition)
, Events.onClick (onClick patternPosition)
, Attr.disabled <| not enabled
, Attr.name name
, Attr.value name
]
[]
, text name
]
lintErrors : Model -> List (Html Msg) lintErrors : Model -> List (Html Msg)
lintErrors model = lintErrors model =
let let