Skip to content

Commit

Permalink
Rework API for module Install.Import to use Config, init, withAlias, …
Browse files Browse the repository at this point in the history
…withExposedValues
  • Loading branch information
jxxcarlson committed Jun 12, 2024
1 parent a561c42 commit 0f368f2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 27 deletions.
1 change: 1 addition & 0 deletions counter/src/Frontend.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Frontend exposing (Model, app)

import Foo.Bar as FB exposing (a, b, c)
import Html exposing (Html, text)
import Html.Attributes exposing (style)
import Html.Events exposing (onClick)
Expand Down
5 changes: 4 additions & 1 deletion preview/src/ReviewConfig.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ config =
-- |> Install.ClauseInCase.makeRule
--, Install.Function.init [ "Frontend" ] "view" viewFunction |> Install.Function.makeRule
--
Install.Import.makeRule "Frontend" "Foo.Bar" "add: import module Foo."
Install.Import.init "Frontend" "Foo.Bar"
|> Install.Import.withAlias "FB"
|> Install.Import.withExposedValues ["a", "b", "c"]
|> Install.Import.makeRule
]


Expand Down
99 changes: 73 additions & 26 deletions src/Install/Import.elm
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,53 @@ import Review.Rule as Rule exposing (Error, Rule)
import Set exposing (Set)


type alias Config =
{ hostModuleName : List String
, importedModuleName : List String
, customErrorMessage : CustomError
, importedModuleAlias : Maybe String
, exposedValues : Maybe (List String)
}


{-| Custom error message to be displayed when running `elm-review --fix` or `elm-review --fix-all`
-}
type CustomError
= CustomError { message : String, details : List String }


{-| Initialize the configuration for the rule.
-}
init : String -> String -> Config
init hostModuleName_ importedModuleName_ =
{ hostModuleName = String.split "." hostModuleName_
, importedModuleName = String.split "." importedModuleName_
, customErrorMessage = CustomError { message = "Install module " ++ importedModuleName_ ++ " in " ++ importedModuleName_, details = [ "" ] }
, importedModuleAlias = Nothing
, exposedValues = Nothing
}


withAlias : String -> Config -> Config
withAlias alias config =
{ config | importedModuleAlias = Just alias }


withExposedValues : List String -> Config -> Config
withExposedValues exposedValues config =
{ config | exposedValues = Just exposedValues }


{-| Create a rule that adds an import for a given module in a given module. For example:
Install.Import.makeRule "Frontend" "Foo.Bar" "add: import module Foo."
-}
makeRule : String -> String -> String -> Rule
makeRule hostModuleName importedModuleName customErrorMessage =
let
hostModuleNameList =
String.split "." hostModuleName

importedModuleNameList =
String.split "." importedModuleName
in
makeRule : Config -> Rule
makeRule config =
Rule.newModuleRuleSchemaUsingContextCreator "Install.Import" initialContext
|> Rule.withImportVisitor (importVisitor hostModuleNameList importedModuleNameList)
|> Rule.withFinalModuleEvaluation (finalEvaluation hostModuleNameList importedModuleNameList)
|> Rule.withImportVisitor (importVisitor config)
|> Rule.withFinalModuleEvaluation (finalEvaluation config)
|> Rule.providesFixesForModuleRule
|> Rule.fromModuleRuleSchema

Expand All @@ -50,34 +74,57 @@ initialContext =
|> Rule.withModuleName


importVisitor : List String -> List String -> Node Import -> Context -> ( List (Error {}), Context )
importVisitor hostModuleNameList importedModuleNameList node context =
importVisitor : Config -> Node Import -> Context -> ( List (Error {}), Context )
importVisitor config node context =
case Node.value node |> .moduleName |> Node.value of
currentModuleName ->
if currentModuleName == importedModuleNameList && hostModuleNameList == context.moduleName then
if currentModuleName == config.importedModuleName && config.hostModuleName == context.moduleName then
( [], { context | moduleWasImported = True, lastNodeRange = Node.range node } )

else
( [], { context | lastNodeRange = Node.range node } )


finalEvaluation : List String -> List String -> Context -> List (Rule.Error {})
finalEvaluation hostModuleNameList moduleToImport context =
let
_ =
Debug.log "CONTEXT" context
in
if context.moduleWasImported == False && hostModuleNameList == context.moduleName then
fixError moduleToImport context
finalEvaluation : Config -> Context -> List (Rule.Error {})
finalEvaluation config context =
if context.moduleWasImported == False && config.hostModuleName == context.moduleName then
fixError config context

else
[]


fixError : List String -> Context -> List (Error {})
fixError moduleToImport context =
fixError : Config -> Context -> List (Error {})
fixError config context =
let
importText =
"import "
++ String.join "." config.importedModuleName
++ " "
|> addAlias config.importedModuleAlias
|> addExposing config.exposedValues
|> Debug.log "importText"

addAlias : Maybe String -> String -> String
addAlias mAlias str =
case mAlias of
Nothing ->
str

Just alias ->
str ++ " as " ++ alias

addExposing : Maybe (List String) -> String -> String
addExposing mExposedValues str =
case mExposedValues of
Nothing ->
str

Just exposedValues ->
str ++ " exposing (" ++ String.join ", " exposedValues ++ ")"
in
[ Rule.errorWithFix
{ message = "moduleToImport: \"" ++ String.join "." moduleToImport ++ "\"", details = [ "" ] }
{ message = "moduleToImport: \"" ++ String.join "." config.importedModuleName ++ "\"", details = [ "" ] }
context.lastNodeRange
[ Fix.insertAt { row = context.lastNodeRange.end.row + 1, column = context.lastNodeRange.end.column } ("import " ++ String.join "." moduleToImport) ]
[ Fix.insertAt { row = context.lastNodeRange.end.row + 1, column = context.lastNodeRange.end.column } importText ]
]

0 comments on commit 0f368f2

Please sign in to comment.