From f15c3d037400cf0d52d2c2587dde9991cff70dee Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 16 Jun 2017 11:53:29 +0200 Subject: [PATCH] Add support for associating a severity to a rule --- lintingDir/src/LintApp.elm | 66 ++++++++++++++++++++++++++--------- lintingDir/src/LintConfig.elm | 32 ++++++++--------- src/Lint.elm | 42 +++++++++++----------- src/Lint/Types.elm | 19 ++++++++-- 4 files changed, 102 insertions(+), 57 deletions(-) diff --git a/lintingDir/src/LintApp.elm b/lintingDir/src/LintApp.elm index 2380181d..e01c10a7 100644 --- a/lintingDir/src/LintApp.elm +++ b/lintingDir/src/LintApp.elm @@ -1,9 +1,8 @@ port module LintApp exposing (..) import Json.Decode -import Dict exposing (Dict) import Lint exposing (lintSource) -import Lint.Types exposing (LintError) +import Lint.Types exposing (LintRule, LintError, Severity(..)) import LintConfig exposing (config) @@ -27,14 +26,24 @@ type Msg = Lint (List File) -lint : String -> List String -lint source = - case lintSource config source of - Err errors -> - errors +enabledRules : List ( Severity, LintRule ) +enabledRules = + config + |> List.filter (Tuple.first >> (/=) Disabled) - Ok errors -> - errors + +lint : String -> List ( Severity, LintError ) +lint source = + lintSource enabledRules source + |> (\result -> + case result of + Err errors -> + [ ( Critical, { rule = "ParseError", message = String.join "\n" errors } ) + ] + + Ok result -> + result + ) update : Msg -> Model -> ( Model, Cmd Msg ) @@ -43,8 +52,10 @@ update msg model = Lint files -> let lintResult = - List.map (\file -> ( file, lint file.source )) files - |> List.filter (Tuple.second >> List.isEmpty >> not) + files + |> List.map (\file -> ( file, lint file.source )) + |> List.filter + (Tuple.second >> List.isEmpty >> not) |> List.map formatReport |> String.join "\n\n" in @@ -67,10 +78,31 @@ main = } -formatReport : ( File, List String ) -> String +formatSeverity : Severity -> String +formatSeverity severity = + case severity of + Disabled -> + "Disabled" + + Warning -> + "Warning" + + Critical -> + "Critical" + + +formatReport : ( File, List ( Severity, LintError ) ) -> String formatReport ( { filename }, errors ) = - (toString (List.length errors)) - ++ " errors found in '" - ++ filename - ++ "':\n\n\t" - ++ (String.join "\n\t" errors) + let + formattedErrors = + List.map + (\( severity, { rule, message } ) -> + "(" ++ (formatSeverity severity) ++ ") " ++ rule ++ ": " ++ message + ) + errors + in + (toString (List.length errors)) + ++ " errors found in '" + ++ filename + ++ "':\n\n\t" + ++ (String.join "\n\t" formattedErrors) diff --git a/lintingDir/src/LintConfig.elm b/lintingDir/src/LintConfig.elm index c1b8c93e..989c081d 100644 --- a/lintingDir/src/LintConfig.elm +++ b/lintingDir/src/LintConfig.elm @@ -1,6 +1,6 @@ module LintConfig exposing (config) -import Lint.Types exposing (LintRule) +import Lint.Types exposing (LintRule, Severity(..)) import Lint.Rules.DefaultPatternPosition import Lint.Rules.NoConstantCondition import Lint.Rules.NoDebug @@ -17,20 +17,20 @@ import Lint.Rules.SimplifyPiping import Lint.Rules.SimplifyPropertyAccess -config : List LintRule +config : List ( Severity, LintRule ) config = - [ Lint.Rules.DefaultPatternPosition.rule { position = Lint.Rules.DefaultPatternPosition.Last } - , Lint.Rules.NoConstantCondition.rule - , Lint.Rules.NoDebug.rule - , Lint.Rules.NoDuplicateImports.rule - , Lint.Rules.NoExposingEverything.rule - , Lint.Rules.NoImportingEverything.rule - , Lint.Rules.NoNestedLet.rule - , Lint.Rules.NoUnannotatedFunction.rule - , Lint.Rules.NoUnusedVariables.rule - , Lint.Rules.NoUselessIf.rule - , Lint.Rules.NoUselessPatternMatching.rule - , Lint.Rules.NoWarningComments.rule - , Lint.Rules.SimplifyPiping.rule - , Lint.Rules.SimplifyPropertyAccess.rule + [ ( Critical, Lint.Rules.DefaultPatternPosition.rule { position = Lint.Rules.DefaultPatternPosition.Last } ) + , ( Critical, Lint.Rules.NoConstantCondition.rule ) + , ( Critical, Lint.Rules.NoDebug.rule ) + , ( Critical, Lint.Rules.NoDuplicateImports.rule ) + , ( Critical, Lint.Rules.NoExposingEverything.rule ) + , ( Critical, Lint.Rules.NoImportingEverything.rule ) + , ( Critical, Lint.Rules.NoNestedLet.rule ) + , ( Critical, Lint.Rules.NoUnannotatedFunction.rule ) + , ( Critical, Lint.Rules.NoUnusedVariables.rule ) + , ( Critical, Lint.Rules.NoUselessIf.rule ) + , ( Critical, Lint.Rules.NoUselessPatternMatching.rule ) + , ( Warning, Lint.Rules.NoWarningComments.rule ) + , ( Critical, Lint.Rules.SimplifyPiping.rule ) + , ( Critical, Lint.Rules.SimplifyPropertyAccess.rule ) ] diff --git a/src/Lint.elm b/src/Lint.elm index 1f60288f..8680950c 100644 --- a/src/Lint.elm +++ b/src/Lint.elm @@ -34,7 +34,7 @@ import Ast import Ast.Expression exposing (Expression) import Ast.Statement import Combine -import Lint.Types exposing (LintRule, LintResult, LintError, LintImplementation, LintRuleImplementation, Direction, Visitor) +import Lint.Types exposing (LintRule, LintResult, LintError, LintImplementation, LintRuleImplementation, Direction, Visitor, Severity, Severity(..)) import Lint.Visitor exposing (transformStatementsIntoVisitors, expressionToVisitors) import Regex @@ -44,22 +44,27 @@ import Regex errors = lintSource rules source -} -lintSource : List LintRule -> String -> Result (List String) (List String) +lintSource : List ( Severity, LintRule ) -> String -> Result (List String) (List ( Severity, LintError )) lintSource rules source = - let - sourceParsingResult = - parseSource source - in - case sourceParsingResult of - Err err -> - Err err - - Ok parsedSource -> + source + |> parseSource + |> Result.map + (\_ -> rules |> List.concatMap - (\rule -> rule source |> Result.withDefault []) - |> List.map (\err -> err.rule ++ ": " ++ err.message) - |> Ok + (lintSourceWithRule source) + ) + + + +-- type alias Reporter a = -> a + + +lintSourceWithRule : String -> ( Severity, LintRule ) -> List ( Severity, LintError ) +lintSourceWithRule source ( severity, rule ) = + rule source + |> Result.map (List.map ((,) severity)) + |> Result.withDefault [] parseSource : String -> Result (List String) (Combine.ParseOk () (List Ast.Statement.Statement)) @@ -67,14 +72,7 @@ parseSource source = source |> removeComments |> Ast.parse - |> (\sourceParsingResult -> - case sourceParsingResult of - Err ( _, _, errors ) -> - Err errors - - Ok parsedSource -> - Ok parsedSource - ) + |> Result.mapError (\( _, _, errors ) -> errors) removeComments : String -> String diff --git a/src/Lint/Types.elm b/src/Lint/Types.elm index e72bb33a..37749e4c 100644 --- a/src/Lint/Types.elm +++ b/src/Lint/Types.elm @@ -1,12 +1,15 @@ -module Lint.Types exposing (LintError, LintImplementation, Direction(..), LintRule, LintRuleImplementation, LintResult, Visitor) +module Lint.Types exposing (LintError, LintImplementation, Direction(..), LintRule, LintRuleImplementation, LintResult, Visitor, Severity, Severity(..)) {-| This module contains types that are used for writing rules. # Elementary types @docs LintError, Direction +# Configuration +@docs LintRule, Severity + # Writing rules -@docs LintRuleImplementation, LintImplementation, LintRule +@docs LintRuleImplementation, LintImplementation # Internal types @docs Visitor, LintResult @@ -127,3 +130,15 @@ Note: this is internal API, and will be removed in a future version. -} type alias Visitor context = LintRuleImplementation context -> context -> ( List LintError, context ) + + +{-| Severity associated to a rule. + +- Critical: Transgressions reported by the rule will make the linting process fail. +- Warning: Transgressions reported by the rule will not make the linting process fail. +- Disabled: Rule will not be enforced. +-} +type Severity + = Disabled + | Warning + | Critical