From 8c3914b36145ed182f76adcde2bef8e7d1ef1557 Mon Sep 17 00:00:00 2001 From: valydia <2129101+valydia@users.noreply.github.com> Date: Sat, 21 Sep 2019 17:00:20 +0100 Subject: [PATCH] Evaluate lazy vals lazily (fixes #179) --- .../mdoc/internal/markdown/Instrumenter.scala | 2 + .../scala/tests/markdown/DefaultSuite.scala | 56 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/mdoc/src/main/scala/mdoc/internal/markdown/Instrumenter.scala b/mdoc/src/main/scala/mdoc/internal/markdown/Instrumenter.scala index 3427e7ab0..ba92041cf 100644 --- a/mdoc/src/main/scala/mdoc/internal/markdown/Instrumenter.scala +++ b/mdoc/src/main/scala/mdoc/internal/markdown/Instrumenter.scala @@ -6,6 +6,7 @@ import scala.meta._ import scala.meta.inputs.Position import Instrumenter.position import mdoc.internal.markdown.Instrumenter.Binders +import scala.meta.Mod.Lazy class Instrumenter(sections: List[SectionInput]) { def instrument(): String = { @@ -122,6 +123,7 @@ object Instrumenter { def binders(pat: Pat): List[Name] = pat.collect { case m: Member => m.name } def unapply(tree: Tree): Option[List[Name]] = tree match { + case Defn.Val(mods, _, _, _) if mods.exists(_.isInstanceOf[Lazy]) => Some(Nil) case Defn.Val(_, pats, _, _) => Some(pats.flatMap(binders)) case Defn.Var(_, pats, _, _) => Some(pats.flatMap(binders)) case _: Defn => Some(Nil) diff --git a/tests/unit/src/test/scala/tests/markdown/DefaultSuite.scala b/tests/unit/src/test/scala/tests/markdown/DefaultSuite.scala index 78adfe755..875e2083c 100644 --- a/tests/unit/src/test/scala/tests/markdown/DefaultSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/DefaultSuite.scala @@ -69,6 +69,62 @@ class DefaultSuite extends BaseMarkdownSuite { """.stripMargin ) + check( + "lazy-val", + """ + |```scala mdoc + |lazy val x = ??? + |List(1).map(_ + 1) + |res0.length + |``` + | + |```scala mdoc + |println(1) + |``` + """.stripMargin, + """|```scala + |lazy val x = ??? + |List(1).map(_ + 1) + |// res0: List[Int] = List(2) + |res0.length + |// res1: Int = 1 + |``` + | + |```scala + |println(1) + |// 1 + |``` + """.stripMargin + ) + + check( + "lazy-val 2", + """ + |```scala mdoc + |lazy val x = 3 + |List(1).map(_ + x) + |res0.length + |``` + | + |```scala mdoc + |println(1) + |``` + """.stripMargin, + """|```scala + |lazy val x = 3 + |List(1).map(_ + x) + |// res0: List[Int] = List(4) + |res0.length + |// res1: Int = 1 + |``` + | + |```scala + |println(1) + |// 1 + |``` + """.stripMargin + ) + check( "defn", """