jfmengels / elm-review

Analyzes Elm projects, to help find mistakes before your users find them.
https://package.elm-lang.org/packages/jfmengels/elm-review/latest/
Other
252 stars 13 forks source link

Add withDirectDependencies visitors #136

Closed jfmengels closed 2 years ago

jfmengels commented 2 years ago

I noticed a few problems recently around the dependencies visitor ( (bugs and/or inefficiencies, including #134) because it was including indirect dependencies.

It is possible to manually filter out indirect dependencies (that's what NoUnused.Dependencies did for instance), but that requires additional and annoying work. You need to add a visitors to the elm.json file, store derived data from that, and then in the dependencies visitor filter out the indirect data using the derived data.

In all the rules I've written so far, I was able to replace all my usages of the dependencies visitors to the new "direct" one.

While I think that looking at indirect dependencies can be useful (for type information for instance potentially), it's rarely useful (it is still useful in NoUnapprovedLicense for instance).

Maybe in a future major version I will merge the 2 visitors in some way, but I didn't want to break the behavior of the current dependencies visitors for the current major version.

Additions

---- Review.Rule ----
withDirectDependenciesModuleVisitor :
    (
    Dict.Dict String.String Review.Project.Dependency.Dependency
    -> moduleContext
    -> moduleContext
    )
    -> Review.Rule.ModuleRuleSchema
           { schemaState | canCollectProjectData : () }
           moduleContext
    -> Review.Rule.ModuleRuleSchema
           { schemaState | canCollectProjectData : () }
           moduleContext
---- Review.Rule ----
withDirectDependenciesProjectVisitor :
    (
    Dict.Dict String.String Review.Project.Dependency.Dependency
    -> projectContext
    -> ( List.List (Review.Rule.Error { useErrorForModule : () })
       , projectContext
       )
    )
    -> Review.Rule.ProjectRuleSchema
           schemaState
           projectContext
           moduleContext
    -> Review.Rule.ProjectRuleSchema
           { schemaState | hasAtLeastOneVisitor : () }
           projectContext
           moduleContext
---- Review.Project ----
directDependencies :
    Review.Project.Project
    -> Dict.Dict String.String Review.Project.Dependency.Dependency

This also adds Review.Project.directDependencies, but that's more of an implementation detail than a feature