diff --git a/src/Lint/Rule/DefaultPatternPosition.elm b/src/Lint/Rule/DefaultPatternPosition.elm index f17c6291..444a0475 100644 --- a/src/Lint/Rule/DefaultPatternPosition.elm +++ b/src/Lint/Rule/DefaultPatternPosition.elm @@ -97,11 +97,26 @@ isDefaultPattern pattern = False -findDefaultPattern : List ( Node Pattern, Node Expression ) -> Maybe Int +findDefaultPattern : List ( Node Pattern, Node Expression ) -> Maybe ( Int, Node Pattern ) findDefaultPattern patterns = - patterns - |> List.map (Tuple.first >> Node.value) - |> findIndex isDefaultPattern + findWithIndex + (Node.value >> isDefaultPattern) + (List.map Tuple.first patterns) + + +findWithIndex : (a -> Bool) -> List a -> Maybe ( Int, a ) +findWithIndex isMatch list = + case list of + [] -> + Nothing + + elem :: rest -> + if isMatch elem then + Just ( 0, elem ) + + else + findWithIndex isMatch rest + |> Maybe.map (\( index, elem_ ) -> ( index + 1, elem_ )) expressionVisitor : PatternPosition -> Node Expression -> List Error @@ -112,18 +127,18 @@ expressionVisitor patternPosition node = Nothing -> [] - Just index -> + Just ( index, patternNode ) -> case patternPosition of ShouldBeFirst -> if index /= 0 then - [ error node "Expected default pattern to appear first in the list of patterns" ] + [ error patternNode "Expected default pattern to appear first in the list of patterns" ] else [] ShouldBeLast -> if index /= List.length cases - 1 then - [ error node "Expected default pattern to appear last in the list of patterns" ] + [ error patternNode "Expected default pattern to appear last in the list of patterns" ] else [] diff --git a/tests/DefaultPatternPositionTest.elm b/tests/DefaultPatternPositionTest.elm index 10955ce4..17f22e07 100644 --- a/tests/DefaultPatternPositionTest.elm +++ b/tests/DefaultPatternPositionTest.elm @@ -1,21 +1,18 @@ module DefaultPatternPositionTest exposing (all) -import Elm.Syntax.Range exposing (Location, Range) -import Lint.Rule exposing (Error, Rule) import Lint.Rule.DefaultPatternPosition exposing (PatternPosition(..), rule) -import Lint.Test exposing (LintResult) +import Lint.Test2 exposing (LintResult) import Test exposing (Test, describe, test) testRule : PatternPosition -> String -> LintResult testRule patternPosition = - Lint.Test.run (rule patternPosition) + Lint.Test2.run (rule patternPosition) -error : String -> Error -error position = - Lint.Test.errorWithoutRange - ("Expected default pattern to appear " ++ position ++ " in the list of patterns") +message : String -> String +message position = + "Expected default pattern to appear " ++ position ++ " in the list of patterns" tests : List Test @@ -29,7 +26,7 @@ a = case b of Foo -> 1 """ |> testRule ShouldBeFirst - |> Lint.Test.expectErrorsWithoutRange [] + |> Lint.Test2.expectNoErrors , test "should not report when default pattern is at the expected position (last)" <| \() -> """module A exposing(..) @@ -39,7 +36,7 @@ a = case b of _ -> 1 """ |> testRule ShouldBeLast - |> Lint.Test.expectErrorsWithoutRange [] + |> Lint.Test2.expectNoErrors , test "should not report when there is no default pattern (first)" <| \() -> """module A exposing(..) @@ -48,7 +45,7 @@ a = case b of Bar -> 1 """ |> testRule ShouldBeFirst - |> Lint.Test.expectErrorsWithoutRange [] + |> Lint.Test2.expectNoErrors , test "should not report when there is no default pattern (last)" <| \() -> """module A exposing(..) @@ -57,7 +54,7 @@ a = case b of Bar -> 1 """ |> testRule ShouldBeLast - |> Lint.Test.expectErrorsWithoutRange [] + |> Lint.Test2.expectNoErrors , test "should report an error when the default pattern is not at the expected position (first) (opposite expected position)" <| \() -> """module A exposing(..) @@ -67,7 +64,12 @@ a = case b of _ -> 1 """ |> testRule ShouldBeFirst - |> Lint.Test.expectErrorsWithoutRange [ error "first" ] + |> Lint.Test2.expectErrors + [ Lint.Test2.error + { message = message "first" + , under = "_" + } + ] , test "should report an error when the default pattern is not at the expected position (first) (somewhere in the middle)" <| \() -> """module A exposing(..) @@ -77,7 +79,12 @@ a = case b of Bar -> 1 """ |> testRule ShouldBeFirst - |> Lint.Test.expectErrorsWithoutRange [ error "first" ] + |> Lint.Test2.expectErrors + [ Lint.Test2.error + { message = message "first" + , under = "_" + } + ] , test "should report an error when the default pattern is not at the expected position (last) (opposite expected position)" <| \() -> """module A exposing(..) @@ -87,7 +94,12 @@ a = case b of Bar -> 1 """ |> testRule ShouldBeLast - |> Lint.Test.expectErrorsWithoutRange [ error "last" ] + |> Lint.Test2.expectErrors + [ Lint.Test2.error + { message = message "last" + , under = "_" + } + ] , test "should report an error when the default pattern is not at the expected position (last) (somewhere in the middle)" <| \() -> """module A exposing(..) @@ -97,7 +109,12 @@ a = case b of Bar -> 1 """ |> testRule ShouldBeLast - |> Lint.Test.expectErrorsWithoutRange [ error "last" ] + |> Lint.Test2.expectErrors + [ Lint.Test2.error + { message = message "last" + , under = "_" + } + ] ]