Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify field access for records and record updates #35

Merged
merged 7 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/Simplify.elm
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ All of these also apply for `Sub`.

import Dict exposing (Dict)
import Elm.Syntax.Declaration as Declaration exposing (Declaration)
import Elm.Syntax.Expression as Expression exposing (Expression)
import Elm.Syntax.Expression as Expression exposing (Expression, RecordSetter)
import Elm.Syntax.ModuleName exposing (ModuleName)
import Elm.Syntax.Node as Node exposing (Node(..))
import Elm.Syntax.Pattern as Pattern exposing (Pattern)
Expand Down Expand Up @@ -1352,10 +1352,50 @@ expressionVisitorHelp node context =
_ ->
onlyErrors []

Expression.RecordAccess record (Node _ fieldName) ->
case Node.value record of
Expression.RecordExpr setters ->
onlyErrors (recordAccessChecks (Node.range node) fieldName setters)

Expression.RecordUpdateExpression _ setters ->
onlyErrors (recordAccessChecks (Node.range node) fieldName setters)

_ ->
onlyErrors []

_ ->
onlyErrors []


recordAccessChecks : Range -> String -> List (Node RecordSetter) -> List (Error {})
recordAccessChecks nodeRange fieldName setters =
let
setterValues : List ( String, Node Expression )
setterValues =
miniBill marked this conversation as resolved.
Show resolved Hide resolved
List.map
(\(Node _ ( nfield, nvalue )) ->
( Node.value nfield, nvalue )
)
setters
in
case
find (\( setterField, _ ) -> setterField == fieldName) setterValues
of
Just ( _, Node setterRange _ ) ->
[ Rule.errorWithFix
{ message = "Field access can be simplified"
, details = [ "Accessing the field of a record or record update can be simplified to just that field's value" ]
}
nodeRange
[ Fix.replaceRangeBy { start = nodeRange.start, end = setterRange.start } "("
, Fix.replaceRangeBy { start = setterRange.end, end = nodeRange.end } ")"
]
]

Nothing ->
[]


type alias CheckInfo =
{ lookupTable : ModuleNameLookupTable
, inferredConstants : ( Infer.Inferred, List Infer.Inferred )
Expand Down
63 changes: 63 additions & 0 deletions tests/SimplifyTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ all =
, subTests
, parserTests
, jsonDecodeTests
, recordAccessTests
]


Expand Down Expand Up @@ -10171,3 +10172,65 @@ a = (x)
"""
]
]



-- Record access


recordAccessTests : Test
recordAccessTests =
let
details : List String
details =
[ "Accessing the field of a record or record update can be simplified to just that field's value"
]
in
describe "Simplify.RecordAccess"
[ test "should simplify record accesses for explicit records" <|
\() ->
"""module A exposing (..)
a = { b = 3 }.b
"""
|> Review.Test.run (rule defaults)
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Field access can be simplified"
, details = details
, under = "{ b = 3 }.b"
}
|> Review.Test.whenFixed """module A exposing (..)
a = (3)
"""
]
, test "shouldn't simplify record accesses for explicit records if it can't find the field" <|
\() ->
"""module A exposing (..)
a = { b = 3 }.c
"""
|> Review.Test.run (rule defaults)
|> Review.Test.expectNoErrors
, test "should simplify record accesses for record updates" <|
\() ->
"""module A exposing (..)
a = foo { d | b = f x y }.b
"""
|> Review.Test.run (rule defaults)
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Field access can be simplified"
, details = details
, under = "{ d | b = f x y }.b"
}
|> Review.Test.whenFixed """module A exposing (..)
a = foo (f x y)
"""
]
, test "shouldn't simplify record accesses for record updates if it can't find the field" <|
\() ->
"""module A exposing (..)
a = { d | b = 3 }.c
"""
|> Review.Test.run (rule defaults)
|> Review.Test.expectNoErrors
]