-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement RemoveUnusedImports rewrite.
This rewrite does not group/sort/expand rewrites, only remove unused imports.
- Loading branch information
Showing
17 changed files
with
251 additions
and
45 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,7 @@ | ||
#!/usr/bin/env bash | ||
set -eux | ||
rewrite=$1 | ||
|
||
touch scalafix-core/src/main/scala/scalafix/rewrite/${rewrite}.scala | ||
touch scalafix-tests/input/src/main/scala/test/${rewrite}.scala | ||
touch scalafix-tests/output/src/main/scala/test/${rewrite}.scala |
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
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
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
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
28 changes: 28 additions & 0 deletions
28
scalafix-core/src/main/scala/scalafix/rewrite/RemoveUnusedImports.scala
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,28 @@ | ||
package scalafix | ||
package rewrite | ||
|
||
import scala.meta._ | ||
import scalafix.syntax._ | ||
import org.scalameta.logger | ||
|
||
case class RemoveUnusedImports(mirror: Mirror) | ||
extends SemanticRewrite(mirror) { | ||
private val unusedImports = mirror.database.messages.toIterator.collect { | ||
case Message(pos, _, "Unused import") => | ||
pos | ||
}.toSet | ||
private def isUnused(importee: Importee) = importee match { | ||
// NOTE: workaround for https://github.com/scalameta/scalameta/issues/899 | ||
case Importee.Wildcard() => | ||
val lookupPos = | ||
importee.parents | ||
.collectFirst { case x: Import => x.pos } | ||
.getOrElse(importee.pos) | ||
unusedImports.contains(lookupPos) | ||
case _ => unusedImports.contains(importee.pos) | ||
} | ||
override def rewrite(ctx: RewriteCtx): Patch = | ||
ctx.tree.collect { | ||
case i: Importee if isUnused(i) => ctx.removeImportee(i) | ||
}.asPatch | ||
} |
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
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
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
78 changes: 78 additions & 0 deletions
78
scalafix-core/src/main/scala/scalafix/util/MatchingParens.scala
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,78 @@ | ||
package scalafix.util | ||
|
||
import scala.meta._ | ||
import scalafix.util.TokenOps._ | ||
import tokens.Token._ | ||
|
||
sealed abstract class MatchingParens(map: Map[TokenHash, Token]) { | ||
private def lookup(token: Token) = map.get(hash(token)) | ||
def close(open: Token.LeftParen): Option[Token.RightParen] = | ||
lookup(open).collect { case x: Token.RightParen => x } | ||
def close(open: Token.LeftBracket): Option[Token.RightBracket] = | ||
lookup(open).collect { case x: Token.RightBracket => x } | ||
def close(open: Token.LeftBrace): Option[Token.RightBrace] = | ||
lookup(open).collect { case x: Token.RightBrace => x } | ||
def open(close: Token.RightParen): Option[Token.LeftParen] = | ||
lookup(close).collect { case x: Token.LeftParen => x } | ||
def open(close: Token.RightBracket): Option[Token.LeftBracket] = | ||
lookup(close).collect { case x: Token.LeftBracket => x } | ||
def open(close: Token.RightBrace): Option[Token.LeftBrace] = | ||
lookup(close).collect { case x: Token.LeftBrace => x } | ||
} | ||
|
||
object MatchingParens { | ||
private def assertValidParens(open: Token, close: Token): Unit = { | ||
(open, close) match { | ||
case (Interpolation.Start(), Interpolation.End()) => | ||
case (LeftBrace(), RightBrace()) => | ||
case (LeftBracket(), RightBracket()) => | ||
case (LeftParen(), RightParen()) => | ||
case (o, c) => | ||
throw new IllegalArgumentException(s"Mismatching parens ($o, $c)") | ||
} | ||
} | ||
|
||
/** | ||
* Finds matching parens [({})]. | ||
* | ||
* Contains lookup keys in both directions, opening [({ and closing })]. | ||
*/ | ||
private def getMatchingParentheses(tokens: Tokens): Map[TokenHash, Token] = { | ||
val ret = Map.newBuilder[TokenHash, Token] | ||
var stack = List.empty[Token] | ||
tokens.foreach { | ||
case open @ (LeftBrace() | LeftBracket() | LeftParen() | | ||
Interpolation.Start()) => | ||
stack = open :: stack | ||
case close @ (RightBrace() | RightBracket() | RightParen() | | ||
Interpolation.End()) => | ||
val open = stack.head | ||
assertValidParens(open, close) | ||
ret += hash(open) -> close | ||
ret += hash(close) -> open | ||
stack = stack.tail | ||
case _ => | ||
} | ||
val result = ret.result() | ||
result | ||
} | ||
def apply(tokens: Tokens): MatchingParens = | ||
new MatchingParens(getMatchingParentheses(tokens)) {} | ||
} | ||
|
||
object TreeOps { | ||
def parents(tree: Tree): Stream[Tree] = | ||
tree #:: (tree.parent match { | ||
case Some(x) => parents(x) | ||
case _ => Stream.empty | ||
}) | ||
} | ||
object TokenOps { | ||
type TokenHash = Long | ||
def hash(token: Token): TokenHash = { | ||
val longHash: Long = | ||
(token.productPrefix.hashCode.toLong << (62 - 8)) | | ||
(token.start.toLong << (62 - (8 + 28))) | token.end | ||
longHash | ||
} | ||
} |
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
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
Oops, something went wrong.