mirror of
https://github.com/jfmengels/elm-review.git
synced 2024-11-23 23:05:35 +03:00
Make project compile again
This commit is contained in:
parent
bf504a69ce
commit
e2c2c0e7e6
@ -19,8 +19,8 @@ module Lint.Rules.NoUnusedVariables exposing (rule)
|
||||
|
||||
import Ast.Expression exposing (..)
|
||||
import Ast.Statement exposing (..)
|
||||
import Lint exposing (lint, doNothing)
|
||||
import Lint.Types exposing (LintRule, Error, Direction(..))
|
||||
import Lint exposing (doNothing, lint)
|
||||
import Lint.Types exposing (Direction(..), Error, LintRule)
|
||||
import Set exposing (Set)
|
||||
|
||||
|
||||
@ -118,6 +118,16 @@ makeReport scope =
|
||||
( errors, variablesUsedButNotFromThisScope )
|
||||
|
||||
|
||||
variableName : Expression -> Maybe (List String)
|
||||
variableName expr =
|
||||
case expr of
|
||||
Variable names ->
|
||||
Just names
|
||||
|
||||
_ ->
|
||||
Nothing
|
||||
|
||||
|
||||
expressionFn : Context -> Direction Expression -> ( List Error, Context )
|
||||
expressionFn ctx node =
|
||||
case node of
|
||||
@ -133,6 +143,8 @@ expressionFn ctx node =
|
||||
let
|
||||
variables =
|
||||
List.map Tuple.first declarations
|
||||
|> List.filterMap variableName
|
||||
|> List.concat
|
||||
|> Set.fromList
|
||||
|
||||
newScope =
|
||||
@ -214,13 +226,6 @@ statementFn ctx node =
|
||||
( [], ctx )
|
||||
|
||||
|
||||
getNotUsed : Set comparable -> Set comparable -> List comparable
|
||||
getNotUsed declared used =
|
||||
Set.diff declared used
|
||||
|> Set.toList
|
||||
|> List.sort
|
||||
|
||||
|
||||
moduleEndFn : Context -> ( List Error, Context )
|
||||
moduleEndFn ctx =
|
||||
let
|
||||
|
@ -83,6 +83,12 @@ expressionToVisitors node =
|
||||
Lambda names expression ->
|
||||
[ expression ]
|
||||
|
||||
Tuple expressions ->
|
||||
expressions
|
||||
|
||||
AccessFunction name ->
|
||||
[]
|
||||
|
||||
childrenVisitors =
|
||||
List.concatMap expressionToVisitors children
|
||||
in
|
||||
|
@ -2,6 +2,7 @@ port module NoUnannotatedFunctionTest exposing (all)
|
||||
|
||||
import Expect
|
||||
import Test exposing (describe, test, Test)
|
||||
import TestUtil exposing (ruleTester)
|
||||
import Lint.Rules.NoUnannotatedFunction exposing (rule)
|
||||
import Lint.Types exposing (Error)
|
||||
|
||||
@ -11,44 +12,48 @@ error =
|
||||
Error "NoUnannotatedFunction"
|
||||
|
||||
|
||||
testRule =
|
||||
ruleTester rule
|
||||
|
||||
|
||||
tests : List Test
|
||||
tests =
|
||||
[ test "should not report constants that are annotated" <|
|
||||
\() ->
|
||||
rule """
|
||||
testRule """
|
||||
f : Int"
|
||||
f = 2
|
||||
"""
|
||||
|> Expect.equal []
|
||||
, test "should not report functions that are annotated" <|
|
||||
\() ->
|
||||
rule """
|
||||
testRule """
|
||||
f : Int -> Int"
|
||||
f n = 2
|
||||
"""
|
||||
|> Expect.equal []
|
||||
, test "should report constants that are not annotated" <|
|
||||
\() ->
|
||||
rule "f = 2"
|
||||
testRule "f = 2"
|
||||
|> Expect.equal [ error "`f` does not have a type declaration" ]
|
||||
, test "should report functions that are not annotated" <|
|
||||
\() ->
|
||||
rule "f n = 2"
|
||||
testRule "f n = 2"
|
||||
|> Expect.equal [ error "`f` does not have a type declaration" ]
|
||||
, test "should report functions that are not annotated" <|
|
||||
\() ->
|
||||
rule "f n = 2"
|
||||
testRule "f n = 2"
|
||||
|> Expect.equal [ error "`f` does not have a type declaration" ]
|
||||
, test "should report functions that are not annotated when there are annotations" <|
|
||||
\() ->
|
||||
rule """
|
||||
testRule """
|
||||
f : Int -> Int
|
||||
g n = 3
|
||||
"""
|
||||
|> Expect.equal [ error "`g` does not have a type declaration" ]
|
||||
, test "should report functions that are not annotated when there are other annotated functions" <|
|
||||
\() ->
|
||||
rule """
|
||||
testRule """
|
||||
f : Int -> Int
|
||||
f n = 2
|
||||
|
||||
@ -57,7 +62,7 @@ tests =
|
||||
|> Expect.equal [ error "`g` does not have a type declaration" ]
|
||||
, test "should not functions declared in a `let` body" <|
|
||||
\() ->
|
||||
rule """
|
||||
testRule """
|
||||
f : Int -> Int
|
||||
f n =
|
||||
let
|
||||
|
@ -2,6 +2,7 @@ port module NoUnusedVariablesTest exposing (all)
|
||||
|
||||
import Expect
|
||||
import Test exposing (describe, test, Test)
|
||||
import TestUtil exposing (ruleTester)
|
||||
import Lint.Rules.NoUnusedVariables exposing (rule)
|
||||
import Lint.Types exposing (Error)
|
||||
|
||||
@ -11,43 +12,47 @@ error =
|
||||
Error "NoUnusedVariables"
|
||||
|
||||
|
||||
testRule =
|
||||
ruleTester rule
|
||||
|
||||
|
||||
tests : List Test
|
||||
tests =
|
||||
[ test "should not report used top-level variables" <|
|
||||
\() ->
|
||||
rule """module A exposing (b)
|
||||
testRule """module A exposing (b)
|
||||
a n = 1
|
||||
b = a 1
|
||||
"""
|
||||
|> Expect.equal []
|
||||
, test "should report unused top-level variables" <|
|
||||
\() ->
|
||||
rule "a = 1"
|
||||
testRule "a = 1"
|
||||
|> Expect.equal [ error "Variable `a` is not used" ]
|
||||
, test "should report unused top-level variables even if they are annotated" <|
|
||||
\() ->
|
||||
rule """
|
||||
testRule """
|
||||
a: Int
|
||||
a = 1
|
||||
"""
|
||||
|> Expect.equal [ error "Variable `a` is not used" ]
|
||||
, test "should not report unused top-level variables if everything is exposed" <|
|
||||
\() ->
|
||||
rule """module A exposing (..)
|
||||
testRule """module A exposing (..)
|
||||
a n = 1
|
||||
b = a 1
|
||||
"""
|
||||
|> Expect.equal []
|
||||
, test "should not report unused top-level variables that are exposed by name" <|
|
||||
\() ->
|
||||
rule """module A exposing (a, b)
|
||||
testRule """module A exposing (a, b)
|
||||
a = 1
|
||||
b = 2
|
||||
"""
|
||||
|> Expect.equal []
|
||||
, test "should not report unused top-level variables that are exposed by name, but report others" <|
|
||||
\() ->
|
||||
rule """module A exposing (a, b)
|
||||
testRule """module A exposing (a, b)
|
||||
a = 1
|
||||
b = 2
|
||||
c = 3
|
||||
@ -55,28 +60,28 @@ tests =
|
||||
|> Expect.equal [ error "Variable `c` is not used" ]
|
||||
, test "should report unused variables from let declarations" <|
|
||||
\() ->
|
||||
rule """module A exposing (a)
|
||||
testRule """module A exposing (a)
|
||||
a = let b = 1
|
||||
in 2
|
||||
"""
|
||||
|> Expect.equal [ error "Variable `b` is not used" ]
|
||||
, test "should report unused variables from let even if they are exposed by name" <|
|
||||
\() ->
|
||||
rule """module A exposing (a, b)
|
||||
testRule """module A exposing (a, b)
|
||||
a = let b = 1
|
||||
in 2
|
||||
"""
|
||||
|> Expect.equal [ error "Variable `b` is not used" ]
|
||||
, test "should report unused variables from let even if everything is exposed" <|
|
||||
\() ->
|
||||
rule """module A exposing (..)
|
||||
testRule """module A exposing (..)
|
||||
a = let b = 1
|
||||
in 2
|
||||
"""
|
||||
|> Expect.equal [ error "Variable `b` is not used" ]
|
||||
, test "should not report top-level variables used inside a let body" <|
|
||||
\() ->
|
||||
rule """module A exposing (a)
|
||||
testRule """module A exposing (a)
|
||||
b = 1
|
||||
a = let c = 1
|
||||
in b + c
|
||||
@ -84,7 +89,7 @@ tests =
|
||||
|> Expect.equal []
|
||||
, test "should not report top-level variables used inside let declarations" <|
|
||||
\() ->
|
||||
rule """module A exposing (a)
|
||||
testRule """module A exposing (a)
|
||||
b = 1
|
||||
a = let c = b
|
||||
in c
|
||||
@ -92,7 +97,7 @@ tests =
|
||||
|> Expect.equal []
|
||||
, test "should not report top-level variables used in nested lets" <|
|
||||
\() ->
|
||||
rule """module A exposing (a)
|
||||
testRule """module A exposing (a)
|
||||
b = 1
|
||||
a = let
|
||||
c = b
|
||||
@ -106,24 +111,24 @@ tests =
|
||||
|> Expect.equal []
|
||||
, test "should not report variables from let declarations that are used in the body" <|
|
||||
\() ->
|
||||
rule """module A exposing (a)
|
||||
testRule """module A exposing (a)
|
||||
a = let c = 1
|
||||
in c
|
||||
"""
|
||||
|> Expect.equal []
|
||||
, test "should not report unused function parameters" <|
|
||||
\() ->
|
||||
rule """module A exposing (a)
|
||||
testRule """module A exposing (a)
|
||||
a n = 1
|
||||
"""
|
||||
|> Expect.equal []
|
||||
, test "should report unused imported functions" <|
|
||||
\() ->
|
||||
rule "import Foo exposing (a)"
|
||||
testRule "import Foo exposing (a)"
|
||||
|> Expect.equal [ error "Variable `a` is not used" ]
|
||||
, test "should report unused imported functions (multiple imports)" <|
|
||||
\() ->
|
||||
rule "import Foo exposing (a, b, C)"
|
||||
testRule "import Foo exposing (a, b, C)"
|
||||
|> Expect.equal
|
||||
[ error "Variable `a` is not used"
|
||||
, error "Variable `b` is not used"
|
||||
@ -132,7 +137,7 @@ tests =
|
||||
-- Right now, every parameter is considered used, which is not great
|
||||
, test "should not report unused pattern matching parameters" <|
|
||||
\() ->
|
||||
rule """module A exposing (a)
|
||||
testRule """module A exposing (a)
|
||||
a = case thing of
|
||||
Foo b c -> []
|
||||
"""
|
||||
@ -140,19 +145,19 @@ tests =
|
||||
-- What to do with types needs to be determined when I understand Type exports better (wrt sub-types)
|
||||
, test "should report unused type declarations" <|
|
||||
\() ->
|
||||
rule """
|
||||
testRule """
|
||||
type A = B | C -- Should B and C be reported if they are not used? Probably.
|
||||
type alias D = { a : B }
|
||||
"""
|
||||
|> Expect.equal []
|
||||
-- , test "should not report unused type declarations if everything is exposed" <|
|
||||
-- \() ->
|
||||
-- rule """module A exposing (a, b)
|
||||
-- testRule """module A exposing (a, b)
|
||||
-- """
|
||||
-- |> Expect.equal [ error "Variable `a` is not used" ]
|
||||
-- , test "should not report unused type declarations that are exposed by name" <|
|
||||
-- \() ->
|
||||
-- rule """module A exposing (a, b)
|
||||
-- testRule """module A exposing (a, b)
|
||||
-- a = 1
|
||||
-- b = 2
|
||||
-- c = 3
|
||||
@ -160,7 +165,7 @@ tests =
|
||||
-- |> Expect.equal [ error "Variable `a` is not used" ]
|
||||
-- , test "should report unused named imports `import A exposing (a)`" <|
|
||||
-- \() ->
|
||||
-- rule """module A exposing (a, b)
|
||||
-- testRule """module A exposing (a, b)
|
||||
-- a = 1
|
||||
-- b = 2
|
||||
-- c = 3
|
||||
@ -168,7 +173,7 @@ tests =
|
||||
-- |> Expect.equal [ error "Variable `a` is not used" ]
|
||||
-- , test "should not report used named imports `import A exposing (a)`" <|
|
||||
-- \() ->
|
||||
-- rule """module A exposing (a, b)
|
||||
-- testRule """module A exposing (a, b)
|
||||
-- a = 1
|
||||
-- b = 2
|
||||
-- c = 3
|
||||
@ -176,7 +181,7 @@ tests =
|
||||
-- |> Expect.equal [ error "Variable `a` is not used" ]
|
||||
-- , test "should not report unused union imports `import A exposing (B(..))`" <|
|
||||
-- \() ->
|
||||
-- rule """module A exposing (a, b)
|
||||
-- testRule """module A exposing (a, b)
|
||||
-- a = 1
|
||||
-- b = 2
|
||||
-- c = 3
|
||||
@ -184,7 +189,7 @@ tests =
|
||||
-- |> Expect.equal [ error "Variable `a` is not used" ]
|
||||
-- , test "should report unused union imports `import A exposing (B(B))`" <|
|
||||
-- \() ->
|
||||
-- rule """module A exposing (a, b)
|
||||
-- testRule """module A exposing (a, b)
|
||||
-- a = 1
|
||||
-- b = 2
|
||||
-- c = 3
|
||||
@ -192,7 +197,7 @@ tests =
|
||||
-- |> Expect.equal [ error "Variable `a` is not used" ]
|
||||
-- , test "should not report unused union imports `import A exposing (B(B))` (with more nesting?)" <|
|
||||
-- \() ->
|
||||
-- rule """module A exposing (a, b)
|
||||
-- testRule """module A exposing (a, b)
|
||||
-- a = 1
|
||||
-- b = 2
|
||||
-- c = 3
|
||||
|
15
tests/TestUtil.elm
Normal file
15
tests/TestUtil.elm
Normal file
@ -0,0 +1,15 @@
|
||||
module TestUtil exposing (ruleTester)
|
||||
|
||||
import Regex
|
||||
import Lint.Types exposing (Error)
|
||||
|
||||
|
||||
spacesRegex : Regex.Regex
|
||||
spacesRegex =
|
||||
Regex.regex "\n "
|
||||
|
||||
|
||||
ruleTester : (String -> List Error) -> String -> List Error
|
||||
ruleTester rule str =
|
||||
Regex.replace Regex.All spacesRegex (\_ -> "\n") str
|
||||
|> rule
|
Loading…
Reference in New Issue
Block a user