Skip to content

Commit 33c55b3

Browse files
committed
Call pipingIntoCompositionChecks
1 parent 83663c4 commit 33c55b3

File tree

3 files changed

+99
-16
lines changed

3 files changed

+99
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ The rule now simplifies:
2020
- `(\()) -> x) <| f <| y` to `x` (previously not always applied in pipelines)
2121
- `(\a _ -> x) y z` to `(\a -> x) y` (previously only the first argument would get removed)
2222
- `f >> (\_ -> x)` to `(\_ -> x)`
23+
- `(f >> g) data` to `g <| f data`
24+
- `(f >> g) <| data` to `g <| f <| data`
25+
- `data |> (g << f)` to `data |> f |> g`
2326
- `List.map f (List.repeat n a)` to `List.repeat n (f a)`
2427
- `Array.map f (Array.repeat n a)` to `Array.repeat n (f a)`
2528
- `String.map f (String.repeat n (String.fromChar c))` to `String.repeat n (String.fromChar (f c))`

src/Simplify.elm

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ Destructuring using case expressions
324324
a |> f >> g
325325
--> a |> f |> g
326326

327+
(f >> g) a
328+
--> g <| f a
329+
330+
(f >> g) <| a
331+
--> g <| f <| a
332+
333+
a |> (g << f)
334+
--> a |> g |> f
335+
327336

328337
### Numbers
329338

@@ -3345,6 +3354,26 @@ expressionVisitorHelp (Node expressionRange expression) config context =
33453354
, moduleCustomTypes = context.moduleCustomTypes
33463355
}
33473356

3357+
Node parens (Expression.ParenthesizedExpression ((Node _ (Expression.OperatorApplication "<<" _ _ _)) as operationNode)) ->
3358+
pipingIntoCompositionChecks
3359+
{ commentRanges = context.commentRanges
3360+
, extractSourceCode = context.extractSourceCode
3361+
, direction = CallStyle.RightToLeft
3362+
}
3363+
(\() ->
3364+
if List.isEmpty argsAfterFirst then
3365+
removeBoundariesFix applied
3366+
3367+
else
3368+
[ Fix.removeRange
3369+
{ start = { row = parens.end.row, column = parens.end.column - 1 }
3370+
, end = parens.end
3371+
}
3372+
, Fix.insertAt (Node.range firstArg).end ")"
3373+
]
3374+
)
3375+
operationNode
3376+
33483377
Node parens (Expression.ParenthesizedExpression ((Node _ (Expression.OperatorApplication ">>" _ _ _)) as operationNode)) ->
33493378
reversedCompositionChecks
33503379
{ commentRanges = context.commentRanges
@@ -17755,7 +17784,7 @@ pipelineChecks :
1775517784
}
1775617785
-> Maybe (Error {})
1775717786
pipelineChecks checkInfo =
17758-
pipingIntoCompositionChecks { commentRanges = checkInfo.commentRanges, extractSourceCode = checkInfo.extractSourceCode } checkInfo.direction checkInfo.pipedInto
17787+
pipingIntoCompositionChecks checkInfo (always []) checkInfo.pipedInto
1775917788
|> onNothing
1776017789
(\() ->
1776117790
reversedCompositionChecks checkInfo (\() -> []) checkInfo.pipedInto
@@ -17825,22 +17854,26 @@ fullyAppliedLambdaInPipelineChecks checkInfo =
1782517854

1782617855

1782717856
pipingIntoCompositionChecks :
17828-
{ commentRanges : List Range, extractSourceCode : Range -> String }
17829-
-> CallStyle.LeftOrRightDirection
17857+
{ context
17858+
| commentRanges : List Range
17859+
, extractSourceCode : Range -> String
17860+
, direction : CallStyle.LeftOrRightDirection
17861+
}
17862+
-> (() -> List Fix)
1783017863
-> Node Expression
1783117864
-> Maybe (Error {})
17832-
pipingIntoCompositionChecks context compositionDirection expressionNode =
17865+
pipingIntoCompositionChecks context fixesFromParent expressionNode =
1783317866
let
1783417867
targetAndReplacement : { opToFind : String, replacement : String }
1783517868
targetAndReplacement =
17836-
case compositionDirection of
17869+
case context.direction of
1783717870
CallStyle.RightToLeft ->
1783817871
{ opToFind = "<<", replacement = "<|" }
1783917872

1784017873
CallStyle.LeftToRight ->
1784117874
{ opToFind = ">>", replacement = "|>" }
1784217875
in
17843-
case pipingIntoCompositionChecksHelp context targetAndReplacement compositionDirection expressionNode of
17876+
case pipingIntoCompositionChecksHelp context targetAndReplacement expressionNode of
1784417877
Nothing ->
1784517878
Nothing
1784617879

@@ -17852,7 +17885,7 @@ pipingIntoCompositionChecks context compositionDirection expressionNode =
1785217885
[ "Because of the precedence of operators, using " ++ targetAndReplacement.opToFind ++ " at this location is the same as using " ++ targetAndReplacement.replacement ++ "."
1785317886
, "To make it more idiomatic in Elm and generally easier to read, please use " ++ targetAndReplacement.replacement ++ " instead. You may need to remove some parentheses to do this."
1785417887
, "Here is an example:"
17855-
++ (case compositionDirection of
17888+
++ (case context.direction of
1785617889
CallStyle.RightToLeft ->
1785717890
leftPipeExample
1785817891

@@ -17862,20 +17895,23 @@ pipingIntoCompositionChecks context compositionDirection expressionNode =
1786217895
]
1786317896
}
1786417897
error.opToReplaceRange
17865-
error.fixes
17898+
(fixesFromParent () ++ error.fixes)
1786617899
)
1786717900

1786817901

1786917902
pipingIntoCompositionChecksHelp :
17870-
{ commentRanges : List Range, extractSourceCode : Range -> String }
17903+
{ context
17904+
| commentRanges : List Range
17905+
, extractSourceCode : Range -> String
17906+
, direction : CallStyle.LeftOrRightDirection
17907+
}
1787117908
-> { opToFind : String, replacement : String }
17872-
-> CallStyle.LeftOrRightDirection
1787317909
-> Node Expression
1787417910
-> Maybe { opToReplaceRange : Range, fixes : List Fix, firstStepIsComposition : Bool }
17875-
pipingIntoCompositionChecksHelp context targetAndReplacement compositionDirection subExpression =
17911+
pipingIntoCompositionChecksHelp context targetAndReplacement subExpression =
1787617912
case Node.value subExpression of
1787717913
Expression.ParenthesizedExpression inParens ->
17878-
case pipingIntoCompositionChecksHelp context targetAndReplacement compositionDirection inParens of
17914+
case pipingIntoCompositionChecksHelp context targetAndReplacement inParens of
1787917915
Nothing ->
1788017916
Nothing
1788117917

@@ -17898,12 +17934,12 @@ pipingIntoCompositionChecksHelp context targetAndReplacement compositionDirectio
1789817934
let
1789917935
continuedSearch : Maybe { opToReplaceRange : Range, fixes : List Fix, firstStepIsComposition : Bool }
1790017936
continuedSearch =
17901-
case compositionDirection of
17937+
case context.direction of
1790217938
CallStyle.RightToLeft ->
17903-
pipingIntoCompositionChecksHelp context targetAndReplacement compositionDirection left
17939+
pipingIntoCompositionChecksHelp context targetAndReplacement left
1790417940

1790517941
CallStyle.LeftToRight ->
17906-
pipingIntoCompositionChecksHelp context targetAndReplacement compositionDirection right
17942+
pipingIntoCompositionChecksHelp context targetAndReplacement right
1790717943
in
1790817944
if symbol == targetAndReplacement.replacement then
1790917945
Maybe.map (\errors -> { errors | firstStepIsComposition = False })

tests/Simplify/PipelineTest.elm

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ After: data |> fn1 |> fn2"""
472472
a = g <| f b
473473
"""
474474
]
475-
, test "should reverse >> when used in a plain application call and add parens if there are more arguments" <|
475+
, test "should reverse >> when used in a plain application call, and add parens if there are more arguments" <|
476476
\() ->
477477
"""module A exposing (..)
478478
a = (f >> g) b c
@@ -492,6 +492,50 @@ After: data |> fn1 |> fn2"""
492492
}
493493
|> Review.Test.whenFixed """module A exposing (..)
494494
a = (g <| f b) c
495+
"""
496+
]
497+
, test "should convert from << to <| when used in a plain application call" <|
498+
\() ->
499+
"""module A exposing (..)
500+
a = (g << f) b
501+
"""
502+
|> Review.Test.run ruleWithDefaults
503+
|> Review.Test.expectErrors
504+
[ Review.Test.error
505+
{ message = "Use <| instead of <<"
506+
, details =
507+
[ "Because of the precedence of operators, using << at this location is the same as using <|."
508+
, "To make it more idiomatic in Elm and generally easier to read, please use <| instead. You may need to remove some parentheses to do this."
509+
, """Here is an example:
510+
Before: (fn3 << fn2) <| fn1 <| data
511+
After: fn3 <| fn2 <| fn1 <| data"""
512+
]
513+
, under = "<<"
514+
}
515+
|> Review.Test.whenFixed """module A exposing (..)
516+
a = g <| f b
517+
"""
518+
]
519+
, test "should convert from << to <| when used in a plain application call, and add parens if there are more arguments" <|
520+
\() ->
521+
"""module A exposing (..)
522+
a = (g << f) b c
523+
"""
524+
|> Review.Test.run ruleWithDefaults
525+
|> Review.Test.expectErrors
526+
[ Review.Test.error
527+
{ message = "Use <| instead of <<"
528+
, details =
529+
[ "Because of the precedence of operators, using << at this location is the same as using <|."
530+
, "To make it more idiomatic in Elm and generally easier to read, please use <| instead. You may need to remove some parentheses to do this."
531+
, """Here is an example:
532+
Before: (fn3 << fn2) <| fn1 <| data
533+
After: fn3 <| fn2 <| fn1 <| data"""
534+
]
535+
, under = "<<"
536+
}
537+
|> Review.Test.whenFixed """module A exposing (..)
538+
a = (g <| f b) c
495539
"""
496540
]
497541
]

0 commit comments

Comments
 (0)