2018-11-11 01:43:58 +03:00
|
|
|
module Lint.Rule.NoDebug exposing (rule)
|
2017-01-07 23:17:01 +03:00
|
|
|
|
2019-07-27 14:24:19 +03:00
|
|
|
{-| Forbid the use of the [`Debug`](https://package.elm-lang.org/packages/elm/core/latest/Debug) module before it goes into production or fails in the CI.
|
2017-01-30 00:31:51 +03:00
|
|
|
|
2018-11-05 16:44:22 +03:00
|
|
|
|
2019-07-23 00:38:39 +03:00
|
|
|
# Rule
|
|
|
|
|
|
|
|
@docs 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)
|
|
|
|
|
|
|
|
|
2019-07-27 14:24:19 +03:00
|
|
|
{-| Forbid the use of the [`Debug`](https://package.elm-lang.org/packages/elm/core/latest/Debug) module before it goes into production or fails in the CI.
|
2019-07-23 00:38:39 +03:00
|
|
|
|
|
|
|
config =
|
2019-07-25 15:35:58 +03:00
|
|
|
[ NoDebug.rule
|
2019-07-23 00:38:39 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-07-03 15:19:29 +03:00
|
|
|
## Fail
|
2017-01-30 00:31:51 +03:00
|
|
|
|
2018-11-05 16:44:22 +03:00
|
|
|
if Debug.log "condition" condition then
|
|
|
|
a
|
|
|
|
|
|
|
|
else
|
|
|
|
b
|
|
|
|
|
2017-01-30 00:31:51 +03:00
|
|
|
if condition then
|
2019-07-27 13:25:04 +03:00
|
|
|
Debug.todo "Nooo!"
|
2018-11-05 16:44:22 +03:00
|
|
|
|
2017-01-30 00:31:51 +03:00
|
|
|
else
|
|
|
|
value
|
|
|
|
|
2018-11-05 16:44:22 +03:00
|
|
|
|
2019-07-03 15:19:29 +03:00
|
|
|
## Success
|
2017-01-30 00:31:51 +03:00
|
|
|
|
|
|
|
if condition then
|
|
|
|
a
|
2018-11-05 16:44:22 +03:00
|
|
|
|
2017-01-30 00:31:51 +03:00
|
|
|
else
|
|
|
|
b
|
2018-11-05 16:44:22 +03:00
|
|
|
|
2019-07-27 14:24:19 +03:00
|
|
|
|
|
|
|
# When (not) to use this rule
|
|
|
|
|
|
|
|
You should use this rule if you're developing a package meant to be published,
|
|
|
|
or an application that is put into production, and wish to know about the use of
|
|
|
|
the [`Debug`](https://package.elm-lang.org/packages/elm/core/latest/Debug)
|
|
|
|
module before committing your changes.
|
|
|
|
|
|
|
|
You should not use this rule if you are developing an application that is not
|
|
|
|
put into production, and you do not care about having stray debug logs or
|
|
|
|
runtime exceptions caused by [`Debug.todo`](https://package.elm-lang.org/packages/elm/core/latest/Debug#todo),
|
|
|
|
and you do not ship to production.
|
|
|
|
|
2017-01-30 00:31:51 +03:00
|
|
|
-}
|
2018-11-11 01:37:18 +03:00
|
|
|
rule : Rule
|
2019-06-15 22:14:40 +03:00
|
|
|
rule =
|
2019-06-24 23:35:30 +03:00
|
|
|
Rule.newSchema "NoDebug"
|
2019-07-05 02:51:58 +03:00
|
|
|
|> Rule.withSimpleImportVisitor importVisitor
|
2019-06-16 22:12:37 +03:00
|
|
|
|> Rule.withSimpleExpressionVisitor expressionVisitor
|
2019-06-24 01:32:27 +03:00
|
|
|
|> Rule.fromSchema
|
2017-01-07 23:17:01 +03:00
|
|
|
|
|
|
|
|
2018-11-11 01:37:18 +03:00
|
|
|
error : Node a -> Error
|
2018-11-06 15:15:23 +03:00
|
|
|
error node =
|
2019-07-27 18:31:04 +03:00
|
|
|
Rule.error
|
|
|
|
{ message = "Remove the use of `Debug` before shipping to production"
|
|
|
|
, details = [ "The `Debug` module is useful when developing, but is not meant to be shipped to production or published in a package. I suggest removing it's use before committing and attempting to push to production." ]
|
|
|
|
}
|
|
|
|
(Node.range node)
|
2017-01-16 01:30:33 +03:00
|
|
|
|
|
|
|
|
2019-07-05 02:51:58 +03:00
|
|
|
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
|
|
|
|
[]
|
|
|
|
|
|
|
|
|
2019-06-16 22:12:37 +03:00
|
|
|
expressionVisitor : Node Expression -> List Error
|
|
|
|
expressionVisitor node =
|
2019-06-24 13:32:07 +03:00
|
|
|
case Node.value node of
|
2019-06-16 22:12:37 +03:00
|
|
|
FunctionOrValue moduleName fnName ->
|
2019-07-05 02:42:27 +03:00
|
|
|
if moduleName == [ "Debug" ] then
|
2019-06-16 22:12:37 +03:00
|
|
|
[ error node ]
|
2018-11-05 16:44:22 +03:00
|
|
|
|
2018-11-05 21:55:03 +03:00
|
|
|
else
|
2019-06-16 22:12:37 +03:00
|
|
|
[]
|
2017-01-07 23:17:01 +03:00
|
|
|
|
|
|
|
_ ->
|
2019-06-16 22:12:37 +03:00
|
|
|
[]
|