mirror of
https://github.com/jfmengels/elm-review.git
synced 2024-12-25 10:41:47 +03:00
NoDebug: Forbid the import of the Debug module
This commit is contained in:
parent
d576423905
commit
2627b176df
@ -34,6 +34,7 @@ module Lint.Rule.NoDebug exposing (rule)
|
||||
-}
|
||||
|
||||
import Elm.Syntax.Expression exposing (Expression(..))
|
||||
import Elm.Syntax.Import exposing (Import)
|
||||
import Elm.Syntax.Node as Node exposing (Node)
|
||||
import Lint.Rule as Rule exposing (Error, Rule)
|
||||
|
||||
@ -48,6 +49,7 @@ import Lint.Rule as Rule exposing (Error, Rule)
|
||||
rule : Rule
|
||||
rule =
|
||||
Rule.newSchema "NoDebug"
|
||||
|> Rule.withSimpleImportVisitor importVisitor
|
||||
|> Rule.withSimpleExpressionVisitor expressionVisitor
|
||||
|> Rule.fromSchema
|
||||
|
||||
@ -57,6 +59,20 @@ error node =
|
||||
Rule.error "Forbidden use of Debug" (Node.range node)
|
||||
|
||||
|
||||
importVisitor : Node Import -> List Error
|
||||
importVisitor node =
|
||||
let
|
||||
moduleNameNode : Node (List String)
|
||||
moduleNameNode =
|
||||
node |> Node.value |> .moduleName
|
||||
in
|
||||
if Node.value moduleNameNode == [ "Debug" ] then
|
||||
[ error moduleNameNode ]
|
||||
|
||||
else
|
||||
[]
|
||||
|
||||
|
||||
expressionVisitor : Node Expression -> List Error
|
||||
expressionVisitor node =
|
||||
case Node.value node of
|
||||
|
@ -228,6 +228,31 @@ a = Foo.Debug.log 1
|
||||
b = Debug.Foo.log 1
|
||||
"""
|
||||
|> Lint.Test.expectNoErrors
|
||||
, test "should report the import of the Debug module" <|
|
||||
\() ->
|
||||
testRule "import Debug"
|
||||
|> Lint.Test.expectErrors
|
||||
[ Lint.Test.error
|
||||
{ message = message
|
||||
, under = "Debug"
|
||||
}
|
||||
]
|
||||
, test "should report the import of the Debug module (with exposing of some things)" <|
|
||||
\() ->
|
||||
testRule "import Debug exposing (log)"
|
||||
|> Lint.Test.expectErrors
|
||||
[ Lint.Test.error
|
||||
{ message = message
|
||||
, under = "Debug"
|
||||
}
|
||||
]
|
||||
, test "should not report imports of modules containing Debug but that is not Debug" <|
|
||||
\() ->
|
||||
testRule """
|
||||
import Foo.Debug
|
||||
import Debug.Foo
|
||||
"""
|
||||
|> Lint.Test.expectNoErrors
|
||||
]
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user