NoDebug: Forbid the import of the Debug module

This commit is contained in:
Jeroen Engels 2019-07-05 01:51:58 +02:00
parent d576423905
commit 2627b176df
2 changed files with 41 additions and 0 deletions

View File

@ -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

View File

@ -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
]