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.NoWarningComments
import Lint.Rules.SimplifyPiping import Lint.Rules.SimplifyPiping
import Lint.Rules.SimplifyPropertyAccess import Lint.Rules.SimplifyPropertyAccess
import Lint.Types import Lint.Types exposing (LintRule, Severity(..))
import Regex exposing (escape, regex) import Regex exposing (escape, regex)
import Result import Result
@ -50,22 +50,22 @@ type Msg
= Replace String = Replace String
rules : List Lint.Types.LintRule config : List ( Severity, LintRule )
rules = config =
[ Lint.Rules.DefaultPatternPosition.rule { position = Lint.Rules.DefaultPatternPosition.Last } [ ( Critical, Lint.Rules.DefaultPatternPosition.rule { position = Lint.Rules.DefaultPatternPosition.Last } )
, Lint.Rules.NoConstantCondition.rule , ( Critical, Lint.Rules.NoConstantCondition.rule )
, Lint.Rules.NoDebug.rule , ( Critical, Lint.Rules.NoDebug.rule )
, Lint.Rules.NoDuplicateImports.rule , ( Critical, Lint.Rules.NoDuplicateImports.rule )
, Lint.Rules.NoExposingEverything.rule , ( Critical, Lint.Rules.NoExposingEverything.rule )
, Lint.Rules.NoImportingEverything.rule , ( Critical, Lint.Rules.NoImportingEverything.rule )
, Lint.Rules.NoNestedLet.rule , ( Critical, Lint.Rules.NoNestedLet.rule )
, Lint.Rules.NoUnannotatedFunction.rule , ( Critical, Lint.Rules.NoUnannotatedFunction.rule )
, Lint.Rules.NoUnusedVariables.rule , ( Critical, Lint.Rules.NoUnusedVariables.rule )
, Lint.Rules.NoUselessIf.rule , ( Critical, Lint.Rules.NoUselessIf.rule )
, Lint.Rules.NoUselessPatternMatching.rule , ( Critical, Lint.Rules.NoUselessPatternMatching.rule )
, Lint.Rules.NoWarningComments.rule , ( Warning, Lint.Rules.NoWarningComments.rule )
, Lint.Rules.SimplifyPiping.rule , ( Critical, Lint.Rules.SimplifyPiping.rule )
, Lint.Rules.SimplifyPropertyAccess.rule , ( Critical, Lint.Rules.SimplifyPropertyAccess.rule )
] ]
@ -194,7 +194,7 @@ lint : String -> Html Msg
lint source = lint source =
let let
lintResult = lintResult =
lintSource rules source lintSource config source
messages = messages =
case lintResult of case lintResult of
@ -205,7 +205,7 @@ lint source =
if List.isEmpty errors then if List.isEmpty errors then
[ "No errors." ] [ "No errors." ]
else else
errors List.map (Tuple.second >> .message) errors
in in
div [] div []
(List.map (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
import Ast.Expression exposing (Expression) import Ast.Expression exposing (Expression)
import Ast.Statement import Ast.Statement exposing (Statement)
import Combine 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 Lint.Visitor exposing (transformStatementsIntoVisitors, expressionToVisitors)
import Regex import Regex
@ -49,22 +49,17 @@ lintSource rules source =
source source
|> parseSource |> parseSource
|> Result.map |> Result.map
(\_ -> (\( _, _, statements ) ->
rules rules
|> List.concatMap |> List.concatMap
(lintSourceWithRule source) (lintSourceWithRule statements)
) )
lintSourceWithRule : List Statement -> ( Severity, LintRule ) -> List ( Severity, LintError )
-- type alias Reporter a = -> a lintSourceWithRule statements ( severity, rule ) =
rule statements
|> List.map ((,) severity)
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)) parseSource : String -> Result (List String) (Combine.ParseOk () (List Ast.Statement.Statement))
@ -96,16 +91,11 @@ removeComments =
, initialContext = Context , initialContext = Context
} }
-} -}
lint : String -> LintRuleImplementation context -> LintResult lint : List Statement -> LintRuleImplementation context -> List LintError
lint source rule = lint statements rule =
source statements
|> parseSource |> transformStatementsIntoVisitors
|> Result.map |> lintWithVisitors rule
(\( _, _, statements ) ->
statements
|> transformStatementsIntoVisitors
|> lintWithVisitors rule
)
{-| Visit an expression using a sub rule implementation. The use of this function is not encouraged, but it can make {-| 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 {-| Shortcut to a lint rule
-} -}
type alias LintRule = 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 ). {-| Shorthand for a function that takes a rule's implementation, a context and returns ( List LintError, context ).