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

Scala 3 JS modifier #504

Merged
merged 9 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 4 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,11 @@ val jsdocs = project
.settings(
sharedSettings,
publish / skip := true,
crossScalaVersions --= scala3,
scalaJSLinkerConfig ~= {
_.withModuleKind(ModuleKind.CommonJSModule)
},
libraryDependencies ++= List(
"org.scala-js" %%% "scalajs-dom" % scalajsDom
"org.scala-js" %%% "scalajs-dom" % scalajsDom cross CrossVersion.for3Use2_13
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DOM library won't be published for Scala 3.

),
scalaJSUseMainModuleInitializer := true,
Compile / npmDependencies ++= List(
Expand Down Expand Up @@ -342,7 +341,6 @@ lazy val unitJS = project
.settings(
sharedSettings,
publish / skip := true,
crossScalaVersions --= scala3,
Compile / unmanagedSourceDirectories ++= multiScalaDirectories("tests/unit-js").value,
libraryDependencies ++= List(
"org.scalameta" %% "munit" % V.munit % Test
Expand Down Expand Up @@ -400,7 +398,6 @@ lazy val js = project
.in(file("mdoc-js"))
.settings(
sharedSettings,
crossScalaVersions --= scala3,
moduleName := "mdoc-js",
Compile / unmanagedSourceDirectories ++= multiScalaDirectories("js").value,
libraryDependencies ++= crossSetting(
Expand All @@ -409,7 +406,9 @@ lazy val js = project
"org.scala-js" % "scalajs-compiler" % scalajs cross CrossVersion.full,
"org.scala-js" %% "scalajs-linker" % scalajs
),
if3 = List()
if3 = List(
"org.scala-js" %% "scalajs-linker" % scalajs cross CrossVersion.for3Use2_13
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After we obtain sjsir files, it doesn't matter which linker we use.

)
)
)
.dependsOn(mdoc)
Expand Down
12 changes: 12 additions & 0 deletions mdoc-js/src/main/scala-2/mdoc/modifiers/CompilerCompat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mdoc.modifiers

import scala.reflect.io.VirtualDirectory
import mdoc.internal.markdown.MarkdownCompiler
import scala.meta.inputs.Input
import mdoc.Reporter
import mdoc.internal.pos.TokenEditDistance
import mdoc.internal.markdown.FileImport

private[modifiers] object CompilerCompat {
def abstractFile(tg: String) = new VirtualDirectory(tg, None)
}
13 changes: 13 additions & 0 deletions mdoc-js/src/main/scala-3/mdoc/modifiers/CompilerCompat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mdoc.modifiers

import dotty.tools.io.{AbstractFile, VirtualDirectory}

import mdoc.internal.markdown.MarkdownCompiler
import scala.meta.inputs.Input
import mdoc.Reporter
import mdoc.internal.pos.TokenEditDistance
import mdoc.internal.markdown.FileImport

private[modifiers] object CompilerCompat {
def abstractFile(tg: String) = new VirtualDirectory(tg, None)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import scala.collection.mutable.ListBuffer
import scala.meta.Term
import scala.meta.inputs.Input
import scala.meta.io.{AbsolutePath, Classpath}
import scala.reflect.io.VirtualDirectory
import org.scalajs.linker.interface.StandardConfig
import org.scalajs.linker.StandardImpl
import org.scalajs.linker.PathIRContainer
Expand All @@ -35,7 +34,7 @@ class JsModifier extends mdoc.PreModifier {
override val name = "js"
override def toString: String = s"JsModifier($config)"
val irCache = new StandardIRFileCache
val target = new VirtualDirectory("(memory)", None)
val target = CompilerCompat.abstractFile("(memory)")
var maybeCompiler: Option[MarkdownCompiler] = None
var config = JsConfig()
var linker: ClearableLinker = newLinker()
Expand Down Expand Up @@ -147,6 +146,7 @@ class JsModifier extends mdoc.PreModifier {
val input = Input.VirtualFile(ctx.relativePath.toString(), wrapped)
val edit = TokenEditDistance.fromInputs(inputs, input)
val oldErrors = ctx.reporter.errorCount

compiler.compileSources(input, ctx.reporter, edit, fileImports = Nil)
val hasErrors = ctx.reporter.errorCount > oldErrors
val sjsir = for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,22 @@ class MarkdownCompiler(
target: AbstractFile = new VirtualDirectory("(memory)")
) {


private def newContext: FreshContext = {
val defaultFlags =
List("-color:never", "-unchecked", "-deprecation", "-Ximport-suggestion-timeout", "0")
val options = scalacOptions.split("\\s+").toList
val settings =
options ::: defaultFlags ::: "-classpath" :: classpath :: Nil
val driver = new InteractiveDriver(settings.distinct)
driver.currentCtx.fresh.setReporter(new CollectionReporter)

val ctx = driver.currentCtx.fresh

ctx
.setReporter(new CollectionReporter)
.setSetting(
ctx.settings.outputDir, target
)
}

private var context = newContext
Expand Down Expand Up @@ -106,13 +114,14 @@ class MarkdownCompiler(
vreporter: Reporter,
edit: TokenEditDistance,
fileImports: List[FileImport],
context: Context
freshContext: Option[Context] = None
): Unit = {
clearTarget()
val context = freshContext.getOrElse(newContext)
val compiler = new Compiler
val run = compiler.newRun(using context)
val inputs = List(input)
scala.util.Try(run.compileSources(inputs.map(toSource)))
val res = scala.util.Try(run.compileSources(inputs.map(toSource)))
report(vreporter, input, fileImports, run.runContext, edit)
}

Expand All @@ -139,7 +148,7 @@ class MarkdownCompiler(
target
)

compileSources(input, vreporter, edit, fileImports, freshContext)
compileSources(input, vreporter, edit, fileImports, Some(freshContext))
if (!freshContext.reporter.hasErrors) {
val loader = new AbstractFileClassLoader(target, appClassLoader)
try {
Expand Down Expand Up @@ -226,7 +235,6 @@ class MarkdownCompiler(
): Unit = {

val infos = context.reporter.pendingMessages(using context).toSeq.sortBy(_.pos.source.path)

infos.foreach {
case diagnostic if diagnostic.position.isPresent =>
val pos = diagnostic.position.get
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.10.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.15.0")
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.7")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.5.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.5.1")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.20.0")

libraryDependencies ++= List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ class JsSuite extends BaseMarkdownSuite {
| required: Int
|val x: Int = ""
| ^^
""".stripMargin
""".stripMargin,
compat = Map(
"3.0" ->
"""
|error: error.md:3:14
|Found: ("" : String)
|Required: Int
""".stripMargin
)
)

check(
Expand Down Expand Up @@ -93,7 +101,21 @@ class JsSuite extends BaseMarkdownSuite {
| required: String
|val y: String = 42
| ^^
""".stripMargin
""".stripMargin,
compat = Map(
"3.0" ->
"""
|error: edit.md:3:14:
|Found: ("" : String)
|Required: Int
| val x: Int = ""
| ^^
|error: edit.md:7:17:
|Found: (42 : Int)
|Required: String
| val y: String = 42
""".stripMargin
)
)

checkError(
Expand All @@ -110,7 +132,14 @@ class JsSuite extends BaseMarkdownSuite {
"""|error: isolated.md:7:9: not found: value x
|println(x)
| ^
""".stripMargin
""".stripMargin,
compat = Map(
"3.0" ->
"""
|error: isolated.md:7:9
|Not found: x
""".stripMargin
)
)

checkCompiles(
Expand Down Expand Up @@ -171,7 +200,15 @@ class JsSuite extends BaseMarkdownSuite {
| required: String
|val x: String = 42
| ^^
""".stripMargin
""".stripMargin,
compat = Map(
"3.0" ->
"""
|-error: compile-only-error.md:3:17:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. This is not right.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Seems like it's because the scala binary version has changed for 3.0.0 stable - so our tests aren't actually verifying Scala 3 assertions :-/

|Found: (42 : Int)
|Required: String
""".stripMargin
)
)

// It's easy to mess up stripMargin multiline strings when generating code with strings.
Expand Down Expand Up @@ -247,7 +284,15 @@ class JsSuite extends BaseMarkdownSuite {
baseSettings.copy(
site = baseSettings.site.updated("js-classpath", Classpath(noScalajsDom).syntax)
)
}
},
compat = Map(
"3.0" ->
"""
|error:
|no-dom.md:3 (mdoc generated code)
| value scalajs is not a member of org
""".stripMargin
)
)

checkError(
Expand Down