diff --git a/src/Review/Test.elm b/src/Review/Test.elm index 9780b8ea..c32b995d 100644 --- a/src/Review/Test.elm +++ b/src/Review/Test.elm @@ -911,7 +911,7 @@ getCodeAtLocationInSourceCode sourceCode = lastLine : Maybe String lastLine = Array.get (end.row - 1) lines - |> Maybe.map (String.dropRight end.column) + |> Maybe.map (String.left end.column) in [ [ firstLine ] , Array.slice start.row (end.row - 1) lines diff --git a/tests/MiscRules/NoBooleanCase.elm b/tests/MiscRules/NoBooleanCase.elm new file mode 100644 index 00000000..bc61e6f1 --- /dev/null +++ b/tests/MiscRules/NoBooleanCase.elm @@ -0,0 +1,43 @@ +module MiscRules.NoBooleanCase exposing (rule) + +import Elm.Syntax.Expression as Expression exposing (Expression) +import Elm.Syntax.Node as Node exposing (Node) +import Elm.Syntax.Pattern as Pattern +import Elm.Syntax.Range exposing (Range) +import Review.Rule as Rule exposing (Error, Rule) + + +rule : Rule +rule = + Rule.newModuleRuleSchema "NoBooleanCase" () + |> Rule.withSimpleExpressionVisitor expressionVisitor + |> Rule.fromModuleRuleSchema + + +expressionVisitor : Node Expression -> List (Error {}) +expressionVisitor (Node.Node range expression) = + case expression of + Expression.CaseExpression caseBlock -> + errorsForCaseBlock range caseBlock + + _ -> + [] + + +errorsForCaseBlock : Range -> Expression.CaseBlock -> List (Error {}) +errorsForCaseBlock ({ end } as range) { expression, cases } = + case cases of + [ ( Node.Node _ (Pattern.NamedPattern { moduleName, name } []), _ ), _ ] -> + if moduleName == [] && (name == "True" || name == "False") then + [ Rule.error + { message = "Matching boolean values in a case .. of expression" + , details = [ "It's quite silly" ] + } + range + ] + + else + [] + + _ -> + [] diff --git a/tests/MiscRules/NoBooleanCaseTest.elm b/tests/MiscRules/NoBooleanCaseTest.elm new file mode 100644 index 00000000..c23c0e09 --- /dev/null +++ b/tests/MiscRules/NoBooleanCaseTest.elm @@ -0,0 +1,31 @@ +module MiscRules.NoBooleanCaseTest exposing (all) + +import MiscRules.NoBooleanCase as NoBooleanCase +import Review.Test +import Test exposing (Test, describe, test) + + +all : Test +all = + describe "noBooleanCase" + [ test "Simple one" <| + \_ -> + """module A exposing (..) +a = + case expr of + True -> True + False -> False +""" + |> Review.Test.run NoBooleanCase.rule + |> Review.Test.expectErrors + [ Review.Test.error + { message = "Matching boolean values in a case .. of expression" + , details = + [ "It's quite silly" + ] + , under = """case expr of + True -> True + False -> False""" + } + ] + ] diff --git a/tests/NoUnused/VariablesTest.elm b/tests/NoUnused/VariablesTest.elm index a5e7f50c..9426cca8 100644 --- a/tests/NoUnused/VariablesTest.elm +++ b/tests/NoUnused/VariablesTest.elm @@ -555,7 +555,7 @@ import Foo [ Review.Test.error { message = "Imported type `C` is not used" , details = details - , under = "C\n " + , under = "C\n ," } |> Review.Test.whenFixed """module SomeModule exposing (d) import Foo