Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better annotation detection #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 8 additions & 7 deletions build.sbt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
ThisBuild / scalaVersion := "3.1.3"

val organizeImportVersion = "0.6.0"
val scalaTestVersion = "3.2.17"
val scalaCheckVersion = "3.2.17.0"
val scalaTestVersion = "3.2.19"
val scalaCheckVersion = "3.2.18.0"

// all LTS versions & latest minor ones
val supportedScalaVersions = List(
"2.12.19",
"2.13.13",
"3.1.3",
"3.2.2",
"3.3.0",
// "2.12.19",
// "2.13.13"
//,
// "3.1.3",
// "3.2.2",
// "3.3.0",
"3.3.1",
"3.3.3",
"3.4.1"
Expand Down
13 changes: 10 additions & 3 deletions plugin/src/main/scala-3/io/github/polentino/redacted/helpers/AstOps.scala
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.polentino.redacted.helpers

import dotty.tools.dotc.*
import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.ast.{Trees, tpd}
import dotty.tools.dotc.core.Constants.Constant
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Symbols.*
Expand All @@ -19,10 +19,17 @@ object AstOps {
extension (symbol: Symbol)(using Context) {

def redactedFields: List[String] = {
val redactedType = redactedSymbol
symbol.primaryConstructor.paramSymss.headOption.fold(List.empty[String]) { params =>
params
.filter(_.annotations.exists(_.matches(redactedType)))
.filter(_.annotations.exists { annotation =>
annotation.tree match {
case Trees.Apply(Trees.TypeApply(qualifier, _), _) =>
qualifier.symbol.maybeOwner.fullName.toString == REDACTED_CLASS
case Trees.Apply(qualifier, _) =>
qualifier.symbol.maybeOwner.fullName.toString == REDACTED_CLASS
case _ => false
}
})
.map(_.name.toString)
}
}
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.9.9
sbt.version = 1.10.7
50 changes: 50 additions & 0 deletions tests/src/test/scala/io/github/polentino/redacted/RedactedSpec.scala
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,56 @@ class RedactedSpec extends AnyFlatSpec with ScalaCheckPropertyChecks {
}
}

it should "work when using its FQDN" in {
final case class ClassWithFQDNAnnot(uuid: UUID, @io.github.polentino.redacted.redacted name: String, age: Int)
forAll { (uuid:UUID, name: String, age: Int) =>
val expected = s"ClassWithFQDNAnnot(${uuid.toString},***,$age)"
val testing = ClassWithFQDNAnnot(uuid, name, age)
val implicitToString = s"$testing"
val explicitToString = testing.toString

val cp = new Checkpoint
cp { assert(implicitToString == expected) }
cp { assert(explicitToString == expected) }
cp { assert(testing.name == name) }
cp.reportAll()
}
}

it should "work when using an alias" in {
import io.github.polentino.redacted.{redacted => testAnnotation}
final case class ClassWithAlias(uuid: UUID, @testAnnotation name: String, age: Int)
forAll { (uuid:UUID, name: String, age: Int) =>
val expected = s"ClassWithAlias(${uuid.toString},***,$age)"
val testing = ClassWithAlias(uuid, name, age)
val implicitToString = s"$testing"
val explicitToString = testing.toString

val cp = new Checkpoint
cp { assert(implicitToString == expected) }
cp { assert(explicitToString == expected) }
cp { assert(testing.name == name) }
cp.reportAll()
}
}

it should "not work when using different `redacted` annotations" in {
import some.fakepkg.redacted
final case class ClassWithAlias(uuid: UUID, @redacted name: String, age: Int)
forAll { (uuid:UUID, name: String, age: Int) =>
val expected = s"ClassWithAlias(${uuid.toString},$name,$age)"
val testing = ClassWithAlias(uuid, name, age)
val implicitToString = s"$testing"
val explicitToString = testing.toString

val cp = new Checkpoint
cp { assert(implicitToString == expected) }
cp { assert(explicitToString == expected) }
cp { assert(testing.name == name) }
cp.reportAll()
}
}

it must "not change the behavior of `hashCode`" in {
final case class TestClass(uuid: UUID, name: String, age: Int)
object RedactedTestClass {
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions tests/src/test/scala/some/fakepkg/redacted.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package some.fakepkg

import scala.annotation.StaticAnnotation

class redacted extends StaticAnnotation
Loading