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

Make node: HTMLElement available as an implicit #539

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.scalajs.linker.MemOutputFile
import java.util.concurrent.Executor
import scala.concurrent.ExecutionContext.Implicits.global
import org.scalajs.linker.interface.ClearableLinker
import scala.util.Random

class JsModifier extends mdoc.PreModifier {
override val name = "js"
Expand Down Expand Up @@ -140,6 +141,7 @@ class JsModifier extends mdoc.PreModifier {
val code = new CodeBuilder()
val wrapped = code
.println("object mdocjs {")
.println(s"""class HTMLElementImplicit(val value: _root_.org.scalajs.dom.raw.HTMLElement)""")
.foreach(runs)(code.println)
.println("}")
.toString
Expand Down Expand Up @@ -254,6 +256,7 @@ class JsModifier extends mdoc.PreModifier {
.println(
s"""def $run($mountNodeParam: _root_.org.scalajs.dom.raw.HTMLElement): Unit = {"""
)
.println(s"implicit val ${mountNodeParam}Implicit = new HTMLElementImplicit($mountNodeParam);")
.println(input.text)
.println("}")
.toString
Expand Down
28 changes: 26 additions & 2 deletions tests/unit-js/src/test/scala/tests/js/JsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,14 @@ class JsSuite extends BaseMarkdownSuite {
|println(jsdocs.ExampleJS.greeting)
|```
""".stripMargin,
"""|error: no-dom.md:4 (mdoc generated code) object scalajs is not a member of package org
"""|error: no-dom.md:3 (mdoc generated code) object scalajs is not a member of package org
|class HTMLElementImplicit(val value: _root_.org.scalajs.dom.raw.HTMLElement)
| ^
|
|error: no-dom.md:5 (mdoc generated code) object scalajs is not a member of package org
|def run0(node: _root_.org.scalajs.dom.raw.HTMLElement): Unit = {
| ^
""".stripMargin,
|""".stripMargin,
settings = {
val noScalajsDom = Classpath(baseSettings.site("js-classpath")).entries
.filterNot(_.toNIO.getFileName.toString.contains("scalajs-dom"))
Expand Down Expand Up @@ -377,4 +381,24 @@ class JsSuite extends BaseMarkdownSuite {
)
}
)

check(
"implicit-node",
"""
|```scala mdoc:js:shared:invisible
|def div(msg: String)(implicit node: HTMLElementImplicit) =
| node.value.innerHTML = "msg"
|```
|```scala mdoc:js
|div("hello world")
|```
|""".stripMargin,
"""|```scala
|div("hello world")
|```
|<div id="mdoc-html-run1" data-mdoc-js></div>
|<script type="text/javascript" src="implicit-node.md.js" defer></script>
|<script type="text/javascript" src="mdoc.js" defer></script>
|""".stripMargin
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite {
original: String,
settings: Settings = baseSettings,
onOutput: String => Unit = _ => ()
): Unit = {
)(implicit loc: munit.Location): Unit = {
test(name) {
val reporter = newReporter()
val context = newContext(settings, reporter)
Expand Down