Simplify Scope by not having a setter and getter

This commit is contained in:
Jeroen Engels 2020-02-16 23:26:28 +01:00
parent 48d4a769ac
commit fbcc11d6a9
6 changed files with 38 additions and 84 deletions

View File

@ -22,9 +22,6 @@ rule : Rule
rule =
Rule.newModuleRuleSchema "NoHtmlButton" initialContext
|> Scope.addVisitors
{ set = \scope context -> { context | scope = scope }
, get = .scope
}
|> Rule.withModuleDefinitionVisitor moduleDefinitionVisitor
|> Rule.withExpressionVisitor expressionVisitor
|> Rule.fromModuleRuleSchema

View File

@ -61,9 +61,6 @@ rule =
, foldProjectContexts = foldProjectContexts
}
|> Scope.addProjectVisitors
{ set = \scope context -> { context | scope = scope }
, get = .scope
}
|> Rule.withContextFromImportedModules
|> Rule.withProjectElmJsonVisitor elmJsonVisitor
|> Rule.withFinalProjectEvaluation finalEvaluationForProject
@ -74,9 +71,6 @@ moduleVisitorSchema : Rule.ModuleRuleSchema {} ModuleContext -> Rule.ModuleRuleS
moduleVisitorSchema schema =
schema
|> Scope.addModuleVisitors
{ set = \scope context -> { context | scope = scope }
, get = .scope
}
|> Rule.withModuleDefinitionVisitor moduleDefinitionVisitor
|> Rule.withDeclarationListVisitor declarationListVisitor
|> Rule.withExpressionVisitor expressionVisitor

View File

@ -60,12 +60,6 @@ type alias Scope =
}
type alias SetterGetter context =
{ set : Context -> context -> context
, get : context -> Context
}
-- USAGE
@ -89,57 +83,51 @@ emptyScope =
addVisitors :
{ set : Context -> context -> context
, get : context -> Context
}
-> Rule.ModuleRuleSchema { anything | withModuleDependenciesVisitor : () } context
-> Rule.ModuleRuleSchema { anything | withModuleDependenciesVisitor : (), hasAtLeastOneVisitor : () } context
addVisitors setterGetter schema =
Rule.ModuleRuleSchema { anything | withModuleDependenciesVisitor : () } { context | scope : Context }
-> Rule.ModuleRuleSchema { anything | withModuleDependenciesVisitor : (), hasAtLeastOneVisitor : () } { context | scope : Context }
addVisitors schema =
schema
|> Rule.withModuleDependenciesVisitor
(mapInnerContext setterGetter dependenciesVisitor)
(mapInnerContext dependenciesVisitor)
|> Rule.withImportVisitor
(mapInnerContext setterGetter importVisitor |> pairWithNoErrors)
(mapInnerContext importVisitor |> pairWithNoErrors)
|> Rule.withDeclarationListVisitor
(mapInnerContext setterGetter declarationListVisitor |> pairWithNoErrors)
(mapInnerContext declarationListVisitor |> pairWithNoErrors)
|> Rule.withDeclarationVisitor
(\visitedElement direction outerContext ->
let
innerContext : InnerContext
innerContext =
outerContext
|> setterGetter.get
outerContext.scope
|> unbox
|> declarationVisitor visitedElement direction
in
( [], setterGetter.set (Context innerContext) outerContext )
( [], { outerContext | scope = Context innerContext } )
)
|> Rule.withExpressionVisitor
(\visitedElement direction outerContext ->
let
innerContext : InnerContext
innerContext =
outerContext
|> setterGetter.get
outerContext.scope
|> unbox
|> popScope visitedElement direction
|> expressionVisitor visitedElement direction
in
( [], setterGetter.set (Context innerContext) outerContext )
( [], { outerContext | scope = Context innerContext } )
)
mapInnerContext : SetterGetter context -> (visitedElement -> InnerContext -> InnerContext) -> visitedElement -> context -> context
mapInnerContext { set, get } visitor visitedElement outerContext =
mapInnerContext : (visitedElement -> InnerContext -> InnerContext) -> visitedElement -> { context | scope : Context } -> { context | scope : Context }
mapInnerContext visitor visitedElement outerContext =
let
innerContext : InnerContext
innerContext =
outerContext
|> get
outerContext.scope
|> unbox
|> visitor visitedElement
in
set (Context innerContext) outerContext
{ outerContext | scope = Context innerContext }
pairWithNoErrors : (visited -> context -> context) -> visited -> context -> ( List Error, context )

View File

@ -1,6 +1,6 @@
module Scope2 exposing
( ProjectContext, ModuleContext
, ProjectSetterGetter, addProjectVisitors, ModuleSetterGetter, addModuleVisitors, initProjectContext, fromProjectToModule, fromModuleToProject, foldProjectContexts
, addProjectVisitors, addModuleVisitors, initProjectContext, fromProjectToModule, fromModuleToProject, foldProjectContexts
, realFunctionOrType
)
@ -14,7 +14,7 @@ module Scope2 exposing
# Usage
@docs ProjectSetterGetter, addProjectVisitors, ModuleSetterGetter, addModuleVisitors, initProjectContext, fromProjectToModule, fromModuleToProject, foldProjectContexts
@docs addProjectVisitors, addModuleVisitors, initProjectContext, fromProjectToModule, fromModuleToProject, foldProjectContexts
# Access
@ -107,18 +107,6 @@ type alias Scope =
}
type alias ProjectSetterGetter context =
{ set : ProjectContext -> context -> context
, get : context -> ProjectContext
}
type alias ModuleSetterGetter context =
{ set : ModuleContext -> context -> context
, get : context -> ModuleContext
}
-- USAGE
@ -182,46 +170,44 @@ emptyScope =
}
addProjectVisitors : ProjectSetterGetter projectContext -> Rule.ProjectRuleSchema projectContext moduleContext -> Rule.ProjectRuleSchema projectContext moduleContext
addProjectVisitors setterGetter schema =
addProjectVisitors : Rule.ProjectRuleSchema { projectContext | scope : ProjectContext } moduleContext -> Rule.ProjectRuleSchema { projectContext | scope : ProjectContext } moduleContext
addProjectVisitors schema =
schema
|> Rule.withContextFromImportedModules
|> Rule.withProjectDependenciesVisitor (mapInnerProjectContext setterGetter dependenciesVisitor)
|> Rule.withProjectDependenciesVisitor (mapInnerProjectContext dependenciesVisitor)
addModuleVisitors : ModuleSetterGetter moduleContext -> Rule.ModuleRuleSchema anything moduleContext -> Rule.ModuleRuleSchema { anything | hasAtLeastOneVisitor : () } moduleContext
addModuleVisitors setterGetter schema =
addModuleVisitors : Rule.ModuleRuleSchema anything { moduleContext | scope : ModuleContext } -> Rule.ModuleRuleSchema { anything | hasAtLeastOneVisitor : () } { moduleContext | scope : ModuleContext }
addModuleVisitors schema =
schema
|> Rule.withModuleDefinitionVisitor
(mapInnerModuleContext setterGetter moduleDefinitionVisitor |> pairWithNoErrors)
(mapInnerModuleContext moduleDefinitionVisitor |> pairWithNoErrors)
|> Rule.withImportVisitor
(mapInnerModuleContext setterGetter importVisitor |> pairWithNoErrors)
(mapInnerModuleContext importVisitor |> pairWithNoErrors)
|> Rule.withDeclarationListVisitor
(mapInnerModuleContext setterGetter declarationListVisitor |> pairWithNoErrors)
(mapInnerModuleContext declarationListVisitor |> pairWithNoErrors)
|> Rule.withDeclarationVisitor
(\visitedElement direction outerContext ->
let
innerContext : InnerModuleContext
innerContext =
outerContext
|> setterGetter.get
outerContext.scope
|> unboxModule
|> declarationVisitor visitedElement direction
in
( [], setterGetter.set (ModuleContext innerContext) outerContext )
( [], { outerContext | scope = ModuleContext innerContext } )
)
|> Rule.withExpressionVisitor
(\visitedElement direction outerContext ->
let
innerContext : InnerModuleContext
innerContext =
outerContext
|> setterGetter.get
outerContext.scope
|> unboxModule
|> popScope visitedElement direction
|> expressionVisitor visitedElement direction
in
( [], setterGetter.set (ModuleContext innerContext) outerContext )
( [], { outerContext | scope = ModuleContext innerContext } )
)
@ -250,30 +236,28 @@ addModuleVisitors setterGetter schema =
-- Rule.newProjectRuleSchema name
mapInnerProjectContext : ProjectSetterGetter context -> (visitedElement -> InnerProjectContext -> InnerProjectContext) -> visitedElement -> context -> context
mapInnerProjectContext { set, get } visitor visitedElement outerContext =
mapInnerProjectContext : (visitedElement -> InnerProjectContext -> InnerProjectContext) -> visitedElement -> { projectContext | scope : ProjectContext } -> { projectContext | scope : ProjectContext }
mapInnerProjectContext visitor visitedElement outerContext =
let
innerContext : InnerProjectContext
innerContext =
outerContext
|> get
outerContext.scope
|> unboxProjectContext
|> visitor visitedElement
in
set (ProjectContext innerContext) outerContext
{ outerContext | scope = ProjectContext innerContext }
mapInnerModuleContext : ModuleSetterGetter context -> (visitedElement -> InnerModuleContext -> InnerModuleContext) -> visitedElement -> context -> context
mapInnerModuleContext { set, get } visitor visitedElement outerContext =
mapInnerModuleContext : (visitedElement -> InnerModuleContext -> InnerModuleContext) -> visitedElement -> { moduleContext | scope : ModuleContext } -> { moduleContext | scope : ModuleContext }
mapInnerModuleContext visitor visitedElement outerContext =
let
innerContext : InnerModuleContext
innerContext =
outerContext
|> get
outerContext.scope
|> unboxModule
|> visitor visitedElement
in
set (ModuleContext innerContext) outerContext
{ outerContext | scope = ModuleContext innerContext }
pairWithNoErrors : (visited -> context -> context) -> visited -> context -> ( List Error, context )

View File

@ -124,19 +124,13 @@ project =
|> Project.withDependency Dependencies.elmHtml
scopeGetterSetter =
{ set = \scope context -> { context | scope = scope }
, get = .scope
}
rule : Rule
rule =
Rule.newProjectRuleSchema "TestRule"
{ moduleVisitorSchema =
\schema ->
schema
|> Scope.addModuleVisitors scopeGetterSetter
|> Scope.addModuleVisitors
|> Rule.withDeclarationVisitor declarationVisitor
|> Rule.withExpressionVisitor expressionVisitor
|> Rule.withFinalModuleEvaluation finalEvaluation
@ -152,7 +146,7 @@ rule =
}
, foldProjectContexts = \a b -> { scope = Scope.foldProjectContexts a.scope b.scope }
}
|> Scope.addProjectVisitors scopeGetterSetter
|> Scope.addProjectVisitors
|> Rule.fromProjectRuleSchema

View File

@ -40,9 +40,6 @@ baseRule :
baseRule =
Rule.newModuleRuleSchema "TestRule" initialContext
|> Scope.addVisitors
{ set = \scope context -> { context | scope = scope }
, get = .scope
}
initialContext : Context