Skip to content

Commit 94eb56d

Browse files
committed
Report minimal ranges
1 parent af9e005 commit 94eb56d

File tree

12 files changed

+117
-56
lines changed

12 files changed

+117
-56
lines changed
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
1-
src/B.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
1+
src/B.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
22

3-
3: f = case 0 of
4-
4: 1 -> true
53
5: 2 ->
64
6: false
75

8-
src/A.purs:9:5: CommaFirstArrays: Prefer comma-first style in array literals, items aligned vertically
6+
src/A.purs:9:5: CommaFirstArrays: Format array literals comma-first, align items vertically
97

108
9: y = [
119
10: true,
1210
11: false
1311
12: ]
1412

15-
src/nested/C.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
13+
src/nested/C.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
1614

17-
3: f = case 0 of
18-
4: 1 -> true
1915
5: 2 ->
2016
6: false
2117

22-
src/nested/doubly-nested/A.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
18+
src/nested/doubly-nested/A.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
2319

24-
3: f = case 0 of
25-
4: 1 -> true
2620
5: 2 ->
2721
6: false
2822

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
src/nested/C.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
1+
src/nested/C.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
22

3-
3: f = case 0 of
4-
4: 1 -> true
53
5: 2 ->
64
6: false
75

8-
src/nested/doubly-nested/A.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
6+
src/nested/doubly-nested/A.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
97

10-
3: f = case 0 of
11-
4: 1 -> true
128
5: 2 ->
139
6: false
1410

15-
src/B.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
11+
src/B.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
1612

17-
3: f = case 0 of
18-
4: 1 -> true
1913
5: 2 ->
2014
6: false
2115

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
src/A.purs:9:5: CommaFirstArrays: Prefer comma-first style in array literals, items aligned vertically
1+
src/A.purs:9:5: CommaFirstArrays: Format array literals comma-first, align items vertically
22

33
9: y = [
44
10: true,
55
11: false
66
12: ]
77

8-
src/nested/doubly-nested/A.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
8+
src/nested/doubly-nested/A.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
99

10-
3: f = case 0 of
11-
4: 1 -> true
1210
5: 2 ->
1311
6: false
1412

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
src/nested/doubly-nested/A.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
1+
src/nested/doubly-nested/A.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
22

3-
3: f = case 0 of
4-
4: 1 -> true
53
5: 2 ->
64
6: false
75

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
src/nested/C.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
1+
src/nested/C.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
22

3-
3: f = case 0 of
4-
4: 1 -> true
53
5: 2 ->
64
6: false
75

8-
src/nested/doubly-nested/A.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
6+
src/nested/doubly-nested/A.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
97

10-
3: f = case 0 of
11-
4: 1 -> true
128
5: 2 ->
139
6: false
1410

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
src/B.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
2-
src/A.purs:9:5: CommaFirstArrays: Prefer comma-first style in array literals, items aligned vertically
3-
src/nested/C.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
4-
src/nested/doubly-nested/A.purs:3:5: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
1+
src/B.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
2+
src/A.purs:9:5: CommaFirstArrays: Format array literals comma-first, align items vertically
3+
src/nested/C.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line
4+
src/nested/doubly-nested/A.purs:5:3: CaseBranchIndentation: Inconsistent indentation in case branches: keep either all single-line or all multi-line

spago.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ package:
4949
dependencies:
5050
- spec-node
5151
publish:
52-
version: 0.0.23
52+
version: 0.0.24
5353
license: MIT
5454
location:
5555
githubOwner: collegevine

src/Whine/Core/CaseBranchIndentation.purs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ rule _ = emptyRule { onExpr = onExpr }
3333
where
3434
onExpr :: Handle Expr
3535
onExpr = Handle case _ of
36-
e@(ExprCase { branches }) -> do
36+
ExprCase { branches } -> do
3737
let { yes, no } = partition isOneLine branchesAndGuards
3838
consistentIndent = null yes || null no
39+
smaller = if length yes <= length no then yes else no
3940
unless consistentIndent $
4041
reportViolation
41-
{ source: Just $ rangeOf e
42+
{ source: mergeRanges $ smaller # concatMap \(head /\ body) -> [head, body]
4243
, message: "Inconsistent indentation in case branches: keep either all single-line or all multi-line"
4344
}
4445
where

src/Whine/Core/CommaFirstArrays.purs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,46 @@ rule _ = emptyRule { onExpr = onExpr }
6060
onExpr :: Handle Expr
6161
onExpr = Handle case _ of
6262
ExprArray (Wrapped { open, value: Just (Separated items), close }) -> do
63-
when (multiLine && (anyMisalignedPrefixes || not correctCloseBracket)) $
63+
for_ (mergeRanges violations) \range ->
6464
reportViolation
65-
{ source: Just { start: open.range.start, end: close.range.end }
66-
, message: "Prefer comma-first style in array literals, items aligned vertically"
65+
{ source: Just range
66+
, message: "Format array literals comma-first, align items vertically"
6767
}
6868
where
6969
multiLine = open.range.start.line /= close.range.end.line
7070
lastItem = last items.tail <#> snd # fromMaybe items.head
71-
correctCloseBracket = close.range.start.line > (rangeOf lastItem).end.line
7271

7372
firstItem = open /\ items.head
7473
newLineStartingItems = firstItem : do
7574
(_ /\ prevItem) /\ (nextComma /\ nextItem) <- zip (firstItem : items.tail) items.tail
7675
guard $ (rangeOf prevItem).end.line < (rangeOf nextItem).start.line
7776
pure $ nextComma /\ nextItem
7877

79-
anyMisalignedPrefixes =
80-
newLineStartingItems # any \(prefix /\ item) ->
81-
or
78+
violations
79+
| multiLine = misalignedPrefixes <> closeBracket
80+
| otherwise = []
81+
82+
closeBracket = do
83+
guard $ or
84+
[ close.range.start.line /= (rangeOf lastItem).end.line + 1
85+
, close.range.start.column /= open.range.start.column
86+
]
87+
pure
88+
{ start: (rangeOf lastItem).end
89+
, end: close.range.end
90+
}
91+
92+
misalignedPrefixes = do
93+
prefix /\ item <- newLineStartingItems
94+
guard $ or
8295
[ prefix.range.end.line /= (rangeOf item).start.line
8396
, prefix.range.end.column /= (rangeOf item).start.column - 1
8497
, prefix.range.start.column /= open.range.start.column
85-
, open.range.start.column /= close.range.start.column
8698
]
99+
pure
100+
{ start: prefix.range.start
101+
, end: (rangeOf item).end
102+
}
87103

88104
_ ->
89105
pure unit

src/Whine/Prelude.purs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Whine.Prelude
22
( module Prelude
33
, module Reexport
4+
, module Whine.Prelude
45
) where
56

67
import Prelude
@@ -10,7 +11,7 @@ import Control.Monad.Error.Class (try) as Reexport
1011
import Control.Monad.Except (mapExcept, mapExceptT, runExcept, runExceptT) as Reexport
1112
import Control.Monad.Writer (class MonadWriter, WriterT, execWriterT, mapWriterT, tell) as Reexport
1213

13-
import Data.Array ((!!), (:), (..), (\\), nub, null, cons, drop, dropWhile, elem, filter, length, catMaybes, mapMaybe, last, partition, take, uncons, zip, zipWith) as Reexport
14+
import Data.Array ((!!), (:), (..), (\\), concatMap, foldl, nub, null, cons, drop, dropWhile, elem, filter, length, catMaybes, mapMaybe, last, partition, take, uncons, zip, zipWith) as Reexport
1415
import Data.Array.NonEmpty (NonEmptyArray) as Reexport
1516
import Data.Bifunctor (bimap, lmap, rmap) as Reexport
1617
import Data.Either (Either(..), fromRight, either, hush, note) as Reexport
@@ -30,7 +31,19 @@ import Data.Tuple.Nested (type (/\), (/\)) as Reexport
3031
import Effect (Effect) as Reexport
3132
import Effect.Class (class MonadEffect, liftEffect) as Reexport
3233

34+
import PureScript.CST.Types (SourceRange) as Reexport
35+
3336
import JSON (JSON) as Reexport
3437
import Node.Encoding (Encoding(..)) as Reexport
3538
import Node.Path (FilePath) as Reexport
3639
import Type.Row (type (+)) as Reexport
40+
41+
mergeRanges :: Array Reexport.SourceRange -> Reexport.Maybe Reexport.SourceRange
42+
mergeRanges = Reexport.foldl go Reexport.Nothing
43+
where
44+
go Reexport.Nothing r = Reexport.Just r
45+
go (Reexport.Just a) b = Reexport.Just { start: min a.start b.start, end: max a.end b.end }
46+
47+
lt x y = x.line < y.line || (x.line == y.line && x.column < y.column)
48+
min x y = if x `lt` y then x else y
49+
max x y = if x `lt` y then y else x

0 commit comments

Comments
 (0)