mirror of
https://github.com/jfmengels/elm-review.git
synced 2024-12-24 02:02:29 +03:00
Refactor how fixes are applied
This commit is contained in:
parent
339e5a0c04
commit
957ce610e3
@ -200,47 +200,23 @@ fix : Error.Target -> List Fix -> String -> FixResult
|
||||
fix target fixes sourceCode =
|
||||
case target of
|
||||
Error.Module ->
|
||||
if containRangeCollisions fixes then
|
||||
Errored HasCollisionsInFixRanges
|
||||
|
||||
else
|
||||
let
|
||||
resultAfterFix : String
|
||||
resultAfterFix =
|
||||
fixes
|
||||
|> List.sortBy (rangePosition >> negate)
|
||||
|> List.foldl applyFix (String.lines sourceCode)
|
||||
|> String.join "\n"
|
||||
in
|
||||
if sourceCode == resultAfterFix then
|
||||
Errored Unchanged
|
||||
|
||||
else
|
||||
tryToApplyFix
|
||||
fixes
|
||||
sourceCode
|
||||
(\resultAfterFix ->
|
||||
case Elm.Parser.parse resultAfterFix of
|
||||
Err _ ->
|
||||
Errored <| SourceCodeIsNotValid resultAfterFix
|
||||
Err (SourceCodeIsNotValid resultAfterFix)
|
||||
|
||||
Ok _ ->
|
||||
Successful resultAfterFix
|
||||
Ok resultAfterFix
|
||||
)
|
||||
|
||||
Error.Readme ->
|
||||
if containRangeCollisions fixes then
|
||||
Errored HasCollisionsInFixRanges
|
||||
|
||||
else
|
||||
let
|
||||
resultAfterFix : String
|
||||
resultAfterFix =
|
||||
fixes
|
||||
|> List.sortBy (rangePosition >> negate)
|
||||
|> List.foldl applyFix (String.lines sourceCode)
|
||||
|> String.join "\n"
|
||||
in
|
||||
if sourceCode == resultAfterFix then
|
||||
Errored Unchanged
|
||||
|
||||
else
|
||||
Successful resultAfterFix
|
||||
tryToApplyFix
|
||||
fixes
|
||||
sourceCode
|
||||
(always (Ok ()))
|
||||
|
||||
Error.ElmJson ->
|
||||
Errored Unchanged
|
||||
@ -252,6 +228,32 @@ fix target fixes sourceCode =
|
||||
Errored Unchanged
|
||||
|
||||
|
||||
tryToApplyFix : List Fix -> String -> (String -> Result Problem b) -> FixResult
|
||||
tryToApplyFix fixes sourceCode validation =
|
||||
if containRangeCollisions fixes then
|
||||
Errored HasCollisionsInFixRanges
|
||||
|
||||
else
|
||||
let
|
||||
resultAfterFix : String
|
||||
resultAfterFix =
|
||||
fixes
|
||||
|> List.sortBy (rangePosition >> negate)
|
||||
|> List.foldl applyFix (String.lines sourceCode)
|
||||
|> String.join "\n"
|
||||
in
|
||||
if sourceCode == resultAfterFix then
|
||||
Errored Unchanged
|
||||
|
||||
else
|
||||
case validation resultAfterFix of
|
||||
Ok _ ->
|
||||
Successful resultAfterFix
|
||||
|
||||
Err problem ->
|
||||
Errored problem
|
||||
|
||||
|
||||
containRangeCollisions : List Fix -> Bool
|
||||
containRangeCollisions fixes =
|
||||
fixes
|
||||
|
Loading…
Reference in New Issue
Block a user