Skip to content

Commit

Permalink
update DiscardValue
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Mar 22, 2024
1 parent 98f97c6 commit 7edc336
Showing 1 changed file with 76 additions and 38 deletions.
114 changes: 76 additions & 38 deletions rules/src/main/scala/fix/DiscardValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import metaconfig.Configured
import metaconfig.generic.Surface
import scala.meta.Decl
import scala.meta.Defn
import scala.meta.Name
import scala.meta.Stat
import scala.meta.Template
import scala.meta.Term
import scala.meta.Tree
import scala.meta.XtensionCollectionLikeUI
import scala.meta.XtensionClassifiable
import scala.meta.contrib.XtensionTreeOps
import scalafix.Patch
import scalafix.lint.Diagnostic
Expand Down Expand Up @@ -118,45 +120,81 @@ object DiscardValue {
severity: LintSeverity,
filter: SemanticType => Boolean
)(implicit doc: SemanticDocument): Patch = {
doc.tree.collect { case BlockOrTemplate(values :+ _) => // ignore last
values.filter {
case _: Defn | _: Term.Assign | _: Decl =>
false
case x =>
x.collectFirst {
case Term.Apply.After_4_6_0(Term.Select(Mockito(), _), _) =>
()
case Term.Apply.After_4_6_0(MockitoVerify(), _) =>
()
case Term.Apply.After_4_6_0(
Term.Select(MockitoInOrder(), Term.Name("verify")),
_
) =>
()
}.isEmpty
}.flatMap(x => x.symbol.info.map(x -> _))
.map { case (x, info) =>
PartialFunction
.condOpt(info.signature) {
case m: MethodSignature =>
m.returnType
case v: ValueSignature =>
v.tpe
}
.filter(filter)
.map { tpe =>
Patch.lint(
Diagnostic(
id = "",
message = message(tpe),
position = x.pos,
severity = severity,
doc.tree.collect {
case BlockOrTemplate(values :+ _) => // ignore last
values.filter {
case _: Defn | _: Term.Assign | _: Decl =>
false
case x =>
x.collectFirst {
case Term.Apply.After_4_6_0(Term.Select(Mockito(), _), _) =>
()
case Term.Apply.After_4_6_0(MockitoVerify(), _) =>
()
case Term.Apply.After_4_6_0(
Term.Select(MockitoInOrder(), Term.Name("verify")),
_
) =>
()
}.isEmpty

}.flatMap(x => x.symbol.info.map(x -> _))
.map { case (x, info) =>
PartialFunction
.condOpt(info.signature) {
case m: MethodSignature =>
m.returnType
case v: ValueSignature =>
v.tpe
}
.filter(filter)
.map { tpe =>
Patch.lint(
Diagnostic(
id = "",
message = message(tpe),
position = x.pos,
severity = severity,
)
)
)
}
.asPatch
}
.asPatch
}
.asPatch
}
.asPatch
case Term.Function.After_4_6_0(
Term.ParamClause(
params,
None
),
body
) =>
params.collect {
case p
if p.mods.isEmpty &&
p.name.is[Name.Placeholder] || body.collect { case t: Term.Name =>
t.value == p.name.value
}.isEmpty =>
p.name.symbol.info
.map(_.signature)
.collect {
case m: MethodSignature =>
m.returnType
case v: ValueSignature =>
v.tpe
}
.filter(filter)
.map { tpe =>
Patch.lint(
Diagnostic(
id = "",
message = message(tpe),
position = p.pos,
severity = severity,
)
)
}
.asPatch
}.asPatch
}.asPatch
}
}

0 comments on commit 7edc336

Please sign in to comment.