Skip to content

Commit

Permalink
Oops: forgot to commit code
Browse files Browse the repository at this point in the history
  • Loading branch information
jxxcarlson committed Jun 3, 2024
1 parent ab9efb0 commit 599255d
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 10 deletions.
84 changes: 74 additions & 10 deletions src/Install/FunctionBody.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -19,8 +16,7 @@ type Config
= Config
{ moduleName : String
, functionName : String
, functionArgs : List String
, functionBody : String
, functionImplementation : String
, customErrorMessage : CustomError
}

Expand All @@ -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 ]
61 changes: 61 additions & 0 deletions tests/Install/FunctionBodyTest.elm
Original file line number Diff line number Diff line change
@@ -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\""""

0 comments on commit 599255d

Please sign in to comment.