Skip to content

Commit 8f9a807

Browse files
committed
Initial commit
0 parents  commit 8f9a807

File tree

10 files changed

+166
-0
lines changed

10 files changed

+166
-0
lines changed

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
*.class
2+
*.log
3+
4+
# sbt specific
5+
.cache
6+
.history
7+
.lib/
8+
dist/*
9+
target/
10+
lib_managed/
11+
src_managed/
12+
project/boot/
13+
project/plugins/project/
14+
15+
# Scala-IDE specific
16+
.scala_dependencies
17+
.worksheet
18+
19+
# IntelliJ specific
20+
.idea
21+
22+
# ENSIME specific
23+
.ensime_cache/
24+
.ensime
25+
26+
# macOS specific
27+
.DS_Store
28+
29+
# Dotty IDE
30+
/.dotty-ide-dev-port
31+
/.dotty-ide-artifact
32+
/.dotty-ide.json
33+
34+
# VS Code
35+
.vscode/

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2018, Allan Renucci
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# jsx-interpolator
2+
A JSX string interpolator built on top of Dotty macros

build.sbt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
val dottyVersion = "0.10.0-bin-20180914-0a2734b-NIGHTLY"
2+
3+
lazy val root = project
4+
.in(file("."))
5+
.settings(
6+
name := "jsx-interpolator",
7+
version := "0.1.0",
8+
scalaVersion := dottyVersion,
9+
scalacOptions ++= Seq(
10+
"-feature",
11+
"-deprecation",
12+
"-unchecked",
13+
"-Xfatal-warnings",
14+
"-encoding", "UTF8",
15+
"-language:implicitConversions"
16+
),
17+
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test
18+
)

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.2.3

project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.2.2")

src/main/scala/jsx/Jsx.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package jsx
2+
3+
object Jsx {
4+
case class Repr(parts: String, arg: Any)
5+
}

src/main/scala/jsx/JsxQuote.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package jsx
2+
3+
import scala.quoted._
4+
5+
// Ideally should be an implicit class but the implicit conversion
6+
// has to be a rewrite method
7+
class JsxQuote(ctx: => StringContext) {
8+
rewrite def jsx(args: => Any*): Jsx.Repr = ~Macros.quoteImpl('(ctx), '(args))
9+
}
10+
11+
object JsxQuote {
12+
implicit rewrite def JsxQuote(ctx: => StringContext): JsxQuote =
13+
new JsxQuote(ctx)
14+
}

src/main/scala/jsx/Macros.scala

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package jsx
2+
3+
import scala.quoted._
4+
import scala.tasty.Tasty
5+
6+
object Macros {
7+
def quoteImpl(ctx: Expr[StringContext], args: Expr[Seq[Any]])
8+
(implicit tasty: Tasty): Expr[Jsx.Repr] = {
9+
import tasty._
10+
import Term._
11+
12+
// for debugging purpose
13+
def pp(tree: Tree): Unit = {
14+
println(tasty.showSourceCode.showTree(tree))
15+
}
16+
17+
def isStringConstant(tree: Term) = tree match {
18+
case Literal(_) => true
19+
case _ => false
20+
}
21+
22+
// _root_.scala.StringContext.apply(("p0", "p1": scala.<repeated>[scala#Predef.String]))
23+
val parts = ctx.toTasty match {
24+
case Inlined(_, _,
25+
Apply(
26+
Select(Select(Select(Ident("_root_"), "scala", _), "StringContext", _), "apply", _),
27+
List(Typed(Repeated(values), _)))) if values.forall(isStringConstant) =>
28+
values.collect { case Literal(Constant.String(value)) => value }
29+
case _ =>
30+
???
31+
}
32+
33+
// (a0, a1: scala.<repeated>[scala.Any])
34+
val args0: List[Term] = args.toTasty match {
35+
case Inlined(_, _, Typed(Repeated(values), _)) =>
36+
values
37+
case _ =>
38+
???
39+
}
40+
41+
val string = parts.mkString("??")
42+
// first one for test purpose
43+
val arg0 = args0.head.toExpr[Any]
44+
45+
'(new Jsx.Repr(~string.toExpr, ~arg0))
46+
}
47+
}

src/test/scala/jsx/JsxQuoteTest.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package jsx
2+
3+
import org.junit.Test
4+
import org.junit.Assert._
5+
6+
import JsxQuote._
7+
8+
class JsxQuoteTest {
9+
@Test
10+
def test(): Unit = {
11+
val name = new Object{}
12+
assertEquals(Jsx.Repr("Hello ??!", name), jsx"Hello $name!")
13+
}
14+
}

0 commit comments

Comments
 (0)