Skip to content

Commit

Permalink
wip but fixing tagChunkSize will require a separate rule
Browse files Browse the repository at this point in the history
  • Loading branch information
bpholt committed Jan 19, 2024
1 parent 5c9dec8 commit 4fbe7c8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
3 changes: 1 addition & 2 deletions scalafix/input/src/main/scala/fix/Fs2Pgp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ object Fs2Pgp {
|-----END PGP PUBLIC KEY BLOCK-----""".stripMargin
val wrappedKey = PGPKeyAlg[IO].readPublicKey(key).unsafeRunSync()
val pos = PosInt(100)
val chunkSize = tagChunkSize(PosInt(100))
val chunkSize2 = tagChunkSize(pos)

(for {
crypto <- Stream.resource(CryptoAlg[IO])
output <- Stream.emit("hello world")
Expand Down
3 changes: 1 addition & 2 deletions scalafix/output/src/main/scala/fix/Fs2Pgp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ object Fs2Pgp {
|-----END PGP PUBLIC KEY BLOCK-----""".stripMargin
val wrappedKey = PGPKeyAlg[IO].readPublicKey(key).unsafeRunSync()
val pos = PosInt(100)
val chunkSize = ChunkSize(PosInt(100))
val chunkSize2 = ChunkSize(pos)

(for {
crypto <- Stream.resource(CryptoAlg.resource[IO])
output <- Stream.emit("hello world")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,29 @@ class V04to05 extends SemanticRule("com.dwolla.security.crypto.V04to05") {

override def fix(implicit doc: SemanticDocument): Patch =
doc.tree.collect {
case t@Term.Apply.After_4_6_0(Term.Name("tagChunkSize"), _) =>
Patch.renameSymbol(t.symbol, "ChunkSize")
case t@Term.ApplyType.After_4_6_0(Term.Name("CryptoAlg"), Type.ArgClause(List(Type.Name(name)))) =>
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)) =>
Patch.replaceTree(t, s"$name.armor").atomic
case t@Term.Apply.After_4_6_0(
Term.Select(Term.Name(algName), fun@Term.Name("encrypt")),
Term.ArgClause((keyName@Term.Name(_)) :: additionalArguments, None)
) =>
migrateEncrypt(t, fun, additionalArguments, algName, Some(keyName), offset = 1)
case t@Term.Apply.After_4_6_0(
Term.Select(Term.Name(algName), fun@Term.Name("encrypt")),
Term.ArgClause(arguments, None)
) =>
migrateEncrypt(t, fun, arguments, algName, None, offset = 0)
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"), _) =>
// Patch.replaceToken(t.tokens.head, "ChunkSizeFromFix")
}.asPatch

private def migrateEncrypt(t: Term,
private def migrateEncrypt(t: Term.ArgClause,
fun: Term.Name,
arguments: List[Term],
algName: String,
keyName: Option[Term],
offset: Int,
)
(implicit doc: SemanticDocument): Patch = {
val map = arguments.zipWithIndex.foldLeft(keyName.map("key" -> _).toList) {
case (s, (Term.Assign(Term.Name("key"), term), _)) => s :+ ("key" -> term)
case (s, (Term.Assign(Term.Name("chunkSize"), Term.Apply.After_4_6_0(Term.Name("tagChunkSize"), Term.ArgClause(List(term), _))), _)) =>
s :+ ("ChunkSize" -> term)
s :+ ("ChunkSizeFromMigrateEncrypt" -> term)
case (s, (Term.Assign(Term.Name(name), term), _)) => s :+ (name.capitalize -> term)
case (s, (value, i)) =>
fun.symbol.info match {
Expand All @@ -47,26 +40,45 @@ class V04to05 extends SemanticRule("com.dwolla.security.crypto.V04to05") {
val parameter = method.parameterLists.head(i + offset)
val parameterName = parameter.displayName

s :+ (parameterName.capitalize -> (value match {
case Term.Apply.After_4_6_0(Term.Name("tagChunkSize"), Term.ArgClause(List(term), _)) =>
Term.Apply(Term.Name("ChunkSize"), Term.ArgClause(List(term)))
case _ => value
}))
s :+ (parameterName.capitalize -> value)
}
case _ => s
}
case (s, _) => s
}

val key = map.toMap.apply("key")
// TODO maybe try replacing the specific arguments instead of the entire tree?
// import scala.meta.tokens.Token.{Comma, Space}
// Patch.addLeft(arguments.head, "EncryptionConfig()") +
// sortKeyToEnd(map)
// .map {
// case ("key", t) => Patch.removeTokens(t.tokens)
// case (s, arg) =>
// t.tokens.foreach(t => println(s"${t.getClass} -> $t"))
// Patch.addAround(arg, s".with$s(", ")")
// }
// .asPatch +
// Patch.removeTokens(t.tokens.filter(_.is[Comma])) +
// Patch.addRight(arguments.last, s", ${map.toMap.apply("key")}")

// TODO maybe try replacing the specific arguments instead of the entire tree?
Patch.replaceTree(t, map.filterNot {
case ("key", _) => true
case _ => false
}.foldLeft(s"$algName.encrypt(EncryptionConfig()") {
Patch.replaceTree(t, sortKeyToEnd(map).foldLeft(s"(EncryptionConfig()") {
case (s, ("key", term)) => s + s", $term)"
case (s, (argName, Term.Apply.After_4_6_0(Term.Name("tagChunkSize"), Term.ArgClause(List(term), _)))) => s + s".with$argName(ChunkSize($term))"
case (s, (argName, term)) => s + s".with$argName($term)"
} + s", $key)")
})
}

private def sortKeyToEnd(terms: List[(String, Term)]): List[(String, Term)] = {
val key = terms.toMap.apply("key")

val remainder = terms
.filterNot {
case ("key", _) => true
case _ => false
}

remainder :+ ("key" -> key)
}


}

0 comments on commit 4fbe7c8

Please sign in to comment.