diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2017-10-21 20:09:20 -0700 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2017-10-21 20:09:20 -0700 |
commit | 6f2de230b39fb9f178df7c2470b667f7f4377478 (patch) | |
tree | 040962eab0701850f797aaa6a16e4be183a03b75 /src | |
parent | 71ddc7e65666844e063c48c80b3ce7fecfe77a0f (diff) | |
download | mill-6f2de230b39fb9f178df7c2470b667f7f4377478.tar.gz mill-6f2de230b39fb9f178df7c2470b667f7f4377478.tar.bz2 mill-6f2de230b39fb9f178df7c2470b667f7f4377478.zip |
Add `StaticContext` macro to let us distinguish top-level targets from dynamically generated targets
Diffstat (limited to 'src')
-rw-r--r-- | src/main/scala/hbt/StaticContext.scala | 25 | ||||
-rw-r--r-- | src/test/scala/hbt/StaticContextTests.scala | 36 |
2 files changed, 61 insertions, 0 deletions
diff --git a/src/main/scala/hbt/StaticContext.scala b/src/main/scala/hbt/StaticContext.scala new file mode 100644 index 00000000..7eb4fa8d --- /dev/null +++ b/src/main/scala/hbt/StaticContext.scala @@ -0,0 +1,25 @@ +package hbt + + +import scala.language.experimental.macros +import scala.reflect.macros._ + +case class StaticContext(value: Boolean) +object StaticContext { + implicit def apply: StaticContext = macro applyImpl + def rec(c: Context)(expr: c.Symbol): Boolean = { + import c.universe._ + // Classes and traits and such + if(!expr.isModuleClass && expr.isClass) false + // Method contents + else if(expr.isMethod) false + else if(expr.owner == NoSymbol) true + else rec(c)(expr.owner) + } + + def applyImpl(c: Context): c.Expr[StaticContext] = { + import c.universe._ + val staticContext = rec(c)(c.internal.enclosingOwner) + c.Expr[StaticContext](q"hbt.StaticContext($staticContext)") + } +} diff --git a/src/test/scala/hbt/StaticContextTests.scala b/src/test/scala/hbt/StaticContextTests.scala new file mode 100644 index 00000000..022cfd1e --- /dev/null +++ b/src/test/scala/hbt/StaticContextTests.scala @@ -0,0 +1,36 @@ +package hbt + +import utest._ +class Helper{ + val static = implicitly[StaticContext] + object Nested { + val static = implicitly[StaticContext] + } + def method = implicitly[StaticContext] +} +object StaticContextTests extends TestSuite{ + val static = implicitly[StaticContext] + object Nested{ + val static = implicitly[StaticContext] + def method = implicitly[StaticContext] + class Helper{ + val static = implicitly[StaticContext] + } + } + + def method = implicitly[StaticContext] + val tests = Tests{ + val helper = new Helper() + 'inObject - assert(static.value) + 'inClass- assert(!helper.static.value) + 'inMethod - assert(!method.value) + + 'inObjectObject - assert(Nested.static.value) + 'inObjectClass- assert(!helper.static.value) + 'inObjectMethod- assert(!Nested.method.value) + + 'inClassObject - assert(!helper.Nested.static.value) + 'inClassMethod- assert(!helper.method.value) + + } +} |