|
1 | 1 | package dotty.tools.backend.sjs
|
2 | 2 |
|
| 3 | +import java.net.{URI, URISyntaxException} |
| 4 | + |
3 | 5 | import dotty.tools.dotc.core._
|
4 | 6 | import Contexts._
|
5 | 7 |
|
| 8 | +import dotty.tools.dotc.report |
| 9 | + |
6 | 10 | import dotty.tools.dotc.util.{SourceFile, SourcePosition}
|
7 | 11 | import dotty.tools.dotc.util.Spans.Span
|
8 | 12 |
|
9 | 13 | import org.scalajs.ir
|
10 | 14 |
|
11 | 15 | /** Conversion utilities from dotty Positions to IR Positions. */
|
12 | 16 | class JSPositions()(using Context) {
|
| 17 | + import JSPositions._ |
| 18 | + |
| 19 | + private val sourceURIMaps: List[URIMap] = { |
| 20 | + ctx.settings.scalajsMapSourceURI.value.flatMap { option => |
| 21 | + val uris = option.split("->") |
| 22 | + if (uris.length != 1 && uris.length != 2) { |
| 23 | + report.error("-scalajs-mapSourceURI needs one or two URIs as argument (separated by '->').") |
| 24 | + Nil |
| 25 | + } else { |
| 26 | + try { |
| 27 | + val from = new URI(uris.head) |
| 28 | + val to = uris.lift(1).map(str => new URI(str)) |
| 29 | + URIMap(from, to) :: Nil |
| 30 | + } catch { |
| 31 | + case e: URISyntaxException => |
| 32 | + report.error(s"${e.getInput} is not a valid URI") |
| 33 | + Nil |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
13 | 38 |
|
14 | 39 | private def sourceAndSpan2irPos(source: SourceFile, span: Span): ir.Position = {
|
15 | 40 | if (!span.exists) ir.Position.NoPosition
|
@@ -59,16 +84,16 @@ class JSPositions()(using Context) {
|
59 | 84 | )
|
60 | 85 | case file =>
|
61 | 86 | val srcURI = file.toURI
|
62 |
| - def matches(pat: java.net.URI) = pat.relativize(srcURI) != srcURI |
63 |
| - |
64 |
| - // TODO |
65 |
| - /*scalaJSOpts.sourceURIMaps.collectFirst { |
66 |
| - case ScalaJSOptions.URIMap(from, to) if matches(from) => |
| 87 | + sourceURIMaps.collectFirst { |
| 88 | + case URIMap(from, to) if from.relativize(srcURI) != srcURI => |
67 | 89 | val relURI = from.relativize(srcURI)
|
68 | 90 | to.fold(relURI)(_.resolve(relURI))
|
69 |
| - } getOrElse*/ |
70 |
| - srcURI |
| 91 | + }.getOrElse(srcURI) |
71 | 92 | }
|
72 | 93 | }
|
73 | 94 | }
|
74 | 95 | }
|
| 96 | + |
| 97 | +object JSPositions { |
| 98 | + final case class URIMap(from: URI, to: Option[URI]) |
| 99 | +} |
0 commit comments