From b9574c06950c6f36fa1da46a1249ea28713209e6 Mon Sep 17 00:00:00 2001 From: Connor James Smith Date: Sat, 20 Jan 2024 06:20:37 -0500 Subject: [PATCH] [scalafix] tagChunkSize migration fix --- .../input/src/main/scala/fix/Fs2Pgp.scala | 1 + .../output/src/main/scala/fix/Fs2Pgp.scala | 1 + .../com/dwolla/security/crypto/V04to05.scala | 20 +++++++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/scalafix/input/src/main/scala/fix/Fs2Pgp.scala b/scalafix/input/src/main/scala/fix/Fs2Pgp.scala index 5d753009..76d92bac 100644 --- a/scalafix/input/src/main/scala/fix/Fs2Pgp.scala +++ b/scalafix/input/src/main/scala/fix/Fs2Pgp.scala @@ -46,6 +46,7 @@ object Fs2Pgp { |-----END PGP PUBLIC KEY BLOCK-----""".stripMargin val wrappedKey = PGPKeyAlg[IO].readPublicKey(key).unsafeRunSync() val pos = PosInt(100) + val chunkSize = tagChunkSize(pos) (for { crypto <- Stream.resource(CryptoAlg[IO]) diff --git a/scalafix/output/src/main/scala/fix/Fs2Pgp.scala b/scalafix/output/src/main/scala/fix/Fs2Pgp.scala index 65b8e313..b2d2446e 100644 --- a/scalafix/output/src/main/scala/fix/Fs2Pgp.scala +++ b/scalafix/output/src/main/scala/fix/Fs2Pgp.scala @@ -44,6 +44,7 @@ object Fs2Pgp { |-----END PGP PUBLIC KEY BLOCK-----""".stripMargin val wrappedKey = PGPKeyAlg[IO].readPublicKey(key).unsafeRunSync() val pos = PosInt(100) + val chunkSize = ChunkSize(pos) (for { crypto <- Stream.resource(CryptoAlg.resource[IO]) diff --git a/scalafix/rules/src/main/scala/com/dwolla/security/crypto/V04to05.scala b/scalafix/rules/src/main/scala/com/dwolla/security/crypto/V04to05.scala index 9b7cd83d..156bc3ab 100644 --- a/scalafix/rules/src/main/scala/com/dwolla/security/crypto/V04to05.scala +++ b/scalafix/rules/src/main/scala/com/dwolla/security/crypto/V04to05.scala @@ -16,9 +16,25 @@ class V04to05 extends SemanticRule("com.dwolla.security.crypto.V04to05") { 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") + case t@Term.Apply.After_4_6_0(Term.Name("tagChunkSize"), _) if !isEncryptAParent(t) => + Patch.replaceToken(t.tokens.head, "ChunkSize") }.asPatch + + + private def isEncryptAParent(t: Tree): Boolean = { + val res = for { + firstParent <- t.parent + secondParent <- firstParent.parent + res = secondParent match { + case Term.Apply.After_4_6_0(Term.Select(_, Term.Name("encrypt")), _) => true + case _ => false + } + } yield res + res match { + case None => false + case Some(v) => v + } + } private def migrateEncrypt(t: Term.ArgClause, fun: Term.Name,