[macro] TemplateMacro: move init to constructor
This commit is contained in:
@@ -64,4 +64,21 @@ class MacroUtil {
|
||||
case _: null;
|
||||
}
|
||||
}
|
||||
|
||||
public static function upgradeField(field:Field, expr:Expr, position:Int = 0):Field {
|
||||
switch field.kind {
|
||||
case FFun(f):
|
||||
var fieldExpr = f.expr;
|
||||
switch fieldExpr.expr {
|
||||
case EBlock(exprs):
|
||||
exprs.insert(position, expr);
|
||||
fieldExpr = macro $b{exprs};
|
||||
case _:
|
||||
fieldExpr = macro $b{[fieldExpr, expr]}
|
||||
}
|
||||
f.expr = fieldExpr;
|
||||
case _:
|
||||
}
|
||||
return field;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,30 +154,22 @@ class TemplateMacro extends ClassTypeMacro {
|
||||
}
|
||||
}
|
||||
|
||||
private function buildConstructor(init:Bool, constructor:Field = null):Field {
|
||||
var contstrExprs = [];
|
||||
if (constructor != null) {
|
||||
switch constructor.kind {
|
||||
case FFun(f): contstrExprs.push(f.expr);
|
||||
case _:
|
||||
private function upgradeConstructor(constructor:Field = null):Field {
|
||||
if (constructor == null) {
|
||||
constructor = {
|
||||
name: "new",
|
||||
access: [Access.APublic],
|
||||
pos: getPosition(),
|
||||
kind: FieldType.FFun({
|
||||
args: [],
|
||||
expr: macro super(),
|
||||
params: [],
|
||||
ret: null
|
||||
})
|
||||
}
|
||||
} else {
|
||||
contstrExprs.push(macro super());
|
||||
}
|
||||
contstrExprs.push(macro build());
|
||||
if (init) contstrExprs.push(macro init());
|
||||
|
||||
return {
|
||||
name: "new",
|
||||
access: [Access.APublic],
|
||||
pos: getPosition(),
|
||||
kind: FieldType.FFun({
|
||||
args: [],
|
||||
expr: macro $b{contstrExprs},
|
||||
params: [],
|
||||
ret: null
|
||||
})
|
||||
};
|
||||
MacroUtil.upgradeField(constructor, macro build(), 1);
|
||||
return constructor;
|
||||
}
|
||||
|
||||
private static function findViewsBindings(fields:Array<Field>):Map<String, String> {
|
||||
@@ -206,14 +198,13 @@ class TemplateMacro extends ClassTypeMacro {
|
||||
bindings = findViewsBindings(fields);
|
||||
var result:Array<Field> = fields.slice(0);
|
||||
var exprs:Array<Expr> = [];
|
||||
var init = Lambda.exists(result, function(f) return f.name == "init");
|
||||
createElement("this", template, exprs);
|
||||
result.push(buildBuild(exprs));
|
||||
var constructor = Lambda.find(result, function(f) return f.name == "new");
|
||||
if (constructor != null) {
|
||||
result.remove(constructor);
|
||||
}
|
||||
result.push(buildConstructor(init, constructor));
|
||||
result.push(upgradeConstructor(constructor));
|
||||
if (Lambda.count(bindings) > 0) {
|
||||
var keys = Lambda.map({iterator: bindings.keys}, function(k) return '"${k}"').join(",");
|
||||
Context.error('Invalid @:view bindings: $keys', getPosition());
|
||||
|
||||
Reference in New Issue
Block a user