Make Lint.Fix.fix return a value that shows whether it was successful

This commit is contained in:
Jeroen Engels 2019-08-04 14:48:57 +02:00
parent 7f6580d1fd
commit 4eb024ef2e
3 changed files with 39 additions and 30 deletions

View File

@ -42,7 +42,7 @@ type LintError
, message : String
, details : List String
, range : Range
, fixedSource : Maybe (() -> String)
, fixedSource : Maybe (() -> Fix.Result)
}
@ -222,6 +222,6 @@ errorRange (LintError error) =
{-| Get the result of the fix of a rule for an error.
-}
fixedSource : LintError -> Maybe (() -> String)
fixedSource : LintError -> Maybe (() -> Fix.Result)
fixedSource (LintError error) =
error.fixedSource

View File

@ -1,8 +1,8 @@
module Lint.Fix exposing
( Fix
, removeRange, replaceRangeBy, insertAt
, Result(..), Problem(..), fix
, mergeRanges
, fix
)
{-| Gives tools to make changes to the source code.
@ -18,15 +18,15 @@ module Lint.Fix exposing
@docs removeRange, replaceRangeBy, insertAt
# Applying fixes
@docs Result, Problem, fix
# Utilitaries for working with ranges
@docs mergeRanges
# Applying fixes
@docs fix
-}
import Array
@ -37,13 +37,21 @@ import Elm.Syntax.Range exposing (Range)
-- DEFINITION
{-| -}
type Fix
= Removal Range
| Replacement Range String
| InsertAt { row : Int, column : Int } String
type Result
= Successful String
| Errored Problem
type Problem
= Unchanged
-- CONSTRUCTORS
@ -73,12 +81,13 @@ insertAt =
{-| Apply the changes on the source code.
-}
fix : List Fix -> String -> String
fix : List Fix -> String -> Result
fix fixes sourceCode =
fixes
|> List.sortBy (rangePosition >> negate)
|> List.foldl applyFix (String.lines sourceCode)
|> String.join "\n"
|> Successful
rangePosition : Fix -> Int

View File

@ -124,9 +124,9 @@ a = Debug.log "foo" 1
]
in
Fix.fix fixes source
|> Expect.equal """module A exposing (a)
|> Expect.equal (Fix.Successful """module A exposing (a)
a = 1
"""
""")
, test "should apply a replacement on a single line" <|
\() ->
let
@ -145,9 +145,9 @@ some_var = 1
]
in
Fix.fix fixes source
|> Expect.equal """module A exposing (a)
|> Expect.equal (Fix.Successful """module A exposing (a)
someVar = 1
"""
""")
, test "should insert something on a single line" <|
\() ->
let
@ -164,9 +164,9 @@ a = 1
]
in
Fix.fix fixes source
|> Expect.equal """module A exposing (a)
|> Expect.equal (Fix.Successful """module A exposing (a)
a = Debug.log "foo" 1
"""
""")
, test "should apply multiple fixes regardless of the order" <|
\() ->
let
@ -190,14 +190,14 @@ a = 1
Expect.all
[ \() ->
Fix.fix fixes source
|> Expect.equal """module A exposing (a)
|> Expect.equal (Fix.Successful """module A exposing (a)
someVar = Debug.log "foo" 1
"""
""")
, \() ->
Fix.fix (List.reverse fixes) source
|> Expect.equal """module A exposing (a)
|> Expect.equal (Fix.Successful """module A exposing (a)
someVar = Debug.log "foo" 1
"""
""")
]
()
, test "should apply a removal on multiple lines" <|
@ -220,11 +220,11 @@ a = 1
]
in
Fix.fix fixes source
|> Expect.equal """module A exposing (someCode)
|> Expect.equal (Fix.Successful """module A exposing (someCode)
someCode = 2
"""
""")
, test "should apply a replacement whose content is on multiple lines" <|
\() ->
let
@ -243,10 +243,10 @@ some_var = 1
]
in
Fix.fix fixes source
|> Expect.equal """module A exposing (a)
|> Expect.equal (Fix.Successful """module A exposing (a)
someVar =
1
"""
""")
, test "should apply a replacement on multiple lines" <|
\() ->
let
@ -266,9 +266,9 @@ some_var =
]
in
Fix.fix fixes source
|> Expect.equal """module A exposing (a)
|> Expect.equal (Fix.Successful """module A exposing (a)
someVar = 1
"""
""")
, test "should apply a replacement on multiple lines with something on multiple lines" <|
\() ->
let
@ -288,10 +288,10 @@ some_var =
]
in
Fix.fix fixes source
|> Expect.equal """module A exposing (a)
|> Expect.equal (Fix.Successful """module A exposing (a)
foo =
2
"""
""")
, test "should apply an insertion on multiple lines" <|
\() ->
let
@ -311,12 +311,12 @@ a = 1
]
in
Fix.fix fixes source
|> Expect.equal """module A exposing (someCode)
|> Expect.equal (Fix.Successful """module A exposing (someCode)
someCode = 2
b =
2
a : Int
a = 1
"""
""")
]