Skip to content

Commit

Permalink
narrow scalafix selectors so they only operate on our code
Browse files Browse the repository at this point in the history
  • Loading branch information
bpholt committed Feb 1, 2024
1 parent d171fda commit 1892deb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
11 changes: 11 additions & 0 deletions scalafix/input/src/main/scala/example/ShouldBeIgnored.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package example
/*
rule = com.dwolla.security.crypto.V04to05
*/
trait Foo {
def encrypt(request: Int): Unit
}

class ShouldBeIgnored(foo: Foo) {
def go(): Unit = foo.encrypt(42)
}
9 changes: 9 additions & 0 deletions scalafix/output/src/main/scala/example/ShouldBeIgnored.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package example

trait Foo {
def encrypt(request: Int): Unit
}

class ShouldBeIgnored(foo: Foo) {
def go(): Unit = foo.encrypt(42)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import scalafix.v1.*

import scala.annotation.tailrec
import scala.meta.*
import scala.meta.inputs.Input.{File, VirtualFile}

class V04to05 extends SemanticRule("com.dwolla.security.crypto.V04to05") {

override def fix(implicit doc: SemanticDocument): Patch =
doc.tree.collect {
case t@Term.ApplyType.After_4_6_0(Term.Name("CryptoAlg"), Type.ArgClause(List(Type.Name(name)))) =>
case t@Term.ApplyType.After_4_6_0(Term.Name("CryptoAlg"), Type.ArgClause(List(Type.Name(name)))) if t.symbol.normalized.value == "com.dwolla.security.crypto.CryptoAlg." =>
Patch.replaceTree(t, s"CryptoAlg.resource[$name]").atomic
case t@Term.Apply.After_4_6_0(Term.Select(Term.Name(name), Term.Name("armor")), Term.ArgClause(List(), None)) =>
case t@Term.Apply.After_4_6_0(Term.Select(Term.Name(name), Term.Name("armor")), Term.ArgClause(List(), None)) if t.symbol.normalized.value == "com.dwolla.security.crypto.CryptoAlg.armor." =>
Patch.replaceTree(t, s"$name.armor").atomic
case Term.Apply.After_4_6_0(Term.Select(Term.Name(_), fun@Term.Name("encrypt")), t@Term.ArgClause((keyName@Term.Name(_)) :: additionalArguments, None)) =>
migrateEncrypt(t, fun, additionalArguments, Some(keyName), offset = 1)
case Term.Apply.After_4_6_0(Term.Select(Term.Name(_), fun@Term.Name("encrypt")), t@Term.ArgClause(arguments, None)) =>
migrateEncrypt(t, fun, arguments, None, offset = 0)
case t@Term.Apply.After_4_6_0(Term.Name("tagChunkSize"), _) if !isEncryptAParent(t) =>
case t@Term.Apply.After_4_6_0(Term.Select(Term.Name(_), fun@Term.Name("encrypt")), argClause@Term.ArgClause((keyName@Term.Name(_)) :: additionalArguments, None)) if t.symbol.normalized.value == "com.dwolla.security.crypto.CryptoAlg.encrypt." =>
migrateEncrypt(argClause, fun, additionalArguments, Some(keyName), offset = 1)
case t@Term.Apply.After_4_6_0(Term.Select(Term.Name(_), fun@Term.Name("encrypt")), argClause@Term.ArgClause(arguments, None)) if t.symbol.normalized.value == "com.dwolla.security.crypto.CryptoAlg.encrypt." =>
migrateEncrypt(argClause, fun, arguments, None, offset = 0)
case t@Term.Apply.After_4_6_0(Term.Name("tagChunkSize"), _) if !isEncryptAParent(t) && t.symbol.normalized.value == "com.dwolla.security.crypto.package.tagChunkSize." =>
Patch.replaceToken(t.tokens.head, "ChunkSize")
}.asPatch

Expand Down Expand Up @@ -50,7 +51,16 @@ class V04to05 extends SemanticRule("com.dwolla.security.crypto.V04to05") {
val parameterName = parameter.displayName

s :+ (parameterName.capitalize -> value)
case _ => throw new RuntimeException(s"Unexpected method signature ${info.signature} (how did the code being modified ever compile?)")
case method: MethodSignature =>
println(method.parameterLists)
fun.pos.input match {
case File(path, _) =>
throw new RuntimeException(s"Unexpected method signature ${info.signature} at $path:${fun.pos.startLine + 1}:${fun.pos.startColumn + 1}${fun.pos.endLine + 1}:${fun.pos.endColumn + 1}")
case VirtualFile(path, _) =>
throw new RuntimeException(s"Unexpected method signature ${info.signature} at $path:${fun.pos.startLine + 1}:${fun.pos.startColumn + 1}${fun.pos.endLine + 1}:${fun.pos.endColumn + 1}")
case _ =>
throw new RuntimeException(s"Unexpected method signature ${info.signature} at ${fun.pos}")
}
}
case _ => s
}
Expand Down

0 comments on commit 1892deb

Please sign in to comment.