Skip to content

Commit

Permalink
preserve no-op behavior when passing trees built with quasiquotes
Browse files Browse the repository at this point in the history
Quasiquotes-generated trees have positions since scalameta#3450. Fixes:

error: patch.md:168:1: token not found:
Patch.removeImportee(importee"Future").showDiff()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
java.util.NoSuchElementException: token not found:
  at scalafix.util.TokenList.$anonfun$tok2idx$2(TokenList.scala:27)
  at scala.collection.immutable.Map$WithDefault.default(Map.scala:181)
  at scala.collection.MapOps.apply(Map.scala:176)
  at scala.collection.MapOps.apply$(Map.scala:175)
  at scala.collection.AbstractMap.apply(Map.scala:405)
  at scalafix.util.TokenList.leading(TokenList.scala:15)
  at scalafix.internal.patch.ImportPatchOps$.remove$1(ImportPatchOps.scala:229)
  • Loading branch information
bjaglin committed Feb 15, 2024
1 parent e3709f5 commit 5f4ef49
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import scala.annotation.tailrec
import scala.collection.mutable

import scala.meta._
import scala.meta.trees.Origin

import scalafix.XtensionOptionPatch
import scalafix.XtensionSeqPatch
Expand Down Expand Up @@ -200,7 +201,11 @@ object ImportPatchOps {
allImports.filter(_.importers.forall(isRemovedImporter))

def remove(toRemove: Tree): Patch = {
if (toRemove.pos == Position.None) return Patch.empty
val isSameInput = (toRemove.origin, ctx.tree.origin) match {
case (a: Origin.Parsed, b: Origin.Parsed) => a.input == b.input
case _ => false
}
if (!isSameInput) return Patch.empty
// Imagine "import a.b, c.d, e.f, g.h" where a.b, c.d and g.h are unused.
// All unused imports are responible to delete their leading comma but
// c.d is additionally responsible for deleting its trailling comma.
Expand Down

0 comments on commit 5f4ef49

Please sign in to comment.