Skip to content

Commit b08b27b

Browse files
chore: use lazy val instead of for collectWithContext
1 parent 4e5e53e commit b08b27b

2 files changed

Lines changed: 13 additions & 16 deletions

File tree

modules/core/src/main/scala/stryker4s/extension/TreeExtensions.scala

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package stryker4s.extension
22

3-
import cats.data.{Chain, State}
3+
import cats.Eq
44
import cats.syntax.all.*
5-
import cats.{Eq, Eval}
65
import mutationtesting.Location
76
import mutationtesting.cats.*
87

@@ -134,21 +133,19 @@ object TreeExtensions {
134133
)(collectFn: PartialFunction[Tree, C => T]): Seq[T] = {
135134
val collectFnLifted = collectFn.lift
136135
val buildContextLifted = buildContext.lift
137-
138-
def traverse(tree: Tree): State[Eval[Option[C]], Chain[T]] =
139-
State.get[Eval[Option[C]]].flatMap { inherited =>
140-
// The context for this node and its descendants
141-
val contextEval = Eval.defer(buildContextLifted(tree).fold(inherited)(_.some.pure[Eval])).memoize
142-
143-
// Only read the context when this node actually collects something.
144-
val collected = collectFnLifted(tree).foldMap(collect => Chain.fromOption(contextEval.value.map(collect)))
145-
146-
// Each child starts from this node's context, independent of its siblings.
147-
tree.children.foldMapM(child => State.set(contextEval) *> traverse(child)).map(collected ++ _)
148-
}
136+
val builder = Vector.newBuilder[T]
137+
138+
def traverse(tree: Tree, inherited: () => Option[C]): Unit = {
139+
// The context for this node and its descendants, only computed when a node actually collects something
140+
lazy val context: Option[C] = buildContextLifted(tree).orElse(inherited())
141+
collectFnLifted(tree).foreach(collect => context.foreach(c => builder += collect(c)))
142+
val contextFn = () => context
143+
tree.children.foreach(traverse(_, contextFn))
144+
}
149145

150146
// Traverse the tree, starting with an empty context
151-
traverse(tree).runA(none.pure[Eval]).value.toVector
147+
traverse(tree, () => None)
148+
builder.result()
152149
}
153150

154151
}

modules/core/src/main/scala/stryker4s/mutants/tree/MutantInstrumenter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class MutantInstrumenter(options: InstrumenterOptions)(implicit log: Logger) {
141141
*/
142142
private def compileErrorIsInCaseStatement(caze: Case, error: CompilerErrMsg): Boolean = {
143143
error.offset match {
144-
case Some(offset) => caze.pos.start <= offset && caze.pos.end >= offset
144+
case Some(offset) => caze.begOffset <= offset && caze.endOffset >= offset
145145
case None => (caze.pos.startLine + 1) <= error.line && (caze.pos.endLine + 1) >= error.line
146146
}
147147
}

0 commit comments

Comments
 (0)