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 1 commit
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
1 change: 1 addition & 0 deletions elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"elm/core": "1.0.5 <= v < 2.0.0",
"elm/json": "1.1.3 <= v < 2.0.0",
"elm/project-metadata-utils": "1.0.2 <= v < 2.0.0",
"elm-community/list-extra": "8.6.0 <= v < 9.0.0",
"jfmengels/elm-review": "2.4.0 <= v < 3.0.0",
"pzp1997/assoc-list": "1.0.0 <= v < 2.0.0",
"stil4m/elm-syntax": "7.2.7 <= v < 8.0.0"
Expand Down
57 changes: 56 additions & 1 deletion src/Simplify.elm
Original file line number Diff line number Diff line change
Expand Up @@ -556,13 +556,14 @@ 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)
import Elm.Syntax.Range as Range exposing (Location, Range)
import Elm.Type
import Json.Decode as Decode
import List.Extra
import Review.Fix as Fix exposing (Fix)
import Review.ModuleNameLookupTable as ModuleNameLookupTable exposing (ModuleNameLookupTable)
import Review.Project.Dependency as Dependency exposing (Dependency)
Expand Down Expand Up @@ -1352,10 +1353,64 @@ expressionVisitorHelp node context =
_ ->
onlyErrors []

Expression.RecordAccess record field ->
let
fieldName =
Node.value field
in
case Node.value record of
Expression.RecordExpr setters ->
onlyErrors (recordAccessChecks node fieldName setters)

Expression.RecordUpdateExpression _ setters ->
onlyErrors (recordAccessChecks node fieldName setters)
miniBill marked this conversation as resolved.
Show resolved Hide resolved

_ ->
onlyErrors []

_ ->
onlyErrors []


recordAccessChecks : Node Expression -> String -> List (Node RecordSetter) -> List (Error {})
recordAccessChecks node fieldName setters =
let
setterValues =
miniBill marked this conversation as resolved.
Show resolved Hide resolved
List.map
(\setter ->
let
( nfield, nvalue ) =
Node.value setter
in
( Node.value nfield, nvalue )
miniBill marked this conversation as resolved.
Show resolved Hide resolved
)
setters

nodeRange =
Node.range node
in
case
List.Extra.find (\( setterField, _ ) -> setterField == fieldName) setterValues
miniBill marked this conversation as resolved.
Show resolved Hide resolved
of
Just ( _, setterValue ) ->
let
setterRange =
Node.range setterValue
in
miniBill marked this conversation as resolved.
Show resolved Hide resolved
[ 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
62 changes: 62 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,64 @@ a = (x)
"""
]
]



-- Record access


recordAccessTests : Test
recordAccessTests =
let
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
]