Move mergeRanges to Lint.Fix

Also add tests and documentation
This commit is contained in:
Jeroen Engels 2019-08-04 11:42:59 +02:00
parent df840de3d6
commit d20396773e
3 changed files with 176 additions and 15 deletions

View File

@ -1,6 +1,7 @@
module Lint.Fix exposing
( Fix
, removeRange, replaceRangeBy, insertAt
, mergeRanges
, fix
)
@ -17,6 +18,11 @@ module Lint.Fix exposing
@docs removeRange, replaceRangeBy, insertAt
# Utilitaries for working with ranges
@docs mergeRanges
# Applying fixes
@docs fix
@ -95,6 +101,66 @@ rangePosition fix_ =
row * 1000000 + column
-- UTILITARIES FOR WORKING WITH RANGES
{-| Create a new range that starts at the start of the range that starts first,
and ends at the end of the range that starts last. If the two ranges are distinct
and there is code in between, that code will be included in the resulting range.
range : Range
range =
Fix.mergeRanges
(Node.range node1)
(Node.range node2)
-}
mergeRanges : Range -> Range -> Range
mergeRanges a b =
let
start : { row : Int, column : Int }
start =
case comparePosition a.start b.start of
LT ->
a.start
EQ ->
a.start
GT ->
b.start
end : { row : Int, column : Int }
end =
case comparePosition a.end b.end of
LT ->
b.end
EQ ->
b.end
GT ->
a.end
in
{ start = start, end = end }
comparePosition : { row : Int, column : Int } -> { row : Int, column : Int } -> Order
comparePosition a b =
let
order : Order
order =
compare a.row b.row
in
case order of
EQ ->
compare a.column b.column
_ ->
order
applyFix : Fix -> String -> String
applyFix fix_ source =
source

View File

@ -621,28 +621,20 @@ registerFunction function context =
functionRange : Range
functionRange =
mergeRanges
(Node.range function.declaration)
(case function.signature of
Just signature ->
Node.range signature
case function.signature of
Just signature ->
Fix.mergeRanges
(Node.range function.declaration)
(Node.range signature)
Nothing ->
Node.range function.declaration
)
Nothing ->
Node.range function.declaration
in
context
|> register (Variable functionRange) (Node.range declaration.name) (Node.value declaration.name)
|> markUsedTypesAndModules namesUsedInSignature
mergeRanges : Range -> Range -> Range
mergeRanges r1 r2 =
-- TODO Inverse r1 and r2 if r2 comes before r1
-- TODO Move into a util file or something
{ start = r1.start, end = r2.end }
collectFromExposing : Exposing -> List ( VariableType, Range, String )
collectFromExposing exposing_ =
case exposing_ of

103
tests/FixTest.elm Normal file
View File

@ -0,0 +1,103 @@
module FixTest exposing (all)
import Elm.Syntax.Range exposing (Range)
import Expect exposing (Expectation)
import Lint.Fix as Fix
import Test exposing (Test, describe, test)
all : Test
all =
describe "Fix"
[ mergeRangesTest
]
mergeRangesTest : Test
mergeRangesTest =
describe "mergeRanges"
[ test "should take the start of the one before and the end of the one after if ranges are distinct" <|
\() ->
let
a : Range
a =
{ start = { row = 2, column = 10 }
, end = { row = 2, column = 15 }
}
b : Range
b =
{ start = { row = 20, column = 1 }
, end = { row = 22, column = 5 }
}
expected : Range
expected =
{ start = { row = 2, column = 10 }
, end = { row = 22, column = 5 }
}
in
Expect.all
[ \() ->
Fix.mergeRanges a b
|> Expect.equal expected
, \() ->
Fix.mergeRanges b a
|> Expect.equal expected
]
()
, test "should take the start of the one that starts first and the end of the one ends last if ranges are shared" <|
\() ->
let
a : Range
a =
{ start = { row = 2, column = 10 }
, end = { row = 10, column = 15 }
}
b : Range
b =
{ start = { row = 5, column = 1 }
, end = { row = 22, column = 5 }
}
expected : Range
expected =
{ start = { row = 2, column = 10 }
, end = { row = 22, column = 5 }
}
in
Expect.all
[ \() ->
Fix.mergeRanges a b
|> Expect.equal expected
, \() ->
Fix.mergeRanges b a
|> Expect.equal expected
]
()
, test "should take the bigger one if one is included in the other" <|
\() ->
let
a : Range
a =
{ start = { row = 2, column = 10 }
, end = { row = 10, column = 15 }
}
b : Range
b =
{ start = { row = 3, column = 1 }
, end = { row = 4, column = 5 }
}
in
Expect.all
[ \() ->
Fix.mergeRanges a b
|> Expect.equal a
, \() ->
Fix.mergeRanges b a
|> Expect.equal a
]
()
]