From 599255dbb2aa9a34b45ffa5c234b84a21346619d Mon Sep 17 00:00:00 2001 From: James Carlson Date: Mon, 3 Jun 2024 03:53:42 +0200 Subject: [PATCH] Oops: forgot to commit code --- src/Install/FunctionBody.elm | 84 ++++++++++++++++++++++++++---- tests/Install/FunctionBodyTest.elm | 61 ++++++++++++++++++++++ 2 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 tests/Install/FunctionBodyTest.elm diff --git a/src/Install/FunctionBody.elm b/src/Install/FunctionBody.elm index d70b110..10b6254 100644 --- a/src/Install/FunctionBody.elm +++ b/src/Install/FunctionBody.elm @@ -4,13 +4,10 @@ import Elm.Syntax.Declaration exposing (Declaration(..)) import Elm.Syntax.Expression exposing (Case, Expression(..), Function, FunctionImplementation) import Elm.Syntax.ModuleName exposing (ModuleName) import Elm.Syntax.Node as Node exposing (Node(..)) -import Elm.Syntax.Pattern exposing (Pattern(..)) import Elm.Syntax.Range exposing (Range) -import List.Extra import Review.Fix as Fix exposing (Fix) import Review.Rule as Rule exposing (Error, Rule) import Set exposing (Set) -import String.Extra {-| Configuration for makeRule: add a clause to a case expression in a specified function in a specified module. @@ -19,8 +16,7 @@ type Config = Config { moduleName : String , functionName : String - , functionArgs : List String - , functionBody : String + , functionImplementation : String , customErrorMessage : CustomError } @@ -31,12 +27,80 @@ type CustomError = CustomError { message : String, details : List String } -init : String -> String -> List String -> String -> Config -init moduleName functionName functionArgs functionBody = +init : String -> String -> String -> Config +init moduleName functionName functionImplementation = Config { moduleName = moduleName , functionName = functionName - , functionArgs = functionArgs - , functionBody = functionBody - , customErrorMessage = CustomError { message = "Replace function body for function " ++ clause, details = [ "" ] } + , functionImplementation = functionImplementation + , customErrorMessage = CustomError { message = "Replace function \"" ++ functionName ++ "\" with new code.", details = [ "" ] } } + + +type alias Ignored = + Set String + + +makeRule : Config -> Rule +makeRule (Config config) = + let + visitor : Node Declaration -> Context -> ( List (Error {}), Context ) + visitor declaration context = + declarationVisitor declaration config.moduleName config.functionName config.functionImplementation context config.customErrorMessage + in + Rule.newModuleRuleSchemaUsingContextCreator "Install.FunctionBody" contextCreator + |> Rule.withDeclarationEnterVisitor visitor + |> Rule.providesFixesForModuleRule + |> Rule.fromModuleRuleSchema + + +type alias Context = + { moduleName : ModuleName + } + + +contextCreator : Rule.ContextCreator () { moduleName : ModuleName } +contextCreator = + Rule.initContextCreator + (\moduleName () -> + { moduleName = moduleName + + -- ...other fields + } + ) + |> Rule.withModuleName + + +declarationVisitor : Node Declaration -> String -> String -> String -> Context -> CustomError -> ( List (Rule.Error {}), Context ) +declarationVisitor node moduleName functionName functionImplementation context customError = + case Node.value node of + FunctionDeclaration function -> + let + name : String + name = + Node.value (Node.value function.declaration).name + + isInCorrectModule = + moduleName == (context.moduleName |> String.join "") + in + if name == functionName && isInCorrectModule then + visitFunction (Node.range node) functionName functionImplementation context + + else + ( [], context ) + + _ -> + ( [], context ) + + +visitFunction : Range -> String -> String -> Context -> ( List (Error {}), Context ) +visitFunction range functionName functionImplemenation context = + ( [ errorWithFix_ functionName functionImplemenation range ], context ) + + +errorWithFix_ : String -> String -> Range -> Error {} +errorWithFix_ functionName functionImplemenation range = + Rule.errorWithFix + { message = "Replace function \"" ++ functionName ++ "\"", details = [ "" ] } + range + [ Fix.replaceRangeBy range functionImplemenation ] diff --git a/tests/Install/FunctionBodyTest.elm b/tests/Install/FunctionBodyTest.elm new file mode 100644 index 0000000..b3bc7aa --- /dev/null +++ b/tests/Install/FunctionBodyTest.elm @@ -0,0 +1,61 @@ +module Install.FunctionBodyTest exposing (..) + +import Install.ClauseInCase +import Install.FunctionBody +import Run +import Test exposing (Test, describe) + + +all : Test +all = + describe "Install.ClauseInCase" + [ Run.testFix test1 + ] + + + +-- TEST 1 + + +test1 = + { description = "Test 1, replace function body of of Frontend.view" + , src = src1 + , rule = rule1 + , under = under1 + , fixed = fixed1 + , message = "Replace function \"view\"" + } + + +rule1 = + Install.FunctionBody.init + "Frontend" + "view" + """view model = + Html.text "This is a test\"""" + |> Install.FunctionBody.makeRule + + +src1 = + """module Frontend exposing(..) + +view model = + Html.div [] [ Html.text "Hello, World!" ]""" + + +under1b = + """module Frontend exposing(..) + +""" + + +under1 = + """view model = + Html.div [] [ Html.text "Hello, World!" ]""" + + +fixed1 = + """module Frontend exposing(..) + +view model = + Html.text "This is a test\""""