forked from lightbend-labs/scala-rewrites
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ExplicitNonNullaryApply: workaround for scalameta/scalameta#1083
and scalacenter/scalafix#1104 Given ``` trait Test { def shouldBe(r: Any) = ??? def arg() = "" this shouldBe (arg) } ``` Without changes in this commit then `this shouldBe (arg)` + will not be rewritten if scalacenter/scalafix#1104 is not fixed + will be rewritten to invalid code `this shouldBe (arg)()` if scalacenter/scalafix#1104 is fixed but scalameta/scalameta#1083 is not fixed
- Loading branch information
Showing
4 changed files
with
42 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package scalafix.v1 | ||
|
||
import scala.meta._ | ||
|
||
/** workaround for: | ||
* + https://github.com/scalameta/scalameta/issues/1083 | ||
* + https://github.com/scalacenter/scalafix/issues/1104 */ | ||
object Workaround1104 { | ||
/** Same as [[scalafix.v1.XtensionTreeScalafix.symbol]] | ||
* but because of scalameta/scalameta#1083, | ||
* sometimes `XtensionTreeScalafix.symbol` return `Symbol.None`` | ||
* In that case, we retry getting symbols at pos `name.pos`` */ | ||
def symbol(name: Name)(implicit doc: SemanticDocument): Symbol = | ||
doc.internal.symbol(name) match { | ||
case Symbol.None if needWorkaround(name) => | ||
doc.internal.symbols(name.pos) | ||
.toStream.headOption // Discard multi symbols | ||
.getOrElse(Symbol.None) | ||
case sym => sym | ||
} | ||
|
||
def needWorkaround(name: Name): Boolean = { | ||
val s = name.syntax | ||
s.startsWith("(") && s.endsWith(")") && s != name.value | ||
} | ||
|
||
/** @return if the token `name` instead of `)` for `(name)` */ | ||
def lastToken(name: Name): Token = { | ||
val tokens = name.tokens | ||
if (needWorkaround(name)) tokens(tokens.length - 2) | ||
else tokens.last | ||
} | ||
} |