|
| 1 | +package coconut.storybook; |
| 2 | + |
| 3 | +import haxe.macro.Context; |
| 4 | +import haxe.macro.Expr; |
| 5 | +import tink.SyntaxHub; |
| 6 | + |
| 7 | +using StringTools; |
| 8 | +using tink.MacroApi; |
| 9 | + |
| 10 | +class Setup { |
| 11 | + public static function setup() { |
| 12 | + var debug = false; |
| 13 | + SyntaxHub.classLevel.after('tink.lang.Sugar', builder -> { |
| 14 | + if (!builder.target.meta.has(':tink')) |
| 15 | + return false; |
| 16 | + |
| 17 | + for (field in builder) |
| 18 | + switch [field.kind, field.metaNamed(':story'), field.metaNamed(':state')] { |
| 19 | + case [FFun(func), stories, v] if (stories.length > 0): |
| 20 | + // wrap with Isolated |
| 21 | + function subst(e:Expr) |
| 22 | + return switch e { |
| 23 | + case macro return $ret: |
| 24 | + macro return coconut.ui.Isolated.fromHxx({}, {children: $ret}); |
| 25 | + case e: |
| 26 | + e; |
| 27 | + } |
| 28 | + func.expr = func.expr.map(subst); |
| 29 | + |
| 30 | + // add states |
| 31 | + for (states in v) |
| 32 | + for (state in states.params) |
| 33 | + switch state.expr { |
| 34 | + case EVars(vars): |
| 35 | + for (v in vars) { |
| 36 | + if (v.expr == null) |
| 37 | + state.pos.error('@:state var ${v.name} requires a initializer'); |
| 38 | + if (v.type == null) |
| 39 | + state.pos.error('@:state var ${v.name} requires a type hint'); |
| 40 | + |
| 41 | + var name = v.name; |
| 42 | + var alias = getAlias(name); |
| 43 | + var ct = v.type; |
| 44 | + |
| 45 | + var original = func.expr; |
| 46 | + func.expr = macro { |
| 47 | + var $name = new tink.state.State<$ct>(${v.expr}); |
| 48 | + var $alias = $i{name}; |
| 49 | + ${func.expr} |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + case _: |
| 54 | + state.pos.error('Only supports EVars expressions'); |
| 55 | + } |
| 56 | + |
| 57 | + case _: |
| 58 | + // skip |
| 59 | + } |
| 60 | + |
| 61 | + return true; |
| 62 | + }); |
| 63 | + |
| 64 | + SyntaxHub.exprLevel.inward.after(_ -> true, { |
| 65 | + appliesTo: builder -> builder.target.meta.has(':storybook'), |
| 66 | + apply: (e:Expr) -> { |
| 67 | + function transform(name:String, original:Expr, transformed:Expr) { |
| 68 | + return switch Context.getLocalTVars()[name] { |
| 69 | + case null: |
| 70 | + original; |
| 71 | + case {t: _.getID() => 'tink.state.State'}: |
| 72 | + transformed; |
| 73 | + case _: |
| 74 | + original; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + switch e.expr { |
| 79 | + case EConst(CIdent(name)) if (!isAlias(name)): |
| 80 | + transform(name, e, macro $i{getAlias(name)}.value); |
| 81 | + |
| 82 | + case EBinop(OpAssign, macro $i{name}, rhs) if (!isAlias(name)): |
| 83 | + transform(name, e, macro $i{getAlias(name)}.set($rhs)); |
| 84 | + |
| 85 | + case EBinop(OpAssignOp(binop), macro $i{name}, rhs) if (!isAlias(name)): |
| 86 | + var rhs = EBinop(binop, macro $i{getAlias(name)}.value, rhs).at(e.pos); |
| 87 | + transform(name, e, macro $i{getAlias(name)}.set($rhs)); |
| 88 | + |
| 89 | + case _: |
| 90 | + e; |
| 91 | + } |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + inline static function getAlias(name:String) { |
| 97 | + return '__alias_${name}'; |
| 98 | + } |
| 99 | + |
| 100 | + inline static function isAlias(name:String) { |
| 101 | + return name.startsWith('__alias_'); |
| 102 | + } |
| 103 | +} |
0 commit comments