-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9276be1
commit c8a2e6f
Showing
7 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ | |
|
||
-lib coconut.storybook | ||
-lib coconut.vdom | ||
|
||
-D analyzer-optimize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--macro coconut.storybook.Setup.setup() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
-lib coconut.ui | ||
-cp ${SCOPE_DIR}/./src | ||
-cp ${SCOPE_DIR}/../coconut.storybook/src | ||
-D coconut.storybook=0.0.0 | ||
${SCOPE_DIR}/../coconut.storybook/extraParams.hxml | ||
--macro Sys.println("haxe_libraries/coconut.storybook.hxml:3: [Warning] Using dev version of library coconut.storybook") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package coconut.storybook; | ||
|
||
import haxe.macro.Context; | ||
import haxe.macro.Expr; | ||
import tink.SyntaxHub; | ||
|
||
using StringTools; | ||
using tink.MacroApi; | ||
|
||
class Setup { | ||
public static function setup() { | ||
var debug = false; | ||
SyntaxHub.classLevel.after('tink.lang.Sugar', builder -> { | ||
if (!builder.target.meta.has(':tink')) | ||
return false; | ||
|
||
for (field in builder) | ||
switch [field.kind, field.metaNamed(':story'), field.metaNamed(':state')] { | ||
case [FFun(func), stories, v] if (stories.length > 0): | ||
// wrap with Isolated | ||
function subst(e:Expr) | ||
return switch e { | ||
case macro return $ret: | ||
macro return coconut.ui.Isolated.fromHxx({}, {children: $ret}); | ||
case e: | ||
e; | ||
} | ||
func.expr = func.expr.map(subst); | ||
|
||
// add states | ||
for (states in v) | ||
for (state in states.params) | ||
switch state.expr { | ||
case EVars(vars): | ||
for (v in vars) { | ||
if (v.expr == null) | ||
state.pos.error('@:state var ${v.name} requires a initializer'); | ||
if (v.type == null) | ||
state.pos.error('@:state var ${v.name} requires a type hint'); | ||
|
||
var name = v.name; | ||
var alias = getAlias(name); | ||
var ct = v.type; | ||
|
||
var original = func.expr; | ||
func.expr = macro { | ||
var $name = new tink.state.State<$ct>(${v.expr}); | ||
var $alias = $i{name}; | ||
${func.expr} | ||
} | ||
} | ||
|
||
case _: | ||
state.pos.error('Only supports EVars expressions'); | ||
} | ||
|
||
case _: | ||
// skip | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
SyntaxHub.exprLevel.inward.after(_ -> true, { | ||
appliesTo: builder -> builder.target.meta.has(':storybook'), | ||
apply: (e:Expr) -> { | ||
function transform(name:String, original:Expr, transformed:Expr) { | ||
return switch Context.getLocalTVars()[name] { | ||
case null: | ||
original; | ||
case {t: _.getID() => 'tink.state.State'}: | ||
transformed; | ||
case _: | ||
original; | ||
} | ||
} | ||
|
||
switch e.expr { | ||
case EConst(CIdent(name)) if (!isAlias(name)): | ||
transform(name, e, macro $i{getAlias(name)}.value); | ||
|
||
case EBinop(OpAssign, macro $i{name}, rhs) if (!isAlias(name)): | ||
transform(name, e, macro $i{getAlias(name)}.set($rhs)); | ||
|
||
case EBinop(OpAssignOp(binop), macro $i{name}, rhs) if (!isAlias(name)): | ||
var rhs = EBinop(binop, macro $i{getAlias(name)}.value, rhs).at(e.pos); | ||
transform(name, e, macro $i{getAlias(name)}.set($rhs)); | ||
|
||
case _: | ||
e; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
inline static function getAlias(name:String) { | ||
return '__alias_${name}'; | ||
} | ||
|
||
inline static function isAlias(name:String) { | ||
return name.startsWith('__alias_'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters