Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions resources/scala-3/Scala3Derives.scala_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
case class Derives3(x: Int) derives ReadWriter

8 changes: 4 additions & 4 deletions src/BreakingChangeDetector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ object BreakingChangeDetector {
initArgs: List[String]
): Boolean =
classInfo.annotations.exists(x =>
x.name == "deriving" && x.args.find(initArgs.contains).isDefined
(x.name == "deriving" || x.name == "derives") && x.args.exists(initArgs.contains)
)
private def listOfFieldsThatDefaultValueWasAdded(
oldClass: ClassInfo,
Expand Down Expand Up @@ -98,15 +98,15 @@ object BreakingChangeDetector {
newClass: ClassInfo
): Boolean = {
val isOldClassContainsSerializable = !(oldClass.annotations
.filter(_.name == "deriving")
.filter(x => x.name == "deriving" || x.name == "derives")
.flatMap(_.args)
.filter(x => serializableClasses.contains(x))
.length == 0)

val oldClassDerivingAnnotations =
oldClass.annotations.filter(_.name == "deriving")
oldClass.annotations.filter(x => x.name == "deriving" || x.name == "derives")
val newClassDerivingAnnotations =
newClass.annotations.find(_.name == "deriving")
newClass.annotations.filter(x => x.name == "deriving" || x.name == "derives")

isOldClassContainsSerializable &&
!oldClassDerivingAnnotations
Expand Down
24 changes: 14 additions & 10 deletions src/FileParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,27 @@ object FileParser {

val tree =
parseTreeClasses(exampleTree)
.map(c =>
(ClassInfo(
.map(c => {
val derivingFromAnnotation =
c.mods
.flatMap(_.children)
.collect { case Init(tpe, _, args) =>
Annotation(tpe.toString, args.flatten.map(_.toString))
}
val derivingFromDerives =
c.templ.derives.map(d => Annotation("derives", List(d.toString)))
ClassInfo(
c.name.value,
c.ctor.paramss.flatten.map(p =>
c.ctor.paramss.headOption.getOrElse(Nil).map(p =>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Field(
p.name.value,
p.decltpe.get.toString,
p.default.map(_.toString)
)
),
c.mods
.flatMap(_.children)
.collect { case Init(tpe, name, args) =>
Annotation(tpe.toString, args.flatten.map(_.toString))
}
))
)
derivingFromAnnotation ++ derivingFromDerives
)
})
ScalaFile(
praseTreeImport(exampleTree),
addSuffixToDuplicates(tree),
Expand Down
19 changes: 19 additions & 0 deletions test/FileParser.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,24 @@ class FileParserTest extends munit.FunSuite {
.getPath
val parsedFile = FileParser.fromPathToClassDef(file)
assert(parsedFile.classes.head.name == "Scala3Class")
// Assert only first param list is treated as fields (using params should be ignored)
assert(parsedFile.classes.head.fields.length == 1)
assert(parsedFile.classes.head.fields(0).name == "a")
assert(parsedFile.classes.head.fields(0).tpe == "Int")
}

test("parses Scala 3 derives annotation") {
val file = Thread
.currentThread()
.getContextClassLoader
.getResource("scala-3/Scala3Derives.scala_test")
.getPath
val parsedFile = FileParser.fromPathToClassDef(file)
assert(parsedFile.classes.head.name == "Derives3")
assert(parsedFile.classes.head.fields.length == 1)
assert(parsedFile.classes.head.fields(0).name == "x")
val derivesAnnotation = parsedFile.classes.head.annotations.find(_.name == "derives")
assert(derivesAnnotation.isDefined)
assert(derivesAnnotation.get.args.contains("ReadWriter"))
}
}