-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
rule = MatchParentheses | ||
*/ | ||
package fix | ||
|
||
trait MatchParenthesesTest { | ||
def f1(x: List[Int]): Int = (x match { | ||
case Nil => "," | ||
case _ => x.mkString(",") | ||
}).length | ||
|
||
def g(s: String): String | ||
|
||
def f2(x: List[Int]): Int = g(x match { | ||
case Nil => "," | ||
case _ => x.mkString(",") | ||
}).length | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package fix | ||
|
||
trait MatchParenthesesTest { | ||
def f1(x: List[Int]): Int = x .match { | ||
case Nil => "," | ||
case _ => x.mkString(",") | ||
}.length | ||
|
||
def g(s: String): String | ||
|
||
def f2(x: List[Int]): Int = g(x match { | ||
case Nil => "," | ||
case _ => x.mkString(",") | ||
}).length | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package fix | ||
|
||
import scala.meta.Term | ||
import scala.meta.XtensionClassifiable | ||
import scala.meta.XtensionCollectionLikeUI | ||
import scala.meta.tokens.Token | ||
import scalafix.Patch | ||
import scalafix.v1.SyntacticDocument | ||
import scalafix.v1.SyntacticRule | ||
import scalafix.v1.XtensionOptionPatch | ||
import scalafix.v1.XtensionSeqPatch | ||
|
||
class MatchParentheses extends SyntacticRule("MatchParentheses") { | ||
override def fix(implicit doc: SyntacticDocument): Patch = { | ||
doc.tree.collect { case t: Term.Match => | ||
PartialFunction | ||
.condOpt(doc.tokens.filter(_.pos.start >= t.pos.end).take(2)) { case Seq(r: Token.RightParen, _: Token.Dot) => | ||
PartialFunction | ||
.condOpt(doc.tokens.filter(_.pos.end <= t.pos.start).takeRight(2)) { | ||
case Seq(n, l: Token.LeftParen) if !n.is[Token.Ident] => | ||
Seq( | ||
Patch.removeToken(l), | ||
t.tokens.collectFirst { case m: Token.KwMatch => | ||
Patch.addLeft(m, ".") | ||
}.asPatch, | ||
Patch.removeToken(r) | ||
).asPatch | ||
} | ||
.asPatch | ||
} | ||
.asPatch | ||
}.asPatch | ||
} | ||
} |