2019-06-03 01:30:24 +03:00
|
|
|
module Lint.Rule.NoImportingEverything exposing (rule, Configuration)
|
|
|
|
|
|
|
|
{-|
|
|
|
|
|
|
|
|
@docs rule, Configuration
|
|
|
|
|
|
|
|
|
|
|
|
# Fail
|
|
|
|
|
|
|
|
import Html exposing (..)
|
|
|
|
|
|
|
|
|
|
|
|
# Success
|
|
|
|
|
|
|
|
import Html exposing (div, p, textarea)
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
import Elm.Syntax.Exposing as Exposing
|
|
|
|
import Elm.Syntax.Import exposing (Import)
|
|
|
|
import Elm.Syntax.Node as Node exposing (Node)
|
|
|
|
import Elm.Syntax.Range exposing (Range)
|
|
|
|
import Lint exposing (Rule, lint)
|
2019-06-15 22:39:06 +03:00
|
|
|
import Lint.Error as Error exposing (Error)
|
2019-06-15 15:47:10 +03:00
|
|
|
import Lint.Rule as Rule exposing (Implementation)
|
2019-06-03 01:30:24 +03:00
|
|
|
import Lint.Util as Util
|
|
|
|
|
|
|
|
|
|
|
|
{-| Configuration for the rule.
|
|
|
|
-}
|
|
|
|
type alias Configuration =
|
|
|
|
{ exceptions : List String }
|
|
|
|
|
|
|
|
|
|
|
|
{-| Forbid importing everything from your module. This can especially be confusing to newcomers when the exposed
|
|
|
|
functions and types are unknown to them.
|
|
|
|
|
|
|
|
rules =
|
|
|
|
[ NoImportingEverything.rule { exceptions = [ "Html" ] }
|
|
|
|
]
|
|
|
|
|
|
|
|
-}
|
|
|
|
rule : Configuration -> Rule
|
2019-06-15 22:14:40 +03:00
|
|
|
rule config =
|
2019-06-15 22:52:21 +03:00
|
|
|
Lint.createRule
|
|
|
|
"NoImportingEverything"
|
|
|
|
(lint (implementation config))
|
2019-06-03 01:30:24 +03:00
|
|
|
|
|
|
|
|
2019-06-16 22:12:37 +03:00
|
|
|
implementation : Configuration -> Implementation ()
|
2019-06-03 01:30:24 +03:00
|
|
|
implementation config =
|
2019-06-16 22:12:37 +03:00
|
|
|
Rule.createSimple
|
|
|
|
|> Rule.withSimpleImportVisitor (importVisitor config)
|
2019-06-03 01:30:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
error : Range -> String -> Error
|
|
|
|
error range name =
|
2019-06-15 22:39:06 +03:00
|
|
|
Error.create ("Do not expose everything from " ++ name) range
|
2019-06-03 01:30:24 +03:00
|
|
|
|
|
|
|
|
2019-06-16 22:12:37 +03:00
|
|
|
importVisitor : Configuration -> Node Import -> List Error
|
|
|
|
importVisitor config node =
|
2019-06-03 01:30:24 +03:00
|
|
|
let
|
|
|
|
{ moduleName, exposingList } =
|
|
|
|
Node.value node
|
|
|
|
|
|
|
|
name : String
|
|
|
|
name =
|
|
|
|
Util.moduleName moduleName
|
|
|
|
in
|
|
|
|
if List.member name config.exceptions then
|
2019-06-16 22:12:37 +03:00
|
|
|
[]
|
2019-06-03 01:30:24 +03:00
|
|
|
|
|
|
|
else
|
|
|
|
case exposingList |> Maybe.map Node.value of
|
|
|
|
Just (Exposing.All range) ->
|
2019-06-16 22:12:37 +03:00
|
|
|
[ error range name ]
|
2019-06-03 01:30:24 +03:00
|
|
|
|
|
|
|
_ ->
|
2019-06-16 22:12:37 +03:00
|
|
|
[]
|