<commit_before>package fix
package v0_7_0

import scalafix.v1._
import scala.meta._

class BQClientRefactoring extends SyntacticRule("BQClientRefactoring") {
  private val imports =
    scala.collection.mutable.ArrayBuffer.empty[(String, String)]

  // Check that the package is not imported multiple times in the same file
  def addImport(p: Position, i: Importer) = {
    val Importer(s) = i
    val Input.VirtualFile(path, _) = p.input

    val t = (s.toString, path)
    if(!imports.contains(t)) {
      imports += t
      Patch.addGlobalImport(i)
    } else Patch.empty
  }

  object BQDef {
    def unapply(t: Tree) =
      t match {
        case Term.Apply(Term.Select(_, t @ Term.Name(c)), _) =>
          Option((t, c))
        case _ => None
      }
  }

  override def fix(implicit doc: SyntacticDocument): Patch = {
    doc.tree.collect {
      case i @ Importee.Name(Name.Indeterminate("BigQueryClient")) =>
        Patch.removeImportee(i) + addImport(i.pos, importer"com.spotify.scio.bigquery.client.BigQuery")
      case BQDef(t, "extractLocation" | "extractTables") =>
        Patch.addLeft(t, "query.")
      case Term.Apply(Term.Select(n @ Term.Name("BigQueryClient"), Term.Name("defaultInstance")), _) =>
        Patch.replaceTree(n, "BigQuery")
      case BQDef(t, "getQuerySchema") =>
        Patch.replaceTree(t, "query.schema")
      case BQDef(t, "getQueryRows") =>
        Patch.replaceTree(t, "query.rows")
      case BQDef(t, "getTableSchema") =>
        Patch.replaceTree(t, "tables.schema")
      case BQDef(t, "createTable") =>
        Patch.replaceTree(t, "tables.create")
      case BQDef(t, "getTable") =>
        Patch.replaceTree(t, "tables.table")
      case BQDef(t, "getTables") =>
        Patch.replaceTree(t, "tables.tableReferences")
      case BQDef(t, "getTableRows") =>
        Patch.replaceTree(t, "tables.rows")
      case ap @ BQDef(t, "loadTableFromCsv") =>
        Patch.addRight(ap, ".get") + Patch.replaceTree(t, "load.csv")
      case ap @ BQDef(t, "loadTableFromJson") =>
        Patch.addRight(ap, ".get") + Patch.replaceTree(t, "load.json")
      case ap @ BQDef(t, "loadTableFromAvro") =>
        Patch.addRight(ap, ".get") + Patch.replaceTree(t, "load.avro")
      case BQDef(t, "exportTableAsCsv") =>
        Patch.replaceTree(t, "extract.asCsv")
      case BQDef(t, "exportTableAsJson") =>
        Patch.replaceTree(t, "extract.asJson")
      case BQDef(t, "exportTableAsAvro") =>
        Patch.replaceTree(t, "extract.asAvro")
      case c if c.toString.contains("BigQueryClient") =>
        Patch.empty
    }.asPatch
  }
}<commit_msg>Improve BQ refactoring scalafix rule (#1470)<commit_after>package fix
package v0_7_0

import scalafix.v1._
import scala.meta._

class BQClientRefactoring extends SyntacticRule("BQClientRefactoring") {
  private val imports =
    scala.collection.mutable.ArrayBuffer.empty[(String, String)]

  // Check that the package is not imported multiple times in the same file
  def addImport(p: Position, i: Importer) = {
    val Importer(s) = i
    val Input.VirtualFile(path, _) = p.input

    val t = (s.toString, path)
    if(!imports.contains(t)) {
      imports += t
      Patch.addGlobalImport(i)
    } else Patch.empty
  }

  object BQDef {
    def unapply(t: Tree) =
      t match {
        case Term.Apply(Term.Select(_, t @ Term.Name(c)), _) =>
          Option((t, c))
        case _ => None
      }
  }

  def addBQImport(i: Tree) =
    addImport(i.pos, importer"com.spotify.scio.bigquery.client.BigQuery")

  override def fix(implicit doc: SyntacticDocument): Patch = {
    doc.tree.collect {
      case i @ Importee.Name(Name.Indeterminate("BigQueryClient")) =>
        Patch.removeImportee(i) + addBQImport(i)
      case BQDef(t, "extractLocation" | "extractTables") =>
        Patch.addLeft(t, "query.") + addBQImport(t)
      case Term.Apply(Term.Select(n @ Term.Name("BigQueryClient"), Term.Name("defaultInstance")), _) =>
        Patch.replaceTree(n, "BigQuery") + addBQImport(n)
      case BQDef(t, "getQuerySchema") =>
        Patch.replaceTree(t, "query.schema") + addBQImport(t)
      case BQDef(t, "getQueryRows") =>
        Patch.replaceTree(t, "query.rows") + addBQImport(t)
      case BQDef(t, "getTableSchema") =>
        Patch.replaceTree(t, "tables.schema") + addBQImport(t)
      case BQDef(t, "createTable") =>
        Patch.replaceTree(t, "tables.create") + addBQImport(t)
      case BQDef(t, "getTable") =>
        Patch.replaceTree(t, "tables.table") + addBQImport(t)
      case BQDef(t, "getTables") =>
        Patch.replaceTree(t, "tables.tableReferences") + addBQImport(t)
      case BQDef(t, "getTableRows") =>
        Patch.replaceTree(t, "tables.rows") + addBQImport(t)
      case ap @ BQDef(t, "loadTableFromCsv") =>
        Patch.addRight(ap, ".get") + Patch.replaceTree(t, "load.csv") + addBQImport(t)
      case ap @ BQDef(t, "loadTableFromJson") =>
        Patch.addRight(ap, ".get") + Patch.replaceTree(t, "load.json") + addBQImport(t)
      case ap @ BQDef(t, "loadTableFromAvro") =>
        Patch.addRight(ap, ".get") + Patch.replaceTree(t, "load.avro") + addBQImport(t)
      case BQDef(t, "exportTableAsCsv") =>
        Patch.replaceTree(t, "extract.asCsv") + addBQImport(t)
      case BQDef(t, "exportTableAsJson") =>
        Patch.replaceTree(t, "extract.asJson") + addBQImport(t)
      case BQDef(t, "exportTableAsAvro") =>
        Patch.replaceTree(t, "extract.asAvro") + addBQImport(t)
      case c if c.toString.contains("BigQueryClient") =>
        Patch.empty
    }.asPatch
  }
}
{'fraction_non_alphanumeric': 0.1509502262443439, 'fraction_numerical': 0.0021719457013574662, 'mean_word_length': 3.1300448430493275, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5829503', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1910, 'n_words': 366}