diff --git a/src/Lint.elm b/src/Lint.elm index e0df9905..799c8d1b 100644 --- a/src/Lint.elm +++ b/src/Lint.elm @@ -1,4 +1,4 @@ -module Lint exposing (lintSource, lint, visitExpression, doNothing) +module Lint exposing (lintSource, lint, visitExpression, doNothing, countErrors) {-| A linter for Elm. @@ -28,13 +28,16 @@ To run the rules on a source code and get a list of errors: # Rule creation functions @docs lint, doNothing, visitExpression + +# Internal +@docs countErrors -} import Ast import Ast.Expression exposing (Expression) import Ast.Statement exposing (Statement) import Combine -import Lint.Types exposing (LintRule, LintError, LintImplementation, LintRuleImplementation, Direction, Visitor, Severity, Severity(..)) +import Lint.Types exposing (File, LintRule, LintError, LintImplementation, LintRuleImplementation, Direction, Visitor, Severity, Severity(..)) import Lint.Visitor exposing (transformStatementsIntoVisitors, expressionToVisitors) import Regex @@ -152,3 +155,19 @@ context. This is used to avoid a bit of boilerplate for visitor functions whose doNothing : LintImplementation a context doNothing ctx _ = ( [], ctx ) + + +{-| Count the number of errors of a given Severity in the list of errors. +-} +countErrors : Severity -> List ( File, List ( Severity, LintError ) ) -> Int +countErrors severity errors = + errors + |> List.map + (Tuple.second + >> List.filter + (Tuple.first + >> (==) severity + ) + >> List.length + ) + |> List.sum diff --git a/src/Lint/Types.elm b/src/Lint/Types.elm index d911d821..a52925e3 100644 --- a/src/Lint/Types.elm +++ b/src/Lint/Types.elm @@ -1,4 +1,17 @@ -module Lint.Types exposing (LintError, LintImplementation, Direction(..), LintRule, LintRuleImplementation, LintResult, Visitor, Severity, Severity(..)) +module Lint.Types + exposing + ( Direction(..) + , File + , LintError + , LintImplementation + , LintResult + , LintRule + , LintRuleImplementation + , Reporter + , Severity + , Severity(..) + , Visitor + ) {-| This module contains types that are used for writing rules. @@ -6,13 +19,13 @@ module Lint.Types exposing (LintError, LintImplementation, Direction(..), LintRu @docs LintError, Direction # Configuration -@docs LintRule, Severity +@docs LintRule, Severity, Reporter # Writing rules @docs LintRuleImplementation, LintImplementation # Internal types -@docs Visitor, LintResult +@docs Visitor, LintResult, File -} import Ast.Expression exposing (..) @@ -142,3 +155,17 @@ type Severity = Disabled | Warning | Critical + + +{-| Description of an Elm file. +-} +type alias File = + { filename : String + , source : String + } + + +{-| Function that summarizes the result of the linting process. +-} +type alias Reporter a = + List ( File, List ( Severity, LintError ) ) -> a