From 4a72b9b0ef4b9d24a76f1a9f6b42aeddd0dbb506 Mon Sep 17 00:00:00 2001 From: Mateus Date: Fri, 14 Jun 2024 09:47:11 -0300 Subject: [PATCH 1/7] in FunctionTest: add types and change name of describe --- tests/Install/FunctionTest.elm | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/Install/FunctionTest.elm b/tests/Install/FunctionTest.elm index d6866a5..12613e1 100644 --- a/tests/Install/FunctionTest.elm +++ b/tests/Install/FunctionTest.elm @@ -1,13 +1,14 @@ module Install.FunctionTest exposing (all) import Install.Function +import Review.Rule exposing (Rule) import Run import Test exposing (Test, describe) all : Test all = - describe "Install.ClauseInCase" + describe "Install.Function" [ Run.testFix test1 ] @@ -16,6 +17,7 @@ all = -- TEST 1 +test1 : { description : String, src : String, rule : Rule, under : String, fixed : String, message : String } test1 = { description = "Test 1, replace function body of of Frontend.view" , src = src1 @@ -26,6 +28,7 @@ test1 = } +rule1 : Rule rule1 = Install.Function.init [ "Frontend" ] @@ -35,6 +38,7 @@ rule1 = |> Install.Function.makeRule +src1 : String src1 = """module Frontend exposing(..) @@ -42,11 +46,13 @@ view model = Html.div [] [ Html.text "Hello, World!" ]""" +under1 : String under1 = """view model = Html.div [] [ Html.text "Hello, World!" ]""" +fixed1 : String fixed1 = """module Frontend exposing(..) From c9ef28c2683ca1b8bb008d1ea4a74f4794875254 Mon Sep 17 00:00:00 2001 From: Mateus Date: Fri, 14 Jun 2024 09:47:50 -0300 Subject: [PATCH 2/7] in ImportTest: add tests to simple imports with and without other imports --- tests/Install/ImportTest.elm | 94 ++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 tests/Install/ImportTest.elm diff --git a/tests/Install/ImportTest.elm b/tests/Install/ImportTest.elm new file mode 100644 index 0000000..e09f6e3 --- /dev/null +++ b/tests/Install/ImportTest.elm @@ -0,0 +1,94 @@ +module Install.ImportTest exposing (..) + +import Install.Import +import Review.Rule exposing (Rule) +import Run +import Test exposing (Test, describe) + + +all : Test +all = + describe "Install.Import" + [ Run.testFix test1 + , Run.testFix test1a + + -- |> Run.withOnly + ] + + + +-- test 1 - add simple import + + +test1 : { description : String, src : String, rule : Rule, under : String, fixed : String, message : String } +test1 = + { description = "add simple import" + , src = src1 + , rule = rule1 + , under = under1 + , fixed = fixed1 + , message = "moduleToImport: \"Dict\"" + } + + +rule1 : Rule +rule1 = + Install.Import.init "Main" "Dict" + |> Install.Import.makeRule + + +src1 : String +src1 = + """module Main exposing (..) + +import Set + +foo = 1""" + + +under1 : String +under1 = + """import Set""" + + +fixed1 : String +fixed1 = + """module Main exposing (..) + +import Set +import Dict +foo = 1""" + + + +-- test 1a - add simple import when there are no imports + + +test1a : { description : String, src : String, rule : Rule, under : String, fixed : String, message : String } +test1a = + { description = "add simple import when there are no imports" + , src = src1a + , rule = rule1 + , under = under1a + , fixed = fixed1a + , message = "moduleToImport: \"Dict\"" + } + + +src1a : String +src1a = + """module Main exposing (..) + +foo = 1""" + + +under1a : String +under1a = + "module Main exposing (..)" + + +fixed1a : String +fixed1a = + """module Main exposing (..) +import Dict +foo = 1""" From e8832682031f27b521380fa739d43c64b1a7ddfe Mon Sep 17 00:00:00 2001 From: Mateus Date: Fri, 14 Jun 2024 09:49:50 -0300 Subject: [PATCH 3/7] fix: set module definition as default range to allow imports to bhe installed even when there are no other imports --- src/Install/Import.elm | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Install/Import.elm b/src/Install/Import.elm index 297d8a4..dd791e9 100644 --- a/src/Install/Import.elm +++ b/src/Install/Import.elm @@ -18,6 +18,7 @@ To add the statement `import Foo.Bar as FB exposing (a, b, c)` to the `Frontend` -} import Elm.Syntax.Import exposing (Import) +import Elm.Syntax.Module exposing (Module) import Elm.Syntax.ModuleName exposing (ModuleName) import Elm.Syntax.Node as Node exposing (Node) import Elm.Syntax.Range as Range exposing (Range) @@ -78,6 +79,7 @@ makeRule : Config -> Rule makeRule config = Rule.newModuleRuleSchemaUsingContextCreator "Install.Import" initialContext |> Rule.withImportVisitor (importVisitor config) + |> Rule.withModuleDefinitionVisitor moduleDefinitionVisitor |> Rule.withFinalModuleEvaluation (finalEvaluation config) |> Rule.providesFixesForModuleRule |> Rule.fromModuleRuleSchema @@ -108,6 +110,12 @@ importVisitor config node context = ( [], { context | lastNodeRange = Node.range node } ) +moduleDefinitionVisitor : Node Module -> Context -> ( List (Error {}), Context ) +moduleDefinitionVisitor def context = + -- visit the module definition to set the module definition as the lastNodeRange in case the module has not imports yet + ( [], { context | lastNodeRange = Node.range def } ) + + finalEvaluation : Config -> Context -> List (Rule.Error {}) finalEvaluation config context = if context.moduleWasImported == False && config.hostModuleName == context.moduleName then From 128eea19404026e7184549eefdf0b76414bb94f7 Mon Sep 17 00:00:00 2001 From: Mateus Date: Fri, 14 Jun 2024 09:50:18 -0300 Subject: [PATCH 4/7] small enhancements to prevent unnecessary whitespace and keyword use --- src/Install/Import.elm | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Install/Import.elm b/src/Install/Import.elm index dd791e9..b5d953f 100644 --- a/src/Install/Import.elm +++ b/src/Install/Import.elm @@ -24,6 +24,7 @@ import Elm.Syntax.Node as Node exposing (Node) import Elm.Syntax.Range as Range exposing (Range) import Review.Fix as Fix import Review.Rule as Rule exposing (Error, Rule) +import String.Extra {-| @@ -134,6 +135,7 @@ fixError config context = ++ " " |> addAlias config.importedModuleAlias |> addExposing config.exposedValues + |> String.Extra.clean addAlias : Maybe String -> String -> String addAlias mAlias str = @@ -141,8 +143,8 @@ fixError config context = Nothing -> str - Just alias -> - str ++ " as " ++ alias + Just alias_ -> + str ++ " as " ++ alias_ addExposing : Maybe (List String) -> String -> String addExposing mExposedValues str = From e774e0d33b1a9a413d447ec356c2171d8377f3d9 Mon Sep 17 00:00:00 2001 From: Mateus Date: Fri, 14 Jun 2024 10:13:59 -0300 Subject: [PATCH 5/7] add more tests --- tests/Install/ImportTest.elm | 65 ++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/tests/Install/ImportTest.elm b/tests/Install/ImportTest.elm index e09f6e3..66a1b0b 100644 --- a/tests/Install/ImportTest.elm +++ b/tests/Install/ImportTest.elm @@ -11,8 +11,7 @@ all = describe "Install.Import" [ Run.testFix test1 , Run.testFix test1a - - -- |> Run.withOnly + , Run.testFix test2 ] @@ -92,3 +91,65 @@ fixed1a = """module Main exposing (..) import Dict foo = 1""" + + + +-- test 2 - add import with alias + + +test2 : { description : String, src : String, rule : Rule, under : String, fixed : String, message : String } +test2 = + { description = "add import with alias" + , src = src1 + , rule = rule2 + , under = under1 + , fixed = fixed2 + , message = "moduleToImport: \"Dict\"" + } + + +rule2 : Rule +rule2 = + Install.Import.init "Main" "Dict" + |> Install.Import.withAlias "D" + |> Install.Import.makeRule + + +fixed2 : String +fixed2 = + """module Main exposing (..) + +import Set +import Dict as D +foo = 1""" + + + +-- test 3 - add import exposing + + +test3 : { description : String, src : String, rule : Rule, under : String, fixed : String, message : String } +test3 = + { description = "add import exposing" + , src = src1 + , rule = rule3 + , under = under1 + , fixed = fixed3 + , message = "moduleToImport: \"Dict\"" + } + + +rule3 : Rule +rule3 = + Install.Import.init "Main" "Dict" + |> Install.Import.withExposedValues [ "Dict" ] + |> Install.Import.makeRule + + +fixed3 : String +fixed3 = + """module Main exposing (..) + +import Set +import Dict exposing (Dict) +foo = 1""" From 25d6e826c468493e2d8b5bf4de9f5b097410b57b Mon Sep 17 00:00:00 2001 From: Mateus Date: Fri, 14 Jun 2024 11:38:09 -0300 Subject: [PATCH 6/7] add test to list --- tests/Install/ImportTest.elm | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Install/ImportTest.elm b/tests/Install/ImportTest.elm index 66a1b0b..2b0dd8e 100644 --- a/tests/Install/ImportTest.elm +++ b/tests/Install/ImportTest.elm @@ -12,6 +12,7 @@ all = [ Run.testFix test1 , Run.testFix test1a , Run.testFix test2 + , Run.testFix test3 ] From 7ad7b2345b993811a69b3f7f9588e352ef5f1024 Mon Sep 17 00:00:00 2001 From: Mateus Date: Fri, 14 Jun 2024 11:38:18 -0300 Subject: [PATCH 7/7] small text adjustments --- src/Install/Import.elm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Install/Import.elm b/src/Install/Import.elm index b5d953f..4dec408 100644 --- a/src/Install/Import.elm +++ b/src/Install/Import.elm @@ -62,8 +62,8 @@ init hostModuleName_ importedModuleName_ = {-| Add an alias to the imported module. -} withAlias : String -> Config -> Config -withAlias alias config = - { config | importedModuleAlias = Just alias } +withAlias alias_ config = + { config | importedModuleAlias = Just alias_ } {-| Add an exposing list to the imported module. @@ -113,7 +113,7 @@ importVisitor config node context = moduleDefinitionVisitor : Node Module -> Context -> ( List (Error {}), Context ) moduleDefinitionVisitor def context = - -- visit the module definition to set the module definition as the lastNodeRange in case the module has not imports yet + -- visit the module definition to set the module definition as the lastNodeRange in case the module has no imports yet ( [], { context | lastNodeRange = Node.range def } )