From 79beb66a2a8c63ff0e41923927857acddae819d2 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Mon, 24 Jun 2019 23:00:36 +0200 Subject: [PATCH] Make Rule type opaque --- src/Lint.elm | 6 +++--- src/Lint/Rule.elm | 54 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/Lint.elm b/src/Lint.elm index c97d7c51..3c7f689b 100644 --- a/src/Lint.elm +++ b/src/Lint.elm @@ -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 diff --git a/src/Lint/Rule.elm b/src/Lint/Rule.elm index 8e6ce0de..088f8ce3 100644 --- a/src/Lint/Rule.elm +++ b/src/Lint/Rule.elm @@ -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