Skip to content
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

Improve accuracy of FixQuery rule #5438

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 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,6 +5,9 @@ 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")
}

Expand All @@ -13,14 +16,26 @@ class FixQuery extends SemanticRule("FixQuery") {
import FixQuery._

private def isQuery(term: Tree)(implicit doc: SemanticDocument): Boolean =
term.symbol.info.exists(_.overriddenSymbols.exists(HasQueryMatcher.matches))
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, _, _) =>
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) =>
case t @ q"$qual.$fn" if HasQueryMethodMatcher.matches(fn) =>
Patch.replaceTree(t, q"$qual.queryRaw".syntax)
case t @ q"$qual.query" if isQuery(t) =>
Patch.replaceTree(t, q"$qual.queryRaw".syntax)
case t @ q"$qual.query.format" if isQueryFormat(qual) =>
Patch.replaceTree(t, q"$qual.queryRaw.format".syntax)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we try to directly use query(..) in that case ?

Suggested change
Patch.replaceTree(t, q"$qual.queryRaw.format".syntax)
Patch.replaceTree(t, q"$qual.query".syntax)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplified it to single case matching query directly 👍

}.asPatch
}

Expand Down
Loading