Make Rule type opaque

This commit is contained in:
Jeroen Engels 2019-06-24 23:00:36 +02:00
parent d23ba4a0a5
commit 79beb66a2a
2 changed files with 41 additions and 19 deletions

View File

@ -46,7 +46,7 @@ import Elm.Syntax.File exposing (File)
import Elm.Syntax.Node exposing (Node) import Elm.Syntax.Node exposing (Node)
import Lint.Direction exposing (Direction) import Lint.Direction exposing (Direction)
import Lint.Error as Error exposing (Error) import Lint.Error as Error exposing (Error)
import Lint.Rule exposing (Rule) import Lint.Rule as Rule exposing (Rule)
import Lint.RuleError as RuleError exposing (RuleError) import Lint.RuleError as RuleError exposing (RuleError)
@ -83,8 +83,8 @@ lintSource rules source =
lintSourceWithRule : File -> ( Severity, Rule ) -> List ( Severity, RuleError ) lintSourceWithRule : File -> ( Severity, Rule ) -> List ( Severity, RuleError )
lintSourceWithRule file ( severity, rule ) = lintSourceWithRule file ( severity, rule ) =
rule.analyze file Rule.analyzer rule file
|> List.map (\error -> ( severity, RuleError.fromError rule.name error )) |> List.map (\error -> ( severity, RuleError.fromError (Rule.name rule) error ))
{-| Parse source code into a AST {-| Parse source code into a AST

View File

@ -3,6 +3,7 @@ module Lint.Rule exposing
, newSchema, fromSchema , newSchema, fromSchema
, withSimpleModuleDefinitionVisitor, withSimpleImportVisitor, withSimpleExpressionVisitor, withSimpleDeclarationVisitor , withSimpleModuleDefinitionVisitor, withSimpleImportVisitor, withSimpleExpressionVisitor, withSimpleDeclarationVisitor
, withInitialContext, withModuleDefinitionVisitor, withImportVisitor, withExpressionVisitor, withDeclarationVisitor, withFinalEvaluation , withInitialContext, withModuleDefinitionVisitor, withImportVisitor, withExpressionVisitor, withDeclarationVisitor, withFinalEvaluation
, name, analyzer
) )
{-| This module contains functions that are used for writing rules. {-| This module contains functions that are used for writing rules.
@ -19,6 +20,11 @@ module Lint.Rule exposing
@docs withSimpleModuleDefinitionVisitor, withSimpleImportVisitor, withSimpleExpressionVisitor, withSimpleDeclarationVisitor @docs withSimpleModuleDefinitionVisitor, withSimpleImportVisitor, withSimpleExpressionVisitor, withSimpleDeclarationVisitor
@docs withInitialContext, withModuleDefinitionVisitor, withImportVisitor, withExpressionVisitor, withDeclarationVisitor, withFinalEvaluation @docs withInitialContext, withModuleDefinitionVisitor, withImportVisitor, withExpressionVisitor, withDeclarationVisitor, withFinalEvaluation
# ACCESS
@docs name, analyzer
-} -}
import Elm.Syntax.Declaration exposing (Declaration) import Elm.Syntax.Declaration exposing (Declaration)
@ -35,10 +41,11 @@ import Lint.Internal.DeclarationVisitor as DeclarationVisitor
{-| Shortcut to a lint rule {-| Shortcut to a lint rule
-} -}
type alias Rule = type Rule
{ name : String = Rule
, analyze : File -> List Error { name : String
} , analyzer : File -> List Error
}
{-| Represents a `rule` that will be enforced. {-| Represents a `rule` that will be enforced.
@ -76,9 +83,9 @@ type Schema context
-} -}
newSchema : String -> Schema () newSchema : String -> Schema ()
newSchema name = newSchema name_ =
Schema Schema
{ name = name { name = name_
, initialContext = () , initialContext = ()
, moduleDefinitionVisitor = \node context -> ( [], context ) , moduleDefinitionVisitor = \node context -> ( [], context )
, importVisitor = \node context -> ( [], context ) , importVisitor = \node context -> ( [], context )
@ -90,16 +97,17 @@ newSchema name =
fromSchema : Schema context -> Rule fromSchema : Schema context -> Rule
fromSchema (Schema schema) = fromSchema (Schema schema) =
{ name = schema.name Rule
, analyze = { name = schema.name
\file -> , analyzer =
schema.initialContext \file ->
|> schema.moduleDefinitionVisitor file.moduleDefinition schema.initialContext
|> accumulateList schema.importVisitor file.imports |> schema.moduleDefinitionVisitor file.moduleDefinition
|> accumulateList (DeclarationVisitor.visit schema.declarationVisitor schema.expressionVisitor) file.declarations |> accumulateList schema.importVisitor file.imports
|> makeFinalEvaluation schema.finalEvaluationFn |> accumulateList (DeclarationVisitor.visit schema.declarationVisitor schema.expressionVisitor) file.declarations
|> List.reverse |> makeFinalEvaluation schema.finalEvaluationFn
} |> List.reverse
}
{-| Concatenate the errors of the previous step and of the last step. {-| Concatenate the errors of the previous step and of the last step.
@ -190,3 +198,17 @@ withDeclarationVisitor visitor (Schema schema) =
withFinalEvaluation : (context -> List Error) -> Schema context -> Schema context withFinalEvaluation : (context -> List Error) -> Schema context -> Schema context
withFinalEvaluation visitor (Schema schema) = withFinalEvaluation visitor (Schema schema) =
Schema { schema | finalEvaluationFn = visitor } Schema { schema | finalEvaluationFn = visitor }
-- ACCESS
name : Rule -> String
name (Rule rule) =
rule.name
analyzer : Rule -> (File -> List Error)
analyzer (Rule rule) =
rule.analyzer