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

AvoidInfix: extract isWrapped, checkMatchingInfix #3146

Merged
merged 1 commit into from
Mar 7, 2022
Merged
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 @@ -38,8 +38,7 @@ class AvoidInfix(implicit ctx: RewriteCtx) extends RewriteSession {

override def rewrite(tree: Tree): Unit =
tree match {
case Term.ApplyInfix(lhs, op, _, args)
if InfixApp.isLeftAssoc(op.value) && matcher.matches(op.value) =>
case x @ Term.ApplyInfix(lhs, op, _, args) if checkMatchingInfix(x) =>
val builder = Seq.newBuilder[TokenPatch]

val opHead = op.tokens.head
Expand All @@ -64,11 +63,7 @@ class AvoidInfix(implicit ctx: RewriteCtx) extends RewriteSession {
}

val shouldWrapLhs = lhs match {
case Term.ApplyInfix(_, o, _, _) if !matcher.matches(o.value) && ! {
val head = lhs.tokens.head
head.is[Token.LeftParen] &&
ctx.getMatchingOpt(head).contains(lhs.tokens.last)
} =>
case y: Term.ApplyInfix if !isWrapped(y) && !checkMatchingInfix(y) =>
if (PlaceholderChecks.hasPlaceholder(lhs)) return
true
// foo _ compose bar => (foo _).compose(bar)
Expand Down Expand Up @@ -100,4 +95,14 @@ class AvoidInfix(implicit ctx: RewriteCtx) extends RewriteSession {
case _ =>
}

private def checkMatchingInfix(ai: Term.ApplyInfix): Boolean = {
val op = ai.op.value
InfixApp.isLeftAssoc(op) && matcher.matches(op)
}

private def isWrapped(t: Tree): Boolean = t.tokens.head match {
case h: Token.LeftParen => ctx.getMatchingOpt(h).contains(t.tokens.last)
case _ => false
}

}