Skip to content

Commit

Permalink
add fromList + isEmpty -> List.isEmpty simpls
Browse files Browse the repository at this point in the history
  • Loading branch information
morteako authored and jfmengels committed Apr 4, 2024
1 parent 3a44c3a commit 20a0c22
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

The rule also simplifies:
- `Set.isEmpty (Set.fromList list)` to `List.isEmpty list`
- `Dict.isEmpty (Dict.fromList list)` to `List.isEmpty list`
- `Array.isEmpty (Array.fromList list)` to `List.isEmpty list`

## [2.1.4] - 2024-04-01

The rule now simplifies unnecessary wrapping of values evaluated by a case expression:
Expand Down
27 changes: 24 additions & 3 deletions src/Simplify.elm
Expand Up @@ -5863,7 +5863,14 @@ arrayIndexedMapChecks =

arrayIsEmptyChecks : IntoFnCheck
arrayIsEmptyChecks =
intoFnCheckOnlyCall (collectionIsEmptyChecks arrayCollection)
intoFnChecksFirstThatConstructsError
[ intoFnCheckOnlyCall (collectionIsEmptyChecks arrayCollection)
, onSpecificFnCallCanBeCombinedCheck
{ args = []
, earlierFn = Fn.Array.fromList
, combinedFn = Fn.List.isEmpty
}
]


arrayLengthChecks : IntoFnCheck
Expand Down Expand Up @@ -5970,7 +5977,14 @@ setFromListChecks =

setIsEmptyChecks : IntoFnCheck
setIsEmptyChecks =
intoFnCheckOnlyCall (collectionIsEmptyChecks setCollection)
intoFnChecksFirstThatConstructsError
[ intoFnCheckOnlyCall (collectionIsEmptyChecks setCollection)
, onSpecificFnCallCanBeCombinedCheck
{ args = []
, earlierFn = Fn.Set.fromList
, combinedFn = Fn.List.isEmpty
}
]


setSizeChecks : IntoFnCheck
Expand Down Expand Up @@ -6055,7 +6069,14 @@ dictFromListChecks =

dictIsEmptyChecks : IntoFnCheck
dictIsEmptyChecks =
intoFnCheckOnlyCall (collectionIsEmptyChecks dictCollection)
intoFnChecksFirstThatConstructsError
[ intoFnCheckOnlyCall (collectionIsEmptyChecks dictCollection)
, onSpecificFnCallCanBeCombinedCheck
{ args = []
, earlierFn = Fn.Dict.fromList
, combinedFn = Fn.List.isEmpty
}
]


dictSizeChecks : IntoFnCheck
Expand Down
18 changes: 18 additions & 0 deletions tests/Simplify/ArrayTest.elm
Expand Up @@ -1188,6 +1188,24 @@ a = Array.isEmpty (Array.repeat 2 x)
|> Review.Test.whenFixed """module A exposing (..)
import Array
a = False
"""
]
, test "should replace list |> Array.fromList |> Array.isEmpty by list |> List.isEmpty" <|
\() ->
"""module A exposing (..)
import Array
a = list |> Array.fromList |> Array.isEmpty
"""
|> Review.Test.run ruleWithDefaults
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Array.fromList, then Array.isEmpty can be combined into List.isEmpty"
, details = [ "You can replace this call by List.isEmpty with the same arguments given to Array.fromList which is meant for this exact purpose." ]
, under = "Array.isEmpty"
}
|> Review.Test.whenFixed """module A exposing (..)
import Array
a = list |> List.isEmpty
"""
]
, test "should not report Array.isEmpty (Array.repeat n x)" <|
Expand Down
18 changes: 18 additions & 0 deletions tests/Simplify/DictTest.elm
Expand Up @@ -134,6 +134,24 @@ a = Dict.singleton x y |> Dict.isEmpty
|> Review.Test.whenFixed """module A exposing (..)
import Dict
a = False
"""
]
, test "should replace list |> Dict.fromList |> Dict.isEmpty by list |> List.isEmpty" <|
\() ->
"""module A exposing (..)
import Dict
a = list |> Dict.fromList |> Dict.isEmpty
"""
|> Review.Test.run ruleWithDefaults
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Dict.fromList, then Dict.isEmpty can be combined into List.isEmpty"
, details = [ "You can replace this call by List.isEmpty with the same arguments given to Dict.fromList which is meant for this exact purpose." ]
, under = "Dict.isEmpty"
}
|> Review.Test.whenFixed """module A exposing (..)
import Dict
a = list |> List.isEmpty
"""
]
]
Expand Down
18 changes: 18 additions & 0 deletions tests/Simplify/SetTests.elm
Expand Up @@ -879,6 +879,24 @@ a = Set.singleton set |> Set.isEmpty
|> Review.Test.whenFixed """module A exposing (..)
import Set
a = False
"""
]
, test "should replace list |> Set.fromList |> Set.isEmpty by list |> List.isEmpty" <|
\() ->
"""module A exposing (..)
import Set
a = list |> Set.fromList |> Set.isEmpty
"""
|> Review.Test.run ruleWithDefaults
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Set.fromList, then Set.isEmpty can be combined into List.isEmpty"
, details = [ "You can replace this call by List.isEmpty with the same arguments given to Set.fromList which is meant for this exact purpose." ]
, under = "Set.isEmpty"
}
|> Review.Test.whenFixed """module A exposing (..)
import Set
a = list |> List.isEmpty
"""
]
]
Expand Down

0 comments on commit 20a0c22

Please sign in to comment.