Report a global error when an import cycle is found

This commit is contained in:
Jeroen Engels 2020-02-13 18:11:48 +01:00
parent c01be0f6fe
commit 23632062ca

View File

@ -234,7 +234,7 @@ TODO Link to "creating a module rule" and project rule instead
See [`newModuleRuleSchema`](#newModuleRuleSchema), and [`newProjectRuleSchema`](#newProjectRuleSchema) for how to create one. See [`newModuleRuleSchema`](#newModuleRuleSchema), and [`newProjectRuleSchema`](#newProjectRuleSchema) for how to create one.
-} -}
type Rule type Rule
= Rule String (Project -> ( List Error, Rule )) = Rule String (Project -> List (Graph.NodeContext ModuleName ()) -> ( List Error, Rule ))
{-| Represents a schema for a module [`Rule`](#Rule). {-| Represents a schema for a module [`Rule`](#Rule).
@ -321,9 +321,19 @@ to compare them or compare the model that holds them.
-} -}
review : List Rule -> Project -> ( List Error, List Rule ) review : List Rule -> Project -> ( List Error, List Rule )
review rules project = review rules project =
let
sortedModules : Result (Graph.Edge ()) (List (Graph.NodeContext ModuleName ()))
sortedModules =
project
|> Review.Project.moduleGraph
|> Graph.checkAcyclic
|> Result.map Graph.topologicalSort
in
case sortedModules of
Ok nodeContexts ->
let let
( ruleErrors, rulesWithCache ) = ( ruleErrors, rulesWithCache ) =
runRules rules project runRules rules project nodeContexts
in in
( List.concat ( List.concat
[ ruleErrors [ ruleErrors
@ -334,14 +344,29 @@ review rules project =
, rulesWithCache , rulesWithCache
) )
Err _ ->
( [ Error
{ filePath = "GLOBAL ERROR"
, ruleName = "Incorrect project"
, message = "Import cycle discovered"
, details =
[ "I detected an import cycle in your project. This prevents me from working correctly, and results in a error for the Elm compiler anyway. Please resolve it using the compiler's suggestions, then try running `elm-review` again."
]
, range = { start = { row = 0, column = 0 }, end = { row = 0, column = 0 } }
, fixes = Nothing
}
]
, rules
)
runRules : List Rule -> Project -> ( List Error, List Rule )
runRules rules project = runRules : List Rule -> Project -> List (Graph.NodeContext ModuleName ()) -> ( List Error, List Rule )
runRules rules project nodeContexts =
List.foldl List.foldl
(\(Rule _ fn) ( errors, previousRules ) -> (\(Rule _ fn) ( errors, previousRules ) ->
let let
( ruleErrors, ruleWithCache ) = ( ruleErrors, ruleWithCache ) =
fn project fn project nodeContexts
in in
( List.concat [ ruleErrors, errors ], ruleWithCache :: previousRules ) ( List.concat [ ruleErrors, errors ], ruleWithCache :: previousRules )
) )
@ -472,8 +497,8 @@ newModuleRuleCache =
} }
runModuleRule : ModuleRuleSchema { anything | hasAtLeastOneVisitor : () } moduleContext -> ModuleRuleCache moduleContext -> Project -> ( List Error, Rule ) runModuleRule : ModuleRuleSchema { anything | hasAtLeastOneVisitor : () } moduleContext -> ModuleRuleCache moduleContext -> Project -> List (Graph.NodeContext ModuleName ()) -> ( List Error, Rule )
runModuleRule ((ModuleRuleSchema schema) as moduleRuleSchema) previousCache project = runModuleRule ((ModuleRuleSchema schema) as moduleRuleSchema) previousCache project _ =
let let
initialContext : moduleContext initialContext : moduleContext
initialContext = initialContext =
@ -721,16 +746,14 @@ type alias ProjectRuleCache projectContext =
} }
runProjectRule : ProjectRuleSchema projectContext moduleContext -> ProjectRuleCache projectContext -> Project -> ( List Error, Rule ) runProjectRule : ProjectRuleSchema projectContext moduleContext -> ProjectRuleCache projectContext -> Project -> List (Graph.NodeContext ModuleName ()) -> ( List Error, Rule )
runProjectRule ((ProjectRuleSchema schema) as wrappedSchema) startCache project = runProjectRule ((ProjectRuleSchema schema) as wrappedSchema) startCache project nodeContexts =
let let
graph : Graph ModuleName () graph : Graph ModuleName ()
graph = graph =
project project
|> Review.Project.moduleGraph |> Review.Project.moduleGraph
in in
case Graph.checkAcyclic graph |> Result.map Graph.topologicalSort of
Ok nodeContexts ->
let let
initialContext : projectContext initialContext : projectContext
initialContext = initialContext =
@ -837,10 +860,6 @@ runProjectRule ((ProjectRuleSchema schema) as wrappedSchema) startCache project
in in
( errors, Rule schema.name (runProjectRule wrappedSchema newCache) ) ( errors, Rule schema.name (runProjectRule wrappedSchema newCache) )
Err _ ->
-- TODO return some kind of global error?
( [], Rule schema.name (runProjectRule wrappedSchema startCache) )
setRuleName : String -> Error -> Error setRuleName : String -> Error -> Error
setRuleName ruleName (Error err) = setRuleName ruleName (Error err) =