-
Notifications
You must be signed in to change notification settings - Fork 186
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
Implement RemoveUnusedImports rewrite. #166
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
61 changes: 61 additions & 0 deletions
61
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,61 @@ | ||
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)) {} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️