Skip to content

Commit

Permalink
Write test and simplify rule
Browse files Browse the repository at this point in the history
  • Loading branch information
clairemcginty committed Jul 25, 2024
1 parent 8960daf commit aabb5c7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 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")
}
13 changes: 1 addition & 12 deletions scalafix/rules/src/main/scala/fix/v0_14_0/FixQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ import scala.meta._
object FixQuery {
val HasQueryMatcher: SymbolMatcher =
SymbolMatcher.normalized("com/spotify/scio/bigquery/types/BigQueryType/HasQuery")

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

class FixQuery extends SemanticRule("FixQuery") {

import FixQuery._

private def isQuery(term: Tree)(implicit doc: SemanticDocument): Boolean =
term.symbol.info.exists(_.overriddenSymbols.exists(HasQueryMethodMatcher.matches))

private def isQueryFormat(term: Tree)(implicit doc: SemanticDocument): Boolean =
term.symbol.info.map(_.signature).exists {
case ClassSignature(_, parents, _, _) =>
Expand All @@ -30,12 +23,8 @@ class FixQuery extends SemanticRule("FixQuery") {

override def fix(implicit doc: SemanticDocument): Patch = {
doc.tree.collect {
case t @ q"$qual.$fn" if HasQueryMethodMatcher.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)
case t @ q"$qual.query.format" if isQueryFormat(qual) =>
Patch.replaceTree(t, q"$qual.queryRaw.format".syntax)
}.asPatch
}

Expand Down

0 comments on commit aabb5c7

Please sign in to comment.