Make ReviewOptions an opaque and extensible type

This commit is contained in:
Jeroen Engels 2022-09-19 15:40:46 +02:00
parent 65c023481a
commit 4de29fb260
5 changed files with 66 additions and 17 deletions

View File

@ -11,7 +11,8 @@
"Review.Project.Dependency",
"Review.Fix",
"Review.Test",
"Review.Test.Dependencies"
"Review.Test.Dependencies",
"Review.Options"
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {

43
src/Review/Options.elm Normal file
View File

@ -0,0 +1,43 @@
module Review.Options exposing
( ReviewOptions
, defaults
, withDataExtraction
)
{-| Configure how `elm-review` runs.
You should not have to use this when writing a rule. This is only necessary if you wish to run `elm-review` in a new
process like the CLI.
@docs ReviewOptions
@docs defaults
@docs withDataExtraction
-}
import Review.Options.Internal exposing (ReviewOptionsInternal(..))
{-| Represents the different options you can use to run the review process.
-}
type alias ReviewOptions =
ReviewOptionsInternal
{-| Somewhat arbitrary default options.
- Does not enable data extraction
-}
defaults : ReviewOptions
defaults =
ReviewOptionsInternal
{ extract = False
}
{-| Enable or disable data extraction.
-}
withDataExtraction : Bool -> ReviewOptions -> ReviewOptions
withDataExtraction extract (ReviewOptionsInternal reviewOptions) =
ReviewOptionsInternal { reviewOptions | extract = extract }

View File

@ -0,0 +1,7 @@
module Review.Options.Internal exposing (ReviewOptionsInternal(..))
type ReviewOptionsInternal
= ReviewOptionsInternal
{ extract : Bool
}

View File

@ -306,6 +306,8 @@ import Review.Exceptions as Exceptions exposing (Exceptions)
import Review.Fix as Fix exposing (Fix)
import Review.ModuleNameLookupTable exposing (ModuleNameLookupTable)
import Review.ModuleNameLookupTable.Internal as ModuleNameLookupTableInternal
import Review.Options as ReviewOptions exposing (ReviewOptions)
import Review.Options.Internal exposing (ReviewOptionsInternal(..))
import Review.Project exposing (ProjectModule)
import Review.Project.Dependency exposing (Dependency)
import Review.Project.Internal exposing (Project(..))
@ -327,7 +329,7 @@ type Rule
, exceptions : Exceptions
, requestedData : RequestedData
, extractsData : Bool
, ruleImplementation : ReviewV3Options -> Exceptions -> Project -> List (Graph.NodeContext ModuleName ()) -> { errors : List (Error {}), rule : Rule, extract : Maybe Extract }
, ruleImplementation : ReviewOptions -> Exceptions -> Project -> List (Graph.NodeContext ModuleName ()) -> { errors : List (Error {}), rule : Rule, extract : Maybe Extract }
, configurationError : Maybe { message : String, details : List String }
}
@ -454,7 +456,7 @@ review rules ((Project p) as project) =
}
scopeResult =
runProjectVisitor
{ extract = True }
(ReviewOptions.withDataExtraction True ReviewOptions.defaults)
scopeRule
Nothing
Exceptions.init
@ -484,7 +486,7 @@ review rules ((Project p) as project) =
let
runRulesResult : { errors : List (Error {}), rules : List Rule, extracts : Dict String Encode.Value }
runRulesResult =
runRules { extract = False } rules projectWithLookupTable sortedModules
runRules ReviewOptions.defaults rules projectWithLookupTable sortedModules
in
( ListExtra.orderIndependentMap errorToReviewError runRulesResult.errors, runRulesResult.rules )
@ -544,7 +546,7 @@ reviewV2 rules maybeProjectData project =
let
runResult : { errors : List ReviewError, rules : List Rule, projectData : Maybe ProjectData, extracts : Dict String Encode.Value }
runResult =
runReview { extract = False } project rules maybeProjectData nodeContexts
runReview ReviewOptions.defaults project rules maybeProjectData nodeContexts
in
{ errors = runResult.errors
, rules = runResult.rules
@ -598,7 +600,7 @@ exported/imported with `elm/browser`'s debugger, and may cause a crash if you tr
to compare them or the model that holds them.
-}
reviewV3 : ReviewV3Options -> List Rule -> Maybe ProjectData -> Project -> { errors : List ReviewError, rules : List Rule, projectData : Maybe ProjectData, extracts : Dict String Encode.Value }
reviewV3 : ReviewOptions -> List Rule -> Maybe ProjectData -> Project -> { errors : List ReviewError, rules : List Rule, projectData : Maybe ProjectData, extracts : Dict String Encode.Value }
reviewV3 reviewOptions rules maybeProjectData project =
case
checkForConfigurationErrors rules
@ -617,11 +619,6 @@ reviewV3 reviewOptions rules maybeProjectData project =
}
type alias ReviewV3Options =
{ extract : Bool
}
checkForConfigurationErrors : List Rule -> Result (List ReviewError) ()
checkForConfigurationErrors rules =
let
@ -802,7 +799,7 @@ wrapInCycle string =
" \n " ++ string ++ "\n "
runReview : ReviewV3Options -> Project -> List Rule -> Maybe ProjectData -> List (Graph.NodeContext ModuleName ()) -> { errors : List ReviewError, rules : List Rule, projectData : Maybe ProjectData, extracts : Dict String Encode.Value }
runReview : ReviewOptions -> Project -> List Rule -> Maybe ProjectData -> List (Graph.NodeContext ModuleName ()) -> { errors : List ReviewError, rules : List Rule, projectData : Maybe ProjectData, extracts : Dict String Encode.Value }
runReview reviewOptions ((Project p) as project) rules maybeProjectData nodeContexts =
let
scopeResult : { projectData : Maybe ProjectData, lookupTables : Maybe (Dict ModuleName ModuleNameLookupTable) }
@ -811,7 +808,7 @@ runReview reviewOptions ((Project p) as project) rules maybeProjectData nodeCont
let
{ cache, extract } =
runProjectVisitor
{ extract = True }
(ReviewOptions.withDataExtraction True ReviewOptions.defaults)
scopeRule
(Maybe.map extractProjectData maybeProjectData)
Exceptions.init
@ -902,7 +899,7 @@ duplicateModulesGlobalError duplicate =
runRules :
ReviewV3Options
ReviewOptions
-> List Rule
-> Project
-> List (Graph.NodeContext ModuleName ())
@ -4209,14 +4206,14 @@ type alias CacheEntryFor value projectContext =
runProjectVisitor :
ReviewV3Options
ReviewOptionsInternal
-> RunnableProjectVisitor projectContext moduleContext
-> Maybe (ProjectRuleCache projectContext)
-> Exceptions
-> Project
-> List (Graph.NodeContext ModuleName ())
-> { errors : List (Error {}), rule : Rule, cache : ProjectRuleCache projectContext, extract : Maybe Extract }
runProjectVisitor reviewOptions projectVisitor maybePreviousCache exceptions project nodeContexts =
runProjectVisitor (ReviewOptionsInternal reviewOptions) projectVisitor maybePreviousCache exceptions project nodeContexts =
-- IGNORE TCO
let
( cacheWithInitialContext, hasInitialContextChanged ) =

View File

@ -133,6 +133,7 @@ import Json.Encode as Encode
import Review.Error as Error
import Review.FileParser as FileParser
import Review.Fix as Fix
import Review.Options as ReviewOptions
import Review.Project as Project exposing (Project, ProjectModule)
import Review.Rule as Rule exposing (ReviewError, Rule)
import Review.Test.Dependencies exposing (projectWithElmCore)
@ -426,7 +427,7 @@ runOnModulesWithProjectDataHelp project rule sources =
let
runResult : { errors : List ReviewError, rules : List Rule, projectData : Maybe Rule.ProjectData, extracts : Dict String Encode.Value }
runResult =
Rule.reviewV3 { extract = True } [ rule ] Nothing projectWithModules
Rule.reviewV3 (ReviewOptions.withDataExtraction True ReviewOptions.defaults) [ rule ] Nothing projectWithModules
errors : List ReviewError
errors =