-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from brbrown25/ISSUE-128
feat(cats-effect): Adding in fix for UUID.randomUUID -> IO.randomUUID…
- Loading branch information
Showing
6 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
modules/cats-effect/input/src/main/scala/fix/IORandomUUIDTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
rule = TypelevelIORandomUUID | ||
*/ | ||
package fix | ||
|
||
import cats.effect.* | ||
import java.util.UUID | ||
|
||
object IORandomUUIDTests { | ||
IO.randomUUID | ||
IO(UUID.randomUUID()) | ||
IO.blocking(UUID.randomUUID()) | ||
val test = IO(UUID.randomUUID()) | ||
for { | ||
ioa <- IO(UUID.randomUUID()) | ||
iob = IO(UUID.randomUUID()) | ||
} yield (ioa, iob) | ||
def generateUUID1(): IO[UUID] = { | ||
IO.blocking(UUID.randomUUID()) | ||
} | ||
def generateUUID2(): IO[UUID] = { | ||
IO.randomUUID | ||
} | ||
def generateUUID3(): IO[UUID] = { | ||
IO(UUID.randomUUID()) | ||
} | ||
def generateUUID4(): IO[(UUID, IO[UUID])] = { | ||
for { | ||
ioa <- IO(UUID.randomUUID()) | ||
iob = IO(UUID.randomUUID()) | ||
} yield (ioa, iob) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
modules/cats-effect/output/src/main/scala/fix/IORandomUUIDTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package fix | ||
|
||
import cats.effect.* | ||
import java.util.UUID | ||
|
||
object IORandomUUIDTests { | ||
IO.randomUUID | ||
IO.randomUUID | ||
IO.randomUUID | ||
val test = IO.randomUUID | ||
for { | ||
ioa <- IO.randomUUID | ||
iob = IO.randomUUID | ||
} yield (ioa, iob) | ||
def generateUUID1(): IO[UUID] = { | ||
IO.randomUUID | ||
} | ||
def generateUUID2(): IO[UUID] = { | ||
IO.randomUUID | ||
} | ||
def generateUUID3(): IO[UUID] = { | ||
IO.randomUUID | ||
} | ||
def generateUUID4(): IO[(UUID, IO[UUID])] = { | ||
for { | ||
ioa <- IO.randomUUID | ||
iob = IO.randomUUID | ||
} yield (ioa, iob) | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
modules/cats-effect/rules/src/main/resources/META-INF/services/scalafix.v1.Rule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
org.typelevel.fix.UnusedIO | ||
org.typelevel.fix.UnusedIO | ||
org.typelevel.fix.IORandomUUID |
76 changes: 76 additions & 0 deletions
76
modules/cats-effect/rules/src/main/scala/org/typelevel/fix/IORandomUUID.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2022 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.fix | ||
|
||
import scala.meta.* | ||
import scalafix.v1.* | ||
|
||
class IORandomUUID extends SemanticRule("TypelevelIORandomUUID") { | ||
def checkSignature(parent: Stat, tree: Term)(implicit doc: SemanticDocument): Patch = { | ||
tree.symbol.info.flatMap { info => | ||
info.signature match { | ||
case MethodSignature(_, _, TypeRef(_, sym, _)) | ||
if IOSym.matches(sym) && containsJavaUUID(parent) => | ||
Some(Patch.replaceTree(parent, "IO.randomUUID")) | ||
case _ => | ||
None | ||
} | ||
}.asPatch | ||
} | ||
|
||
def containsJavaUUID(outer: Stat)(implicit doc: SemanticDocument): Boolean = { | ||
outer.children.exists(_.children.map(_.symbol).exists(JavaUUIDSym.matches)) | ||
} | ||
|
||
def checkDiscardedStat(outer: Stat)(implicit doc: SemanticDocument): Patch = { | ||
def checkInner(stat: Stat): Patch = { | ||
lazy val self: PartialFunction[Stat, Patch] = { | ||
case Term.Apply.Initial(fn @ Term.Name(_), _) | ||
if IOCompanionSym.matches(fn) && containsJavaUUID(outer) => | ||
Patch.replaceTree(outer, "IO.randomUUID").atomic | ||
case Term.Apply.Initial(Term.Select(_, method), _) => | ||
checkSignature(outer, method) | ||
} | ||
|
||
self.lift(stat).asPatch | ||
} | ||
|
||
checkInner(outer) | ||
} | ||
|
||
override def fix(implicit doc: SemanticDocument): Patch = { | ||
doc.tree.collect { | ||
case Term.Block(body) => | ||
body.map(checkDiscardedStat).asPatch | ||
case Defn.Val(_, _, _, expr) => | ||
checkDiscardedStat(expr) | ||
case Template.Initial(_, _, _, stats) => | ||
stats.map(checkDiscardedStat).asPatch | ||
case Term.ForYield(enums, _) => | ||
enums.collect { | ||
case Enumerator.Generator(_, rhs) => | ||
checkDiscardedStat(rhs) | ||
case Enumerator.Val(_, rhs) => | ||
checkDiscardedStat(rhs) | ||
}.asPatch | ||
}.asPatch | ||
} | ||
|
||
val IOSym = SymbolMatcher.exact("cats/effect/IO#") | ||
val IOCompanionSym = SymbolMatcher.exact("cats/effect/IO.") | ||
val JavaUUIDSym = SymbolMatcher.exact("java/util/UUID#randomUUID().") | ||
} |