Refactor how fixes are applied

This commit is contained in:
Jeroen Engels 2021-04-02 08:48:10 +02:00
parent 339e5a0c04
commit 957ce610e3

View File

@ -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 =
tryToApplyFix
fixes
|> List.sortBy (rangePosition >> negate)
|> List.foldl applyFix (String.lines sourceCode)
|> String.join "\n"
in
if sourceCode == resultAfterFix then
Errored Unchanged
else
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 =
tryToApplyFix
fixes
|> List.sortBy (rangePosition >> negate)
|> List.foldl applyFix (String.lines sourceCode)
|> String.join "\n"
in
if sourceCode == resultAfterFix then
Errored Unchanged
else
Successful resultAfterFix
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