diff --git a/library/coreGeneric/src/main/scala-2/japgolly/scalajs/react/hooks/HookMacros.scala b/library/coreGeneric/src/main/scala-2/japgolly/scalajs/react/hooks/HookMacros.scala index c12d5a2a0..69ab581e2 100644 --- a/library/coreGeneric/src/main/scala-2/japgolly/scalajs/react/hooks/HookMacros.scala +++ b/library/coreGeneric/src/main/scala-2/japgolly/scalajs/react/hooks/HookMacros.scala @@ -27,23 +27,74 @@ class HookMacros(val c: Context) extends MacroUtils { private implicit def autoTagToType[A](t: c.WeakTypeTag[A]): Type = t.tpe - private def Box : Tree = q"_root_.japgolly.scalajs.react.internal.Box" - private def Box(t: Type): Type = appliedType(c.typeOf[Box[_]], t) - private def Hooks : Tree = q"_root_.japgolly.scalajs.react.hooks.Hooks" - private def JsFn : Tree = q"_root_.japgolly.scalajs.react.component.JsFn" - private def React : Tree = q"_root_.japgolly.scalajs.react.facade.React" - private def ScalaFn : Tree = q"_root_.japgolly.scalajs.react.component.ScalaFn" - private def withHooks = "withHooks" + private def Box : Tree = q"_root_.japgolly.scalajs.react.internal.Box" + private def Box(t: Type) : Type = appliedType(c.typeOf[Box[_]], t) + private def HookCtx : Tree = q"_root_.japgolly.scalajs.react.hooks.HookCtx" + private def Hooks : Tree = q"_root_.japgolly.scalajs.react.hooks.Hooks" + private def JsFn : Tree = q"_root_.japgolly.scalajs.react.component.JsFn" + private def PropsChildren: Tree = q"_root_.japgolly.scalajs.react.PropsChildren" + private def React : Tree = q"_root_.japgolly.scalajs.react.facade.React" + private def ScalaFn : Tree = q"_root_.japgolly.scalajs.react.component.ScalaFn" + private def withHooks = "withHooks" - case class HookDefn(steps: List[HookStep]) - case class HookStep(name: String, targs: List[Tree], args: List[List[Tree]]) + private case class HookDefn(steps: List[HookStep]) + + private case class HookStep(name: String, targs: List[Tree], args: List[List[Tree]]) + + private class HookRewriter(props: Tree, initChildren: Tree, propsChildren: Tree) { + private var stmts = Vector.empty[Tree] + private var hooks = List.empty[Ident] + private var _hookCount = 0 + private var _usesChildren = false + + def usesChildren() = + _usesChildren + + def useChildren(): Unit = { + _usesChildren = true + this += initChildren + } + + def +=(stmt: Tree): Unit = + stmts :+= stmt + + def hookCount(): Int = + _hookCount + + def nextHookName(suffix: String = ""): TermName = + TermName("hook" + (hookCount() + 1) + suffix) + + def registerHook(h: TermName): Unit = { + hooks :+= Ident(h) + _hookCount += 1 + } + + def args(): List[Tree] = + if (usesChildren()) + props :: propsChildren :: hooks + else + props :: hooks + + def ctxArg(): Tree = { + val hookCtxObj = if (usesChildren()) q"$HookCtx.withChildren" else HookCtx + val create = Apply(hookCtxObj, args()) + val name = nextHookName("_ctx") + this += q"val $name = $create" + Ident(name) + } + + def wrap(body: Tree): Tree = + q"..$stmts; $body" + } + + // ------------------------------------------------------------------------------------------------------------------- def render[P, C <: Children, Ctx, CtxFn[_], Step <: SubsequentStep[Ctx, CtxFn]] (f: c.Tree)(step: c.Tree, s: c.Tree) (implicit P: c.WeakTypeTag[P], C: c.WeakTypeTag[C]): c.Tree = { implicit val log = MacroLogger() - // log.enabled = showCode(c.macroApplication).contains("counter.value") + log.enabled = showCode(c.macroApplication).contains("DEBUG") // TODO: DELETE log.header() log("macroApplication", showRaw(c.macroApplication)) @@ -103,72 +154,90 @@ class HookMacros(val c: Context) extends MacroUtils { Left(() => "Don't know how to parse " + showRaw(tree)) } - private type RenderInliner = (Tree, Init) => Tree - - private def inlineHookDefn(h: HookDefn)(implicit log: MacroLogger): Either[() => String, RenderInliner] = { - val init = new Init("hook" + _, lazyVals = false) + private def inlineHookDefn(h: HookDefn)(implicit log: MacroLogger): Either[() => String, HookRewriter => Tree] = { val it = h.steps.iterator - var stepId = 0 var renderStep: HookStep = null - var hooks = List.empty[TermName] + var hooks = Vector.empty[HookRewriter => TermName] + var withPropsChildren = false while (it.hasNext) { val step = it.next() if (it.hasNext) { - stepId += 1 - inlineHookStep(stepId, step, init) match { - case Right(termName) => hooks ::= termName - case Left(e) => return Left(e) - } + if (hooks.isEmpty && step.name == "withPropsChildren") + withPropsChildren = true + else + inlineHookStep(step) match { + case Right(h) => hooks :+= h + case Left(e) => return Left(e) + } } else renderStep = step } - hooks = hooks.reverse - hookRenderInliner(renderStep, hooks.map(Ident(_))).map { f => - (props, init2) => { - init2 ++= init.stmts - f(props, init2) - } + hookRenderInliner(renderStep).map { buildRender => b => + if (withPropsChildren) + b.useChildren() + for (h <- hooks) + b registerHook h(b) + buildRender(b) } } - private def inlineHookStep(stepId: Int, step: HookStep, init: Init)(implicit log: MacroLogger): Either[() => String, TermName] = { + private def inlineHookStep(step: HookStep)(implicit log: MacroLogger): Either[() => String, HookRewriter => TermName] = { log("inlineHookStep." + step.name, step) + + def useState(b: HookRewriter, tpe: Tree, body: Tree) = { + val rawName = b.nextHookName("_raw") + val name = b.nextHookName() + b += q"val $rawName = $React.useStateFn(() => $Box[$tpe]($body))" + b += q"val $name = $Hooks.UseState.fromJsBoxed[$tpe]($rawName)" + name + } + step.name match { + case "useState" => - val stateType = step.targs.head - val arg = step.args.head.head - val rawName = TermName("hook" + stepId + "_raw") - val name = TermName("hook" + stepId) - init += q"val $rawName = $React.useStateFn(() => $Box[$stateType]($arg))" - init += q"val $name = $Hooks.UseState.fromJsBoxed[$stateType]($rawName)" - Right(name) + val targ = step.targs.head + val arg = step.args.head.head + Right(useState(_, targ, arg)) + + case "useStateBy" => + val targ = step.targs.head + val arg = step.args.head.head + arg match { + case f@ Function(params, _) => + if (params.sizeIs == 1) + Right { b => + val ctxArg = b.ctxArg() + useState(b, targ, call(f, ctxArg :: Nil)) + } + else + Right(b => useState(b, targ, call(f, b.args()))) + + case _ => + Left(() => s"Expected a function, found: ${showRaw(arg)}") + } case _ => Left(() => s"Inlining of hook method '${step.name}' not yet supported.") } } - private def hookRenderInliner(step: HookStep, hooks: List[Tree])(implicit log: MacroLogger): Either[() => String, RenderInliner] = { + private def hookRenderInliner(step: HookStep)(implicit log: MacroLogger): Either[() => String, HookRewriter => Tree] = { log("inlineHookRender." + step.name, step) step.name match { case "render" => @nowarn("msg=exhaustive") val List(List(renderFn), _) = step.args - Right { (props, _) => - val args = props :: hooks - Apply(Select(renderFn, TermName("apply")), args) - } + Right(b => call(renderFn, b.args())) case _ => Left(() => s"Inlining of hook render method '${step.name}' not yet supported.") } } - private def inlineHookRawComponent[P](renderInliner: RenderInliner)(implicit P: c.WeakTypeTag[P]): Tree = { - val props_unbox = q"props.unbox" - val init = new Init("_i" + _) - val render1 = renderInliner(props_unbox, init) - val render2 = init.wrap(q"$render1.rawNode") + private def inlineHookRawComponent[P](rewrite: HookRewriter => Tree)(implicit P: c.WeakTypeTag[P]): Tree = { + val b = new HookRewriter(q"props.unbox", q"val children = $PropsChildren.fromRawProps(props)", q"children") + val render1 = rewrite(b) + val render2 = b.wrap(q"$render1.rawNode") q"(props => $render2): $JsFn.RawComponent[${Box(P)}]" } @@ -178,4 +247,35 @@ class HookMacros(val c: Context) extends MacroUtils { $ScalaFn.fromBoxed($JsFn.fromJsFn[${Box(P)}, $C](rawComponent)($summoner)) """) } + + // ------------------------------------------------------------------------------------------------------------------- + + private def call(function: Tree, args: List[Tree]): Tree = { + import internal._ + + function match { + case Function(params, body) => + + // From scala/test/files/run/macro-range/Common_1.scala + class TreeSubstituter(from: List[Symbol], to: List[Tree]) extends Transformer { + override def transform(tree: Tree): Tree = tree match { + case Ident(_) => + def subst(from: List[Symbol], to: List[Tree]): Tree = + if (from.isEmpty) tree + else if (tree.symbol == from.head) to.head.duplicate + else subst(from.tail, to.tail); + subst(from, to) + case _ => + val tree1 = super.transform(tree) + if (tree1 ne tree) setType(tree1, null) + tree1 + } + } + val t = new TreeSubstituter(params.map(_.symbol), args) + t.transform(body) + + case _ => + Apply(Select(function, TermName("apply")), args) + } + } } diff --git a/library/coreGeneric/src/main/scala/japgolly/scalajs/react/internal/Box.scala b/library/coreGeneric/src/main/scala/japgolly/scalajs/react/internal/Box.scala index 7eb205eb9..fc7ab71bc 100644 --- a/library/coreGeneric/src/main/scala/japgolly/scalajs/react/internal/Box.scala +++ b/library/coreGeneric/src/main/scala/japgolly/scalajs/react/internal/Box.scala @@ -9,7 +9,7 @@ trait Box[+A] extends js.Object { } object Box { - @inline def apply[A](value: A): Box[A] = + def apply[A](value: A): Box[A] = js.Dynamic.literal(a = value.asInstanceOf[js.Any]).asInstanceOf[Box[A]] val Unit: Box[Unit] = diff --git a/library/coreGeneric/src/main/scala/japgolly/scalajs/react/internal/MacroLogger.scala b/library/coreGeneric/src/main/scala/japgolly/scalajs/react/internal/MacroLogger.scala index 2b3bdec44..fc2bacffd 100644 --- a/library/coreGeneric/src/main/scala/japgolly/scalajs/react/internal/MacroLogger.scala +++ b/library/coreGeneric/src/main/scala/japgolly/scalajs/react/internal/MacroLogger.scala @@ -69,4 +69,15 @@ class MacroLogger { def apply(name: => Any, value: => Any)(implicit l: Line): Unit = apply(s"$YELLOW$name:$RESET $value") + + def all(name: => Any, values: => Iterable[Any])(implicit l: Line): Unit = + if (enabled) { + val vs = values + val total = vs.size + var i = 0 + for (v <- vs) { + i += 1 + apply(s"$name [$i/$total]", v) + } + } } diff --git a/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/HooksWithChildren.scala b/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/HooksWithChildren.scala new file mode 100644 index 000000000..8254de9e8 --- /dev/null +++ b/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/HooksWithChildren.scala @@ -0,0 +1,19 @@ +package japgolly.scalajs.react.test.emissions + +import japgolly.scalajs.react._ +import japgolly.scalajs.react.vdom.html_<^._ + +object HooksWithChildren { + + val Component = ScalaFnComponent.withHooks[Int] + .withPropsChildren + .useState(123) + .render { (p, c, s1) => + val sum = p + s1.value + c.count + <.button( + "Sum = ", sum, + ^.onClick --> s1.modState(_ + 1), + c + ) + } +} diff --git a/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/Main.scala b/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/Main.scala index fe01e6602..f4d324aaf 100644 --- a/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/Main.scala +++ b/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/Main.scala @@ -14,7 +14,9 @@ object Main { private val Component = ScalaFnComponent[Unit] { _ => <.div( - UseState.Component(), + HooksWithChildren.Component(0)(<.div), + NoHooksWithChildren.Component(0)(<.div), + UseState.Component(0), ) } } diff --git a/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/NoHooksWithChildren.scala b/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/NoHooksWithChildren.scala new file mode 100644 index 000000000..38090e67b --- /dev/null +++ b/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/NoHooksWithChildren.scala @@ -0,0 +1,14 @@ +package japgolly.scalajs.react.test.emissions + +import japgolly.scalajs.react._ +import japgolly.scalajs.react.vdom.html_<^._ + +object NoHooksWithChildren { + + val Component = ScalaFnComponent.withHooks[Int] + .withPropsChildren + .render { (p, c) => + val sum = p + c.count + <.div("DEBUG = ", sum) + } +} diff --git a/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/UseState.scala b/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/UseState.scala index e685d6bd1..fa0e3bd99 100644 --- a/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/UseState.scala +++ b/library/testEmissions/js/src/main/scala/japgolly/scalajs/react/test/emissions/UseState.scala @@ -5,12 +5,15 @@ import japgolly.scalajs.react.vdom.html_<^._ object UseState { - val Component = ScalaFnComponent.withHooks[Unit] + val Component = ScalaFnComponent.withHooks[Int] .useState(123) - .render { (_, s) => + .useStateBy((p, s1) => p + s1.value) + .useStateBy($ => $.props + $.hook1.value + $.hook2.value) + .render { (_, s1, s2, s3) => + val sum = s1.value + s2.value + s3.value <.button( - "Count is ", s.value, - ^.onClick --> s.modState(_ + 1), + "Sum = ", sum, + ^.onClick --> s1.modState(_ + 1), ) } } diff --git a/library/testEmissions/jvm/src/test/resources/rr-js/fn-in.js b/library/testEmissions/jvm/src/test/resources/rr-js/fn-in.js new file mode 100644 index 000000000..d3ce852c3 --- /dev/null +++ b/library/testEmissions/jvm/src/test/resources/rr-js/fn-in.js @@ -0,0 +1,7 @@ +import React from 'react' + +function App() { + return React.createElement("div", {}, "count is: 0"); +} + +export default App diff --git a/library/testEmissions/jvm/src/test/resources/rr-js/fn-out.js b/library/testEmissions/jvm/src/test/resources/rr-js/fn-out.js new file mode 100644 index 000000000..db4a7b468 --- /dev/null +++ b/library/testEmissions/jvm/src/test/resources/rr-js/fn-out.js @@ -0,0 +1,12 @@ +import React from 'react'; + +function App() { + return React.createElement("div", {}, "count is: 0"); +} + +_c = App; +export default App; + +var _c; + +$RefreshReg$(_c, "App"); diff --git a/library/testEmissions/jvm/src/test/resources/rr-js/demo-in.js b/library/testEmissions/jvm/src/test/resources/rr-js/hooks-in.js similarity index 100% rename from library/testEmissions/jvm/src/test/resources/rr-js/demo-in.js rename to library/testEmissions/jvm/src/test/resources/rr-js/hooks-in.js diff --git a/library/testEmissions/jvm/src/test/resources/rr-js/demo-out.js b/library/testEmissions/jvm/src/test/resources/rr-js/hooks-out.js similarity index 100% rename from library/testEmissions/jvm/src/test/resources/rr-js/demo-out.js rename to library/testEmissions/jvm/src/test/resources/rr-js/hooks-out.js diff --git a/library/testEmissions/jvm/src/test/resources/rr-sjr/HooksWithChildren-out2.js b/library/testEmissions/jvm/src/test/resources/rr-sjr/HooksWithChildren-out2.js new file mode 100644 index 000000000..0b18166aa --- /dev/null +++ b/library/testEmissions/jvm/src/test/resources/rr-sjr/HooksWithChildren-out2.js @@ -0,0 +1,91 @@ +'use strict'; + +var _s = $RefreshSig$(); + +import * as $i_react from "react"; +import * as $j_japgolly$002escalajs$002ereact$002eCtorType$0024Summoner$0024 from "./japgolly.scalajs.react.CtorType$Summoner$.js"; +import * as $j_japgolly$002escalajs$002ereact$002ePropsChildren from "./japgolly.scalajs.react.PropsChildren.js"; +import * as $j_japgolly$002escalajs$002ereact$002eReusable$0024 from "./japgolly.scalajs.react.Reusable$.js"; +import * as $j_japgolly$002escalajs$002ereact$002ecallback$002eCallbackTo from "./japgolly.scalajs.react.callback.CallbackTo.js"; +import * as $j_japgolly$002escalajs$002ereact$002ecomponent$002eJsFn$0024 from "./japgolly.scalajs.react.component.JsFn$.js"; +import * as $j_japgolly$002escalajs$002ereact$002ecomponent$002eScalaFn$0024 from "./japgolly.scalajs.react.component.ScalaFn$.js"; +import * as $j_japgolly$002escalajs$002ereact$002ehooks$002eHooks$0024UseState$0024 from "./japgolly.scalajs.react.hooks.Hooks$UseState$.js"; +import * as $j_japgolly$002escalajs$002ereact$002ehooks$002eHooks$0024UseStateF from "./japgolly.scalajs.react.hooks.Hooks$UseStateF.js"; +import * as $j_japgolly$002escalajs$002ereact$002einternal$002eBox$0024 from "./japgolly.scalajs.react.internal.Box$.js"; +import * as $j_japgolly$002escalajs$002ereact$002einternal$002eSingleton$0024 from "./japgolly.scalajs.react.internal.Singleton$.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002eAttr$0024EventCallback$0024 from "./japgolly.scalajs.react.vdom.Attr$EventCallback$.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002eExports$0024 from "./japgolly.scalajs.react.vdom.Exports$.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002eHtmlTagOf$0024 from "./japgolly.scalajs.react.vdom.HtmlTagOf$.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002eVdomElement from "./japgolly.scalajs.react.vdom.VdomElement.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024 from "./japgolly.scalajs.react.vdom.VdomNode$.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024$0024anon$00241 from "./japgolly.scalajs.react.vdom.VdomNode$$anon$1.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002ehtml$005f$0024less$0024up$0024 from "./japgolly.scalajs.react.vdom.html_$less$up$.js"; +import * as $j_java$002elang$002eObject from "./java.lang.Object.js"; +import * as $j_scala$002eFunction1 from "./scala.Function1.js"; +import * as $j_scala$002escalajs$002eruntime$002eAnonFunction0 from "./scala.scalajs.runtime.AnonFunction0.js"; +import * as $j_scala$002escalajs$002eruntime$002eWrappedVarArgs from "./scala.scalajs.runtime.WrappedVarArgs.js"; + +function $c_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$() { + this.Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$__f_Component = null; + $n_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$ = this; + + var rawComponent = arg1$2 => $m_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$().japgolly$scalajs$react$test$emissions$HooksWithChildren$$$anonfun$Component$1__Ljapgolly_scalajs_react_internal_Box__sjs_js_$bar(arg1$2); + + var $$x1 = $j_japgolly$002escalajs$002ereact$002ecomponent$002eScalaFn$0024.$m_Ljapgolly_scalajs_react_component_ScalaFn$(); + var this$1 = $j_japgolly$002escalajs$002ereact$002ecomponent$002eJsFn$0024.$m_Ljapgolly_scalajs_react_component_JsFn$(); + var s = $j_japgolly$002escalajs$002ereact$002eCtorType$0024Summoner$0024.$m_Ljapgolly_scalajs_react_CtorType$Summoner$().summonPC__Ljapgolly_scalajs_react_internal_Singleton$Not__Ljapgolly_scalajs_react_CtorType$Summoner(($j_japgolly$002escalajs$002ereact$002einternal$002eSingleton$0024.$m_Ljapgolly_scalajs_react_internal_Singleton$(), null)); + this.Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$__f_Component = $$x1.fromBoxed__Ljapgolly_scalajs_react_component_JsBaseComponentTemplate$ComponentWithRoot__Ljapgolly_scalajs_react_component_JsBaseComponentTemplate$ComponentWithRoot(this$1.force__O__Ljapgolly_scalajs_react_CtorType$Summoner__Ljapgolly_scalajs_react_component_JsBaseComponentTemplate$ComponentWithRoot(rawComponent, s)); +} + +export { $c_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$ }; +$c_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$.prototype = new $j_java$002elang$002eObject.$h_O(); +$c_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$.prototype.constructor = $c_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$; + +function $h_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$() {} + +export { $h_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$ }; +$h_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$.prototype = $c_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$.prototype; +$c_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$.prototype.japgolly$scalajs$react$test$emissions$HooksWithChildren$$$anonfun$Component$1__Ljapgolly_scalajs_react_internal_Box__sjs_js_$bar = _s(function (props) { + _s(); + + var children = $j_japgolly$002escalajs$002ereact$002ePropsChildren.$m_Ljapgolly_scalajs_react_PropsChildren$().fromRawProps__sjs_js_Object__sjs_js_Any(props); + var hook1_raw = $i_react.useState(() => { + $m_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$(); + return $j_japgolly$002escalajs$002ereact$002einternal$002eBox$0024.$m_Ljapgolly_scalajs_react_internal_Box$().apply__O__Ljapgolly_scalajs_react_internal_Box(123); + }); + var hook1 = $j_japgolly$002escalajs$002ereact$002ehooks$002eHooks$0024UseState$0024.$m_Ljapgolly_scalajs_react_hooks_Hooks$UseState$().fromJsBoxed__sjs_js_Tuple2__Ljapgolly_scalajs_react_hooks_Hooks$UseStateF(hook1_raw); + var sum = ($j_java$002elang$002eObject.$uI(props.a) + $j_java$002elang$002eObject.$uI(hook1.Ljapgolly_scalajs_react_hooks_Hooks$UseStateF$$anon$2__f_raw[0]) | 0) + $j_japgolly$002escalajs$002ereact$002ePropsChildren.$m_Ljapgolly_scalajs_react_PropsChildren$().count$extension__sjs_js_Any__I(children) | 0; + var $$x1 = $j_japgolly$002escalajs$002ereact$002evdom$002eHtmlTagOf$0024.$m_Ljapgolly_scalajs_react_vdom_HtmlTagOf$(); + $j_japgolly$002escalajs$002ereact$002evdom$002ehtml$005f$0024less$0024up$0024.$m_Ljapgolly_scalajs_react_vdom_html_$less$up$(); + $j_japgolly$002escalajs$002ereact$002evdom$002eExports$0024.$m_Ljapgolly_scalajs_react_vdom_Exports$(); + var array = [($j_japgolly$002escalajs$002ereact$002evdom$002ehtml$005f$0024less$0024up$0024.$m_Ljapgolly_scalajs_react_vdom_html_$less$up$(), $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024.$m_Ljapgolly_scalajs_react_vdom_VdomNode$(), new $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024$0024anon$00241.$c_Ljapgolly_scalajs_react_vdom_VdomNode$$anon$1("Sum = ")), ($j_japgolly$002escalajs$002ereact$002evdom$002ehtml$005f$0024less$0024up$0024.$m_Ljapgolly_scalajs_react_vdom_html_$less$up$(), $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024.$m_Ljapgolly_scalajs_react_vdom_VdomNode$(), new $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024$0024anon$00241.$c_Ljapgolly_scalajs_react_vdom_VdomNode$$anon$1(sum)), $j_japgolly$002escalajs$002ereact$002evdom$002ehtml$005f$0024less$0024up$0024.$m_Ljapgolly_scalajs_react_vdom_html_$less$up$().Ljapgolly_scalajs_react_vdom_html_$less$up$__f_$up.Ljapgolly_scalajs_react_vdom_HtmlAttrAndStyles$__f_onClick.$minus$minus$greater__F0__F1__Ljapgolly_scalajs_react_vdom_TagMod(new $j_scala$002escalajs$002eruntime$002eAnonFunction0.$c_sjsr_AnonFunction0((hook1$1 => () => { + $j_japgolly$002escalajs$002ereact$002eReusable$0024.$m_Ljapgolly_scalajs_react_Reusable$(); + var r = $j_japgolly$002escalajs$002ereact$002ehooks$002eHooks$0024UseStateF.$f_Ljapgolly_scalajs_react_hooks_Hooks$UseStateF__modState__Ljapgolly_scalajs_react_Reusable(hook1$1); + return new $j_japgolly$002escalajs$002ereact$002ecallback$002eCallbackTo.$c_Ljapgolly_scalajs_react_callback_CallbackTo($j_japgolly$002escalajs$002ereact$002ecallback$002eCallbackTo.$as_Ljapgolly_scalajs_react_callback_CallbackTo($j_scala$002eFunction1.$as_F1(r.value__O()).apply__O__O(new $j_scala$002eFunction1.$c_sjsr_AnonFunction1(x$1$2 => { + var x$1 = $j_java$002elang$002eObject.$uI(x$1$2); + return 1 + x$1 | 0; + }))).Ljapgolly_scalajs_react_callback_CallbackTo__f_japgolly$scalajs$react$callback$CallbackTo$$trampoline); + })(hook1)), $j_japgolly$002escalajs$002ereact$002evdom$002eAttr$0024EventCallback$0024.$m_Ljapgolly_scalajs_react_vdom_Attr$EventCallback$().Ljapgolly_scalajs_react_vdom_Attr$EventCallback$__f_defaultSync), ($j_japgolly$002escalajs$002ereact$002evdom$002ehtml$005f$0024less$0024up$0024.$m_Ljapgolly_scalajs_react_vdom_html_$less$up$(), $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024.$m_Ljapgolly_scalajs_react_vdom_VdomNode$(), new $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024$0024anon$00241.$c_Ljapgolly_scalajs_react_vdom_VdomNode$$anon$1(children))]; + var this$17 = $$x1.apply$extension__T__sci_Seq__Ljapgolly_scalajs_react_vdom_TagOf("button", new $j_scala$002escalajs$002eruntime$002eWrappedVarArgs.$c_sjsr_WrappedVarArgs(array)); + return $j_japgolly$002escalajs$002ereact$002evdom$002eVdomElement.$f_Ljapgolly_scalajs_react_vdom_VdomElement__rawNode__sjs_js_$bar(this$17); +}, "PgpFLcjq+HHAg1XBIX3s6h+EMrQ="); +var $d_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$ = new $j_java$002elang$002eObject.$TypeData().initClass({ + Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$: 0 +}, false, "japgolly.scalajs.react.test.emissions.HooksWithChildren$", { + Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$: 1, + O: 1 +}); +export { $d_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$ }; +$c_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$.prototype.$classData = $d_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$; +var $n_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$; + +function $m_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$() { + if (!$n_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$) { + $n_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$ = new $c_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$(); + } + + ; + return $n_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$; +} + +export { $m_Ljapgolly_scalajs_react_test_emissions_HooksWithChildren$ }; diff --git a/library/testEmissions/jvm/src/test/resources/rr-sjr/UseState-out2.js b/library/testEmissions/jvm/src/test/resources/rr-sjr/UseState-out2.js new file mode 100644 index 000000000..ace1c539c --- /dev/null +++ b/library/testEmissions/jvm/src/test/resources/rr-sjr/UseState-out2.js @@ -0,0 +1,110 @@ +'use strict'; + +var _s = $RefreshSig$(); + +import * as $i_react from "react"; +import * as $j_japgolly$002escalajs$002ereact$002eCtorType$0024Summoner$0024 from "./japgolly.scalajs.react.CtorType$Summoner$.js"; +import * as $j_japgolly$002escalajs$002ereact$002eReusable$0024 from "./japgolly.scalajs.react.Reusable$.js"; +import * as $j_japgolly$002escalajs$002ereact$002ecallback$002eCallbackTo from "./japgolly.scalajs.react.callback.CallbackTo.js"; +import * as $j_japgolly$002escalajs$002ereact$002ecomponent$002eJsFn$0024 from "./japgolly.scalajs.react.component.JsFn$.js"; +import * as $j_japgolly$002escalajs$002ereact$002ecomponent$002eScalaFn$0024 from "./japgolly.scalajs.react.component.ScalaFn$.js"; +import * as $j_japgolly$002escalajs$002ereact$002ehooks$002eHookCtx$0024P2 from "./japgolly.scalajs.react.hooks.HookCtx$P2.js"; +import * as $j_japgolly$002escalajs$002ereact$002ehooks$002eHooks$0024UseState$0024 from "./japgolly.scalajs.react.hooks.Hooks$UseState$.js"; +import * as $j_japgolly$002escalajs$002ereact$002ehooks$002eHooks$0024UseStateF from "./japgolly.scalajs.react.hooks.Hooks$UseStateF.js"; +import * as $j_japgolly$002escalajs$002ereact$002einternal$002eBox$0024 from "./japgolly.scalajs.react.internal.Box$.js"; +import * as $j_japgolly$002escalajs$002ereact$002einternal$002eSingleton$0024 from "./japgolly.scalajs.react.internal.Singleton$.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002eAttr$0024EventCallback$0024 from "./japgolly.scalajs.react.vdom.Attr$EventCallback$.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002eExports$0024 from "./japgolly.scalajs.react.vdom.Exports$.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002eHtmlTagOf$0024 from "./japgolly.scalajs.react.vdom.HtmlTagOf$.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002eVdomElement from "./japgolly.scalajs.react.vdom.VdomElement.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024 from "./japgolly.scalajs.react.vdom.VdomNode$.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024$0024anon$00241 from "./japgolly.scalajs.react.vdom.VdomNode$$anon$1.js"; +import * as $j_japgolly$002escalajs$002ereact$002evdom$002ehtml$005f$0024less$0024up$0024 from "./japgolly.scalajs.react.vdom.html_$less$up$.js"; +import * as $j_java$002elang$002eObject from "./java.lang.Object.js"; +import * as $j_scala$002eFunction1 from "./scala.Function1.js"; +import * as $j_scala$002escalajs$002eruntime$002eAnonFunction0 from "./scala.scalajs.runtime.AnonFunction0.js"; +import * as $j_scala$002escalajs$002eruntime$002eWrappedVarArgs from "./scala.scalajs.runtime.WrappedVarArgs.js"; + +function $c_Ljapgolly_scalajs_react_test_emissions_UseState$() { + this.Ljapgolly_scalajs_react_test_emissions_UseState$__f_Component = null; + $n_Ljapgolly_scalajs_react_test_emissions_UseState$ = this; + + var rawComponent = arg1$2 => $m_Ljapgolly_scalajs_react_test_emissions_UseState$().japgolly$scalajs$react$test$emissions$UseState$$$anonfun$Component$1__Ljapgolly_scalajs_react_internal_Box__sjs_js_$bar(arg1$2); + + var $$x1 = $j_japgolly$002escalajs$002ereact$002ecomponent$002eScalaFn$0024.$m_Ljapgolly_scalajs_react_component_ScalaFn$(); + var this$1 = $j_japgolly$002escalajs$002ereact$002ecomponent$002eJsFn$0024.$m_Ljapgolly_scalajs_react_component_JsFn$(); + var s = $j_japgolly$002escalajs$002ereact$002eCtorType$0024Summoner$0024.$m_Ljapgolly_scalajs_react_CtorType$Summoner$().summonP__Ljapgolly_scalajs_react_internal_Singleton$Not__Ljapgolly_scalajs_react_CtorType$Summoner(($j_japgolly$002escalajs$002ereact$002einternal$002eSingleton$0024.$m_Ljapgolly_scalajs_react_internal_Singleton$(), null)); + this.Ljapgolly_scalajs_react_test_emissions_UseState$__f_Component = $$x1.fromBoxed__Ljapgolly_scalajs_react_component_JsBaseComponentTemplate$ComponentWithRoot__Ljapgolly_scalajs_react_component_JsBaseComponentTemplate$ComponentWithRoot(this$1.force__O__Ljapgolly_scalajs_react_CtorType$Summoner__Ljapgolly_scalajs_react_component_JsBaseComponentTemplate$ComponentWithRoot(rawComponent, s)); +} + +export { $c_Ljapgolly_scalajs_react_test_emissions_UseState$ }; +$c_Ljapgolly_scalajs_react_test_emissions_UseState$.prototype = new $j_java$002elang$002eObject.$h_O(); +$c_Ljapgolly_scalajs_react_test_emissions_UseState$.prototype.constructor = $c_Ljapgolly_scalajs_react_test_emissions_UseState$; + +function $h_Ljapgolly_scalajs_react_test_emissions_UseState$() {} + +export { $h_Ljapgolly_scalajs_react_test_emissions_UseState$ }; +$h_Ljapgolly_scalajs_react_test_emissions_UseState$.prototype = $c_Ljapgolly_scalajs_react_test_emissions_UseState$.prototype; + +$c_Ljapgolly_scalajs_react_test_emissions_UseState$.prototype.japgolly$scalajs$react$test$emissions$UseState$$$anonfun$Component$3__Ljapgolly_scalajs_react_internal_Box__Ljapgolly_scalajs_react_hooks_Hooks$UseStateF__Ljapgolly_scalajs_react_internal_Box = function (props$1, hook1$1) { + return $j_japgolly$002escalajs$002ereact$002einternal$002eBox$0024.$m_Ljapgolly_scalajs_react_internal_Box$().apply__O__Ljapgolly_scalajs_react_internal_Box($j_java$002elang$002eObject.$uI(props$1.a) + $j_java$002elang$002eObject.$uI(hook1$1.Ljapgolly_scalajs_react_hooks_Hooks$UseStateF$$anon$2__f_raw[0]) | 0); +}; + +$c_Ljapgolly_scalajs_react_test_emissions_UseState$.prototype.japgolly$scalajs$react$test$emissions$UseState$$$anonfun$Component$4__Ljapgolly_scalajs_react_hooks_HookCtx$P2__Ljapgolly_scalajs_react_internal_Box = function (hook3_ctx$1) { + var $$x3 = $j_japgolly$002escalajs$002ereact$002einternal$002eBox$0024.$m_Ljapgolly_scalajs_react_internal_Box$(); + var $$x2 = $j_java$002elang$002eObject.$uI(hook3_ctx$1.Ljapgolly_scalajs_react_hooks_HookCtx$P0__f_props); + var this$1 = $j_japgolly$002escalajs$002ereact$002ehooks$002eHooks$0024UseStateF.$as_Ljapgolly_scalajs_react_hooks_Hooks$UseStateF(hook3_ctx$1.Ljapgolly_scalajs_react_hooks_HookCtx$P1__f_hook1); + var $$x1 = $j_java$002elang$002eObject.$uI(this$1.Ljapgolly_scalajs_react_hooks_Hooks$UseStateF$$anon$2__f_raw[0]); + var this$2 = $j_japgolly$002escalajs$002ereact$002ehooks$002eHooks$0024UseStateF.$as_Ljapgolly_scalajs_react_hooks_Hooks$UseStateF(hook3_ctx$1.Ljapgolly_scalajs_react_hooks_HookCtx$P2__f_hook2); + return $$x3.apply__O__Ljapgolly_scalajs_react_internal_Box(($$x2 + $$x1 | 0) + $j_java$002elang$002eObject.$uI(this$2.Ljapgolly_scalajs_react_hooks_Hooks$UseStateF$$anon$2__f_raw[0]) | 0); +}; + +$c_Ljapgolly_scalajs_react_test_emissions_UseState$.prototype.japgolly$scalajs$react$test$emissions$UseState$$$anonfun$Component$1__Ljapgolly_scalajs_react_internal_Box__sjs_js_$bar = _s(function (props) { + _s(); + + var hook1_raw = $i_react.useState(() => { + $m_Ljapgolly_scalajs_react_test_emissions_UseState$(); + return $j_japgolly$002escalajs$002ereact$002einternal$002eBox$0024.$m_Ljapgolly_scalajs_react_internal_Box$().apply__O__Ljapgolly_scalajs_react_internal_Box(123); + }); + var hook1 = $j_japgolly$002escalajs$002ereact$002ehooks$002eHooks$0024UseState$0024.$m_Ljapgolly_scalajs_react_hooks_Hooks$UseState$().fromJsBoxed__sjs_js_Tuple2__Ljapgolly_scalajs_react_hooks_Hooks$UseStateF(hook1_raw); + var hook2_raw = $i_react.useState(((props$1, hook1$1) => () => $m_Ljapgolly_scalajs_react_test_emissions_UseState$().japgolly$scalajs$react$test$emissions$UseState$$$anonfun$Component$3__Ljapgolly_scalajs_react_internal_Box__Ljapgolly_scalajs_react_hooks_Hooks$UseStateF__Ljapgolly_scalajs_react_internal_Box(props$1, hook1$1))(props, hook1)); + var hook2 = $j_japgolly$002escalajs$002ereact$002ehooks$002eHooks$0024UseState$0024.$m_Ljapgolly_scalajs_react_hooks_Hooks$UseState$().fromJsBoxed__sjs_js_Tuple2__Ljapgolly_scalajs_react_hooks_Hooks$UseStateF(hook2_raw); + var props$2 = props.a; + var hook3_ctx = new $j_japgolly$002escalajs$002ereact$002ehooks$002eHookCtx$0024P2.$c_Ljapgolly_scalajs_react_hooks_HookCtx$P2(props$2, hook1, hook2); + var hook3_raw = $i_react.useState((hook3_ctx$1 => () => $m_Ljapgolly_scalajs_react_test_emissions_UseState$().japgolly$scalajs$react$test$emissions$UseState$$$anonfun$Component$4__Ljapgolly_scalajs_react_hooks_HookCtx$P2__Ljapgolly_scalajs_react_internal_Box(hook3_ctx$1))(hook3_ctx)); + var hook3 = $j_japgolly$002escalajs$002ereact$002ehooks$002eHooks$0024UseState$0024.$m_Ljapgolly_scalajs_react_hooks_Hooks$UseState$().fromJsBoxed__sjs_js_Tuple2__Ljapgolly_scalajs_react_hooks_Hooks$UseStateF(hook3_raw); + var sum = ($j_java$002elang$002eObject.$uI(hook1.Ljapgolly_scalajs_react_hooks_Hooks$UseStateF$$anon$2__f_raw[0]) + $j_java$002elang$002eObject.$uI(hook2.Ljapgolly_scalajs_react_hooks_Hooks$UseStateF$$anon$2__f_raw[0]) | 0) + $j_java$002elang$002eObject.$uI(hook3.Ljapgolly_scalajs_react_hooks_Hooks$UseStateF$$anon$2__f_raw[0]) | 0; + var $$x1 = $j_japgolly$002escalajs$002ereact$002evdom$002eHtmlTagOf$0024.$m_Ljapgolly_scalajs_react_vdom_HtmlTagOf$(); + $j_japgolly$002escalajs$002ereact$002evdom$002ehtml$005f$0024less$0024up$0024.$m_Ljapgolly_scalajs_react_vdom_html_$less$up$(); + $j_japgolly$002escalajs$002ereact$002evdom$002eExports$0024.$m_Ljapgolly_scalajs_react_vdom_Exports$(); + var array = [($j_japgolly$002escalajs$002ereact$002evdom$002ehtml$005f$0024less$0024up$0024.$m_Ljapgolly_scalajs_react_vdom_html_$less$up$(), $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024.$m_Ljapgolly_scalajs_react_vdom_VdomNode$(), new $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024$0024anon$00241.$c_Ljapgolly_scalajs_react_vdom_VdomNode$$anon$1("Sum = ")), ($j_japgolly$002escalajs$002ereact$002evdom$002ehtml$005f$0024less$0024up$0024.$m_Ljapgolly_scalajs_react_vdom_html_$less$up$(), $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024.$m_Ljapgolly_scalajs_react_vdom_VdomNode$(), new $j_japgolly$002escalajs$002ereact$002evdom$002eVdomNode$0024$0024anon$00241.$c_Ljapgolly_scalajs_react_vdom_VdomNode$$anon$1(sum)), $j_japgolly$002escalajs$002ereact$002evdom$002ehtml$005f$0024less$0024up$0024.$m_Ljapgolly_scalajs_react_vdom_html_$less$up$().Ljapgolly_scalajs_react_vdom_html_$less$up$__f_$up.Ljapgolly_scalajs_react_vdom_HtmlAttrAndStyles$__f_onClick.$minus$minus$greater__F0__F1__Ljapgolly_scalajs_react_vdom_TagMod(new $j_scala$002escalajs$002eruntime$002eAnonFunction0.$c_sjsr_AnonFunction0((hook1$2 => () => { + $j_japgolly$002escalajs$002ereact$002eReusable$0024.$m_Ljapgolly_scalajs_react_Reusable$(); + var r = $j_japgolly$002escalajs$002ereact$002ehooks$002eHooks$0024UseStateF.$f_Ljapgolly_scalajs_react_hooks_Hooks$UseStateF__modState__Ljapgolly_scalajs_react_Reusable(hook1$2); + return new $j_japgolly$002escalajs$002ereact$002ecallback$002eCallbackTo.$c_Ljapgolly_scalajs_react_callback_CallbackTo($j_japgolly$002escalajs$002ereact$002ecallback$002eCallbackTo.$as_Ljapgolly_scalajs_react_callback_CallbackTo($j_scala$002eFunction1.$as_F1(r.value__O()).apply__O__O(new $j_scala$002eFunction1.$c_sjsr_AnonFunction1(x$2$2 => { + var x$2 = $j_java$002elang$002eObject.$uI(x$2$2); + return 1 + x$2 | 0; + }))).Ljapgolly_scalajs_react_callback_CallbackTo__f_japgolly$scalajs$react$callback$CallbackTo$$trampoline); + })(hook1)), $j_japgolly$002escalajs$002ereact$002evdom$002eAttr$0024EventCallback$0024.$m_Ljapgolly_scalajs_react_vdom_Attr$EventCallback$().Ljapgolly_scalajs_react_vdom_Attr$EventCallback$__f_defaultSync)]; + var this$15 = $$x1.apply$extension__T__sci_Seq__Ljapgolly_scalajs_react_vdom_TagOf("button", new $j_scala$002escalajs$002eruntime$002eWrappedVarArgs.$c_sjsr_WrappedVarArgs(array)); + return $j_japgolly$002escalajs$002ereact$002evdom$002eVdomElement.$f_Ljapgolly_scalajs_react_vdom_VdomElement__rawNode__sjs_js_$bar(this$15); +}, "qo8Jb8XvrnzpV9ZPNQN0hbh5dNs="); +var $d_Ljapgolly_scalajs_react_test_emissions_UseState$ = new $j_java$002elang$002eObject.$TypeData().initClass({ + Ljapgolly_scalajs_react_test_emissions_UseState$: 0 +}, false, "japgolly.scalajs.react.test.emissions.UseState$", { + Ljapgolly_scalajs_react_test_emissions_UseState$: 1, + O: 1 +}); +export { $d_Ljapgolly_scalajs_react_test_emissions_UseState$ }; +$c_Ljapgolly_scalajs_react_test_emissions_UseState$.prototype.$classData = $d_Ljapgolly_scalajs_react_test_emissions_UseState$; +var $n_Ljapgolly_scalajs_react_test_emissions_UseState$; + +function $m_Ljapgolly_scalajs_react_test_emissions_UseState$() { + if (!$n_Ljapgolly_scalajs_react_test_emissions_UseState$) { + $n_Ljapgolly_scalajs_react_test_emissions_UseState$ = new $c_Ljapgolly_scalajs_react_test_emissions_UseState$(); + } + + ; + return $n_Ljapgolly_scalajs_react_test_emissions_UseState$; +} + +export { $m_Ljapgolly_scalajs_react_test_emissions_UseState$ }; diff --git a/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/EmissionTest.scala b/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/EmissionTest.scala index 67c479ea3..1991221e9 100644 --- a/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/EmissionTest.scala +++ b/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/EmissionTest.scala @@ -9,10 +9,10 @@ object EmissionTest extends TestSuite { override def tests = Tests { - "UseState" - testScala(hack = TestJs.Hack { _ - .dropLinesUntil(_ endsWith " = function (props) {") - .takeLinesTo(_ == "};") - }, golden = false) + // "UseState" - testScala(hack = TestJs.Hack { _ + // .dropLinesUntil(_ endsWith " = function (props) {") + // .takeLinesTo(_ == "};") + // }.disable, golden = false) } diff --git a/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/ReactRefreshTest.scala b/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/ReactRefreshTest.scala index df62368ce..e3de34f93 100644 --- a/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/ReactRefreshTest.scala +++ b/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/ReactRefreshTest.scala @@ -2,6 +2,7 @@ package japgolly.scalajs.react.test.emissions import japgolly.microlibs.utils.FileUtils import japgolly.scalajs.react.test.emissions.util._ +import japgolly.univeq._ import scala.annotation.nowarn import scala.util.Try import utest._ @@ -12,11 +13,16 @@ object ReactRefreshTest extends TestSuite { override def tests = Tests { "js" - { - "demo" - testJs() + "fn" - testJs() + "hooks" - testJs() // "temp" - testJs() } - // "UseState" - showScala() + "sjr" - { + "HooksWithChildren" - testScala() + "NoHooksWithChildren" - showScala() + "UseState" - testScala() + } } // =================================================================================================================== @@ -44,8 +50,8 @@ object ReactRefreshTest extends TestSuite { protected def showScala(assertRR : Boolean = false, assertNoRR : Boolean = false, assertBabelChanges: Boolean = false, - showPreBabel : Boolean = true, - showResult : Boolean = false, + showPreBabel : Boolean = false, + showResult : Boolean = true, showDiff : Boolean = true, golden : Boolean = false, hack : TestJs.Hack = null, @@ -137,7 +143,7 @@ object ReactRefreshTest extends TestSuite { Util.debugShowContent(s"$name.scala JS pre-babel", before, "\u001b[107;30m", rrFlags = false) if (showResult) Util.debugShowContent(s"$name.scala JS post-babel", after, "\u001b[107;30m") - if (showDiff) + if (showDiff && (before !=* after)) Util.debugShowDiff(before, after) } diff --git a/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/util/Babel.scala b/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/util/Babel.scala index 618f069c0..59b469d83 100644 --- a/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/util/Babel.scala +++ b/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/util/Babel.scala @@ -62,8 +62,14 @@ object Babel { private def useCfgNorm = "--config-file=./babel.norm.json" def normaliseToStr(srcFilename: String): String = - Node.babel(srcFilename, useCfgNorm) + if (Util.exists(srcFilename)) + Node.babel(srcFilename, useCfgNorm) + else + throw new RuntimeException("File not found: " + srcFilename) def normaliseToFile(srcFilename: String, tgtFilename: String): Unit = - Node.babel(srcFilename, useCfgNorm, "-o", tgtFilename) + if (Util.exists(srcFilename)) + Node.babel(srcFilename, useCfgNorm, "-o", tgtFilename) + else + throw new RuntimeException("File not found: " + srcFilename) } diff --git a/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/util/TestJs.scala b/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/util/TestJs.scala index 408179ded..8d14045c2 100644 --- a/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/util/TestJs.scala +++ b/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/util/TestJs.scala @@ -35,7 +35,7 @@ object TestJs { val humanReadable: Hack = { // Lines that simply initialise object singletons - val objectInit = """^ *(?:\$j_[a-zA-Z_]+\$\.)?\$m_[a-zA-Z_]+\$\(\);$""".r + val objectInit = """^ *(?:\$j_[a-zA-Z0-9_]+\$\.)?\$m_[a-zA-Z0-9_]+\$\(\); *$""".r apply(_ .modifyLines(_ diff --git a/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/util/Util.scala b/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/util/Util.scala index 3182f694a..ff9840349 100644 --- a/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/util/Util.scala +++ b/library/testEmissions/jvm/src/test/scala/japgolly/scalajs/react/test/emissions/util/Util.scala @@ -30,7 +30,7 @@ object Util { def debugShowContent(name: String, content: String, colour: String, rrFlags: Boolean = true): Unit = { val batPath = "/usr/bin/bat" - var useBat = (new File(batPath)).exists() + var useBat = exists(batPath) val rrDesc = if (rrFlags) { @@ -83,7 +83,7 @@ object Util { def debugShowDiff(content1: String, content2: String): Unit = { var diffPath = "/usr/bin/colordiff" - if (!(new File(diffPath)).exists()) + if (!exists(diffPath)) diffPath = "diff" val file1 = writeToTempFile("")(content1) @@ -95,6 +95,9 @@ object Util { println(sep) } + def exists(filename: String): Boolean = + (new File(filename)).exists() + def getFileContent(path: String): Option[String] = { val f = new File(path) Option.when(f.exists())(readSource(Source.fromFile(f)))