Skip to content

Commit

Permalink
Improve accuracy of FixQuery rule (#5438)
Browse files Browse the repository at this point in the history
Co-authored-by: Michel Davit <[email protected]>
  • Loading branch information
clairemcginty and RustedBones authored Jul 25, 2024
1 parent ce2825a commit 0cc95ac
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
19 changes: 13 additions & 6 deletions scalafix/input-0_14/src/main/scala/fix/v0_14_0/FixQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ rule = FixQuery
*/
package fix.v0_14_0

import com.spotify.scio.bigquery.types.BigQueryType.{HasQuery, Query}
import com.spotify.scio.bigquery.types.BigQueryType.HasQuery

object FixQuery {
// Workaround to get 0.14 code to compile (query method has been removed from HasQuery)
trait HasQueryRaw {
def query: String = ???
}

val a: HasQuery = ???
val b: Query[_] = ???
object HasQueryType extends HasQuery with HasQueryRaw {
override def queryRaw: String = ???
override def query: String = ???
}

object FixQuery {
HasQueryType.query

a.query
b.query
HasQueryType.query.format("foo")
}
19 changes: 13 additions & 6 deletions scalafix/output-0_14/src/main/scala/fix/v0_14_0/FixQuery.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package fix.v0_14_0

import com.spotify.scio.bigquery.types.BigQueryType.{HasQuery, Query}
import com.spotify.scio.bigquery.types.BigQueryType.HasQuery

object FixQuery {
// Workaround to get 0.14 code to compile (query method has been removed from HasQuery)
trait HasQueryRaw {
def query: String = ???
}

val a: HasQuery = ???
val b: Query[_] = ???
object HasQueryType extends HasQuery with HasQueryRaw {
override def queryRaw: String = ???
override def query: String = ???
}

object FixQuery {
HasQueryType.queryRaw

a.queryRaw
b.queryRaw
HasQueryType.queryRaw.format("foo")
}
18 changes: 11 additions & 7 deletions scalafix/rules/src/main/scala/fix/v0_14_0/FixQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ import scala.meta._

object FixQuery {
val HasQueryMatcher: SymbolMatcher =
SymbolMatcher.normalized("com/spotify/scio/bigquery/types/BigQueryType/HasQuery#query")
SymbolMatcher.normalized("com/spotify/scio/bigquery/types/BigQueryType/HasQuery")
}

class FixQuery extends SemanticRule("FixQuery") {

import FixQuery._

private def isQuery(term: Tree)(implicit doc: SemanticDocument): Boolean =
term.symbol.info.exists(_.overriddenSymbols.exists(HasQueryMatcher.matches))
private def isQueryFormat(term: Tree)(implicit doc: SemanticDocument): Boolean =
term.symbol.info.map(_.signature).exists {
case ClassSignature(_, parents, _, _) =>
parents.exists {
case TypeRef(_, symbol, _) if HasQueryMatcher.matches(symbol) => true
case _ => false
}
case _ => false
}

override def fix(implicit doc: SemanticDocument): Patch = {
doc.tree.collect {
case t @ q"$qual.$fn" if HasQueryMatcher.matches(fn) =>
Patch.replaceTree(t, q"$qual.queryRaw".syntax)
case t @ q"$qual.query" if isQuery(t) =>
case t @ q"$qual.query" if isQueryFormat(qual) =>
Patch.replaceTree(t, q"$qual.queryRaw".syntax)
}.asPatch
}
Expand Down

0 comments on commit 0cc95ac

Please sign in to comment.