Skip to content

Commit

Permalink
Router bugfix: find the correct lambda right arrow
Browse files Browse the repository at this point in the history
When lambda parameters are themselves functions and contained arrows in
their type definitions, the code would locate the wrong arrow and then
fail to break after it as no such break is defined in any rules.

Fixes #1714.
  • Loading branch information
kitbellew committed Feb 17, 2020
1 parent 20d0f57 commit 2f3e215
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,18 @@ class Router(formatOps: FormatOps) {
.get(hash(right))
.collect {
case owner: Term.Function =>
val arrow = lastLambda(owner).tokens.find(_.is[T.RightArrow])
val lastFunc = lastLambda(owner)
// look for arrow before body, if any, else after params
val arrow = lastFunc.body.tokens.headOption
.map(x => prevNonComment(tokens(x, -1)).left)
.orElse {
val lastParam = lastFunc.params.lastOption
lastParam.flatMap(_.tokens.lastOption).map { x =>
val beforeArrow = nextNonComment(tokens(x))
if (beforeArrow.right.is[T.RightArrow]) beforeArrow.right
else nextNonComment(tokens(beforeArrow, 1)).right
}
}
val expire = arrow.getOrElse(owner.params.last.tokens.last)
(expire, arrow, 0)
}
Expand Down
85 changes: 85 additions & 0 deletions scalafmt-tests/src/test/resources/default/Lambda.stat
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,88 @@ object a {
|""".stripMargin
}
}
<<< #1714 1
testAsync("Resource.make is equivalent to a partially applied bracket") { implicit ec =>
check { (acquire: IO[String], release: String => IO[Unit], f: String => IO[String]) =>
acquire.bracket(f)(release) <-> Resource.make(acquire)(release).use(f)
}
}
>>>
testAsync("Resource.make is equivalent to a partially applied bracket") {
implicit ec =>
check {
(acquire: IO[String],
release: String => IO[Unit],
f: String => IO[String]) =>
acquire.bracket(f)(release) <-> Resource.make(acquire)(release).use(f)
}
}
<<< #1714 2
align = none
maxColumn = 120
danglingParentheses = true
===
testAsync("Resource.make is equivalent to a partially applied bracket") { implicit ec =>
check { (acquire: IO[String], release: String => IO[Unit], f: String => IO[String]) =>
acquire.bracket(f)(release) <-> Resource.make(acquire)(release).use(f)
}
}
>>>
testAsync("Resource.make is equivalent to a partially applied bracket") { implicit ec =>
check { (acquire: IO[String], release: String => IO[Unit], f: String => IO[String]) =>
acquire.bracket(f)(release) <-> Resource.make(acquire)(release).use(f)
}
}
<<< #1714 3
align = none
maxColumn = 120
danglingParentheses = true
===
testAsync("Resource.make is equivalent to a partially applied bracket") { implicit ec =>
check { (acquire: IO[String], release: String => IO[Unit], f: String => IO[String]/* c1 */)/* c2 */=>
/*c3*/ acquire.bracket(f)(release) <-> Resource.make(acquire)(release).use(f)
}
}
>>>
testAsync("Resource.make is equivalent to a partially applied bracket") { implicit ec =>
check { (acquire: IO[String], release: String => IO[Unit], f: String => IO[String] /* c1 */ ) /* c2 */ =>
/*c3*/
acquire.bracket(f)(release) <-> Resource.make(acquire)(release).use(f)
}
}
<<< #1714 4
align = none
maxColumn = 120
danglingParentheses = true
===
testAsync("Resource.make is equivalent to a partially applied bracket") { implicit ec =>
check { (acquire: IO[String], release: String => IO[Unit], f: String => IO[String]/* c1 */)/* c2 */=>
/*c3*/ { acquire.bracket(f)(release) <-> Resource.make(acquire)(release).use(f) }
}
}
>>>
testAsync("Resource.make is equivalent to a partially applied bracket") { implicit ec =>
check { (acquire: IO[String], release: String => IO[Unit], f: String => IO[String] /* c1 */ ) /* c2 */ =>
/*c3*/
{ acquire.bracket(f)(release) <-> Resource.make(acquire)(release).use(f) }
}
}
<<< #1714 5
align = none
maxColumn = 120
danglingParentheses = true
===
testAsync("Resource.make is equivalent to a partially applied bracket") { implicit ec =>
check { (acquire: IO[String], release: String => IO[Unit], f: String => IO[String]/* c1 */)/* c2 */=>
{ /*c3*/ acquire.bracket(f)(release) <-> Resource.make(acquire)(release).use(f) }
}
}
>>>
testAsync("Resource.make is equivalent to a partially applied bracket") { implicit ec =>
check { (acquire: IO[String], release: String => IO[Unit], f: String => IO[String] /* c1 */ ) /* c2 */ =>
{
/*c3*/
acquire.bracket(f)(release) <-> Resource.make(acquire)(release).use(f)
}
}
}

0 comments on commit 2f3e215

Please sign in to comment.