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

No unit insertion as an addition of no auto tupling #352

Merged
merged 2 commits into from
Sep 17, 2017
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 @@ -13,16 +13,29 @@ case class NoAutoTupling(index: SemanticdbIndex)
ctx.addLeft(args.head.tokens.head, "(") +
ctx.addRight(args.last.tokens.last, ")")

override def fix(ctx: RuleCtx): Patch = {
val adaptations = index.messages.toIterator.collect {
private[this] def insertUnit(ctx: RuleCtx, t: Term.Apply): Patch =
ctx.addRight(t.tokens.init.last, "()")

lazy val unitAdaptations: Set[Position] =
index.messages.toIterator.collect {
case Message(pos, _, msg)
if msg.startsWith("Adaptation of argument list by inserting ()") =>
pos
}.toSet

lazy val tupleAdaptations: Set[Position] =
index.messages.toIterator.collect {
case Message(pos, _, msg)
if msg.startsWith("Adapting argument list by creating a") =>
pos
}.toSet

override def fix(ctx: RuleCtx): Patch = {
ctx.tree.collect {
case t: Term.Apply if adaptations.contains(t.pos) =>
case t: Term.Apply if tupleAdaptations.contains(t.pos) =>
addWrappingParens(ctx, t.args)
case t: Term.Apply if t.args.isEmpty && unitAdaptations.contains(t.pos) =>
insertUnit(ctx, t)
}.asPatch
}
}
27 changes: 27 additions & 0 deletions scalafix-tests/input/src/main/scala/test/NoUnitInsertion.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
rules = NoAutoTupling
*/
package test

class NoUnitInsertion {

val x: Option[Unit] = Option()

def a(u: Unit): Unit = u
a()

def b(x: Int)(u: Unit): Unit = (x, u)
b(2)()

val c: Unit => Unit =
u => u
c()

case class Foo(u: Unit)
Foo()
Foo.apply()

case class Bar(i: Int)(u: Unit)
Bar.apply(2)()

}
24 changes: 24 additions & 0 deletions scalafix-tests/output/src/main/scala/test/NoUnitInsertion.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package test

class NoUnitInsertion {

val x: Option[Unit] = Option(())

def a(u: Unit): Unit = u
a(())

def b(x: Int)(u: Unit): Unit = (x, u)
b(2)(())

val c: Unit => Unit =
u => u
c(())

case class Foo(u: Unit)
Foo(())
Foo.apply(())

case class Bar(i: Int)(u: Unit)
Bar.apply(2)(())

}