Skip to content

Commit

Permalink
Merge pull request #191 from valydia/lazy-val
Browse files Browse the repository at this point in the history
Evaluate lazy vals lazily (fixes #179)
  • Loading branch information
olafurpg authored Oct 4, 2019
2 parents 1e25572 + 8c3914b commit 908f569
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mdoc/src/main/scala/mdoc/internal/markdown/Instrumenter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
Expand Down
56 changes: 56 additions & 0 deletions tests/unit/src/test/scala/tests/markdown/DefaultSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"""
Expand Down

0 comments on commit 908f569

Please sign in to comment.