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 Lint.Direction exposing (Direction)
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)
@ -83,8 +83,8 @@ lintSource rules source =
lintSourceWithRule : File -> ( Severity, Rule ) -> List ( Severity, RuleError )
lintSourceWithRule file ( severity, rule ) =
rule.analyze file
|> List.map (\error -> ( severity, RuleError.fromError rule.name error ))
Rule.analyzer rule file
|> List.map (\error -> ( severity, RuleError.fromError (Rule.name rule) error ))
{-| Parse source code into a AST

View File

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