-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #97: Add duplicate record field update check
- Loading branch information
Showing
9 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Analyser.Checks.DuplicateRecordFieldUpdate exposing (checker) | ||
|
||
import Elm.Syntax.Range as Syntax | ||
import Elm.Syntax.Expression exposing (RecordUpdate, Expression) | ||
import Analyser.Messages.Range as Range exposing (RangeContext) | ||
import Analyser.FileContext exposing (FileContext) | ||
import Analyser.Messages.Types exposing (Message, MessageData(DuplicateRecordFieldUpdate), newMessage) | ||
import ASTUtil.Inspector as Inspector exposing (Order(Post), defaultConfig) | ||
import Dict | ||
import Analyser.Configuration exposing (Configuration) | ||
import Analyser.Checks.Base exposing (Checker, keyBasedChecker) | ||
import Dict.Extra as Dict | ||
|
||
|
||
checker : Checker | ||
checker = | ||
{ check = scan | ||
, shouldCheck = keyBasedChecker [ "DuplicateRecordFieldUpdate" ] | ||
} | ||
|
||
|
||
type alias Context = | ||
List ( String, List Syntax.Range ) | ||
|
||
|
||
scan : RangeContext -> FileContext -> Configuration -> List Message | ||
scan rangeContext fileContext _ = | ||
Inspector.inspect | ||
{ defaultConfig | ||
| onRecordUpdate = Post onRecordUpdate | ||
} | ||
fileContext.ast | ||
[] | ||
|> List.map (Tuple.mapSecond (List.map (Range.build rangeContext))) | ||
|> List.map (uncurry (DuplicateRecordFieldUpdate fileContext.path)) | ||
|> List.map (newMessage [ ( fileContext.sha1, fileContext.path ) ]) | ||
|
||
|
||
onRecordUpdate : RecordUpdate -> Context -> Context | ||
onRecordUpdate { updates } context = | ||
updates | ||
|> Dict.groupBy Tuple.first | ||
|> Dict.filter (\_ v -> List.length v > 1) | ||
|> Dict.map (\_ v -> List.map (Tuple.second >> expressionRange) v) | ||
|> Dict.toList | ||
|> (++) context | ||
|
||
|
||
expressionRange : Expression -> Syntax.Range | ||
expressionRange ( r, _ ) = | ||
r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Analyser.Checks.DuplicateRecordFieldUpdateTests exposing (..) | ||
|
||
import Analyser.Checks.DuplicateRecordFieldUpdate as DuplicateRecordFieldUpdate | ||
import Analyser.Checks.CheckTestUtil as CTU | ||
import Analyser.Messages.Types exposing (..) | ||
import Analyser.Messages.Range as Range | ||
import Test exposing (..) | ||
|
||
|
||
duplicateUpdate : ( String, String, List MessageData ) | ||
duplicateUpdate = | ||
( "duplicateUpdate" | ||
, """module Foo exposing (..) | ||
foo = { bar | a = x, a = y} | ||
""" | ||
, [ DuplicateRecordFieldUpdate "./foo.elm" | ||
"a" | ||
[ Range.manual | ||
{ start = { row = 3, column = 18 }, end = { row = 3, column = 19 } } | ||
{ start = { row = 3, column = 17 }, end = { row = 3, column = 18 } } | ||
, Range.manual | ||
{ start = { row = 3, column = 25 }, end = { row = 3, column = 26 } } | ||
{ start = { row = 3, column = 24 }, end = { row = 3, column = 25 } } | ||
] | ||
] | ||
) | ||
|
||
|
||
nonDuplicateUpdate : ( String, String, List MessageData ) | ||
nonDuplicateUpdate = | ||
( "nonDuplicateUpdate" | ||
, """module Foo exposing (..) | ||
foo = { bar | a = x } | ||
""" | ||
, [] | ||
) | ||
|
||
|
||
all : Test | ||
all = | ||
CTU.build "Analyser.Checks.ImportAllTests" | ||
DuplicateRecordFieldUpdate.checker | ||
[ duplicateUpdate | ||
, nonDuplicateUpdate | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters