From 65c2791abba68a9468471588ca8746b57c464da4 Mon Sep 17 00:00:00 2001 From: Gregg Hernandez Date: Fri, 1 Jul 2016 12:02:07 -0600 Subject: [PATCH] Adds @Input() similar to typescript API --- src/main/scala/angulate2/Component.scala | 25 +++++++++++++++++++++++- src/main/scala/angulate2/Input.scala | 7 +++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/main/scala/angulate2/Input.scala diff --git a/src/main/scala/angulate2/Component.scala b/src/main/scala/angulate2/Component.scala index 37ed401..55f22d1 100644 --- a/src/main/scala/angulate2/Component.scala +++ b/src/main/scala/angulate2/Component.scala @@ -51,17 +51,40 @@ object Component { def modifiedDeclaration(classDecl: ClassDef) = { val parts = extractClassParts(classDecl) + import parts._ // load debug annotation values (returns default config, if there is no @debug on this component) val debug = getDebugConfig(modifiers) + val inputStrLiterals = body collect { + case q"@Input() var $iName: $iType = $iRhs" => iName.toString + case q"@Input($extName) var $iName: $iType = $iRhs" => extName match { + case Literal(Constant(value)) => s"$iName:$value" + case currentTree => c.abort(currentTree.pos, "@Input() parameter must be a literal String") + } + } + val objName = fullName + "_" - val componentAnnotationParams = extractAnnotationParameters(c.prefix.tree,annotationParamNames) collect { + val allComponentAnnotationParams = extractAnnotationParameters(c.prefix.tree, annotationParamNames).collect { case (name,Some(value)) => q"${Ident(TermName(name))} = $value" } + val (nonInputAnnotationParams, inputAnnotationParams) = allComponentAnnotationParams.partition { + case q"inputs = $v" => false + case _ => true + } + + val inputs = inputAnnotationParams match { + case q"inputs = js.Array(..$ins)" :: Nil => + q"inputs = scalajs.js.Array(..${ins ++ inputStrLiterals.map(s => Literal(Constant(s)))})" + case _ => + q"inputs = scalajs.js.Array(..$inputStrLiterals)" + } + + val componentAnnotationParams = Iterable(inputs) ++ nonInputAnnotationParams + val parameterAnnot = parameterAnnotation(fullName,params) // string to be written to the annotations.js file diff --git a/src/main/scala/angulate2/Input.scala b/src/main/scala/angulate2/Input.scala new file mode 100644 index 0000000..2d83331 --- /dev/null +++ b/src/main/scala/angulate2/Input.scala @@ -0,0 +1,7 @@ +package angulate2 + +import scala.annotation.StaticAnnotation + +class Input() extends StaticAnnotation { + def this(externalName: String) = this() +}