Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormatWriter: do not rewrite {} => () around Defn #1757

Merged
merged 3 commits into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,13 @@ class FormatWriter(formatOps: FormatOps) {
tok.left match {
case rb: T.RightBrace => // look for "foo { bar }"
tok.meta.leftOwner match {
case b: Term.Block if RedundantBraces.canRewriteWithParens(b) =>
b.parent match {
case Some(ta: Term.Apply)
if TreeOps.isSingleElement(ta.args, b) &&
(ta.tokens.last eq rb) =>
val beg = tokens(matching(rb))
lookup.update(beg.meta.idx, tok.meta.idx -> lineOffset)
case _ =>
}
case b: Term.Block if b.parent.exists {
case ta: Term.Apply if ta.tokens.last eq rb =>
TreeOps.isSingleElement(ta.args, b)
case _ => false
} && RedundantBraces.canRewriteWithParens(b) =>
val beg = tokens(matching(rb))
lookup.update(beg.meta.idx, tok.meta.idx -> lineOffset)
case _ =>
}
case _: T.LeftBrace =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@ object RedundantBraces extends Rewrite {
case _ => false
}

def canRewriteWithParens(b: Term.Block): Boolean = {
def canRewriteWithParens(b: Term.Block): Boolean =
getBlockSingleStat(b).exists {
case f: Term.Function => RedundantBraces.canRewriteWithParens(f)
case f: Term.Function => canRewriteWithParens(f)
case _: Term.Assign => false // disallowed in 2.13
case _: Defn => false
case _ => true
}
}

/* guard for statements requiring a wrapper block
* "foo { x => y; z }" can't become "foo(x => y; z)" */
@tailrec
def canRewriteWithParens(f: Term.Function, nested: Boolean = false): Boolean =
!needParensAroundParams(f) && (getTermSingleStat(f.body) match {
case Some(t: Term.Function) => canRewriteWithParens(t, true)
case Some(_: Defn) => false
case x => nested || x.isDefined
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,57 @@ F.runAsync(stream.compile.toVector) {
def old = foo.map { a => b: SomeType => blah() }
>>>
def old = foo.map { a => b: SomeType => blah() }
<<< #1756 val within lambda
object a {
childContext.onDone { _ =>
val _ = self.withChildren(_.remove(childContext))
}
}
>>>
object a {
childContext.onDone { _ => val _ = self.withChildren(_.remove(childContext)) }
}
<<< #1756 def within lambda
object a {
childContext.onDone { _ =>
def a = self.withChildren(_.remove(childContext))
}
}
>>>
object a {
childContext.onDone { _ => def a = self.withChildren(_.remove(childContext)) }
}
<<< #1756 type within lambda
object a {
childContext.onDone { _ =>
type a = Int
}
}
>>>
object a {
childContext.onDone { _ => type a = Int }
}
<<< #1756 val without lambda
object a {
childContext.onDone { val _ = self.withChildren(_.remove(childContext)) }
}
>>>
object a {
childContext.onDone { val _ = self.withChildren(_.remove(childContext)) }
}
<<< #1756 def without lambda
object a {
childContext.onDone { def a = self.withChildren(_.remove(childContext)) }
}
>>>
object a {
childContext.onDone { def a = self.withChildren(_.remove(childContext)) }
}
<<< #1756 type without lambda
object a {
childContext.onDone { type a = Int }
}
>>>
object a {
childContext.onDone { type a = Int }
}