Do not reparse source for every rule

This commit is contained in:
Jeroen Engels 2017-06-17 11:56:34 +02:00
parent 7f37ee4c68
commit 9536c1cb6f
3 changed files with 33 additions and 43 deletions

View File

@ -23,7 +23,7 @@ import Lint.Rules.NoUselessPatternMatching
import Lint.Rules.NoWarningComments
import Lint.Rules.SimplifyPiping
import Lint.Rules.SimplifyPropertyAccess
import Lint.Types
import Lint.Types exposing (LintRule, Severity(..))
import Regex exposing (escape, regex)
import Result
@ -50,22 +50,22 @@ type Msg
= Replace String
rules : List Lint.Types.LintRule
rules =
[ 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
config : List ( Severity, LintRule )
config =
[ ( 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 )
]
@ -194,7 +194,7 @@ lint : String -> Html Msg
lint source =
let
lintResult =
lintSource rules source
lintSource config source
messages =
case lintResult of
@ -205,7 +205,7 @@ lint source =
if List.isEmpty errors then
[ "No errors." ]
else
errors
List.map (Tuple.second >> .message) errors
in
div []
(List.map

View File

@ -32,9 +32,9 @@ To run the rules on a source code and get a list of errors:
import Ast
import Ast.Expression exposing (Expression)
import Ast.Statement
import Ast.Statement exposing (Statement)
import Combine
import Lint.Types exposing (LintRule, LintResult, LintError, LintImplementation, LintRuleImplementation, Direction, Visitor, Severity, Severity(..))
import Lint.Types exposing (LintRule, LintError, LintImplementation, LintRuleImplementation, Direction, Visitor, Severity, Severity(..))
import Lint.Visitor exposing (transformStatementsIntoVisitors, expressionToVisitors)
import Regex
@ -49,22 +49,17 @@ lintSource rules source =
source
|> parseSource
|> Result.map
(\_ ->
(\( _, _, statements ) ->
rules
|> List.concatMap
(lintSourceWithRule source)
(lintSourceWithRule statements)
)
-- 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 []
lintSourceWithRule : List Statement -> ( Severity, LintRule ) -> List ( Severity, LintError )
lintSourceWithRule statements ( severity, rule ) =
rule statements
|> List.map ((,) severity)
parseSource : String -> Result (List String) (Combine.ParseOk () (List Ast.Statement.Statement))
@ -96,16 +91,11 @@ removeComments =
, initialContext = Context
}
-}
lint : String -> LintRuleImplementation context -> LintResult
lint source rule =
source
|> parseSource
|> Result.map
(\( _, _, statements ) ->
lint : List Statement -> LintRuleImplementation context -> List LintError
lint statements rule =
statements
|> transformStatementsIntoVisitors
|> lintWithVisitors rule
)
{-| Visit an expression using a sub rule implementation. The use of this function is not encouraged, but it can make

View File

@ -120,7 +120,7 @@ type alias LintResult =
{-| Shortcut to a lint rule
-}
type alias LintRule =
String -> LintResult
List Statement -> List LintError
{-| Shorthand for a function that takes a rule's implementation, a context and returns ( List LintError, context ).