mirror of
https://github.com/jfmengels/elm-review.git
synced 2024-12-27 03:33:00 +03:00
Add support for associating a severity to a rule
This commit is contained in:
parent
baa5effdb3
commit
f15c3d0374
@ -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)
|
||||
|
@ -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 )
|
||||
]
|
||||
|
42
src/Lint.elm
42
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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user