Skip to content

Commit

Permalink
print arrow functions correctly in haxe.macro.Printer (fixes #9151)
Browse files Browse the repository at this point in the history
  • Loading branch information
RealyUniqueName committed Feb 18, 2020
1 parent 9dd62f1 commit 73c7949
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
15 changes: 11 additions & 4 deletions std/haxe/macro/Printer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,19 @@ class Printer {
public function printFunctionArg(arg:FunctionArg)
return (arg.opt ? "?" : "") + arg.name + opt(arg.type, printComplexType, ":") + opt(arg.value, printExpr, " = ");

public function printFunction(func:Function)
public function printFunction(func:Function, ?kind:FunctionKind) {
var skipParentheses = switch func.args {
case [{ type:null }]: kind == FArrow;
case _: false;
}
return (func.params == null ? "" : func.params.length > 0 ? "<" + func.params.map(printTypeParamDecl).join(", ") + ">" : "")
+ "("
+ (skipParentheses ? "" : "(")
+ func.args.map(printFunctionArg).join(", ")
+ ")"
+ (skipParentheses ? "" : ")")
+ (kind == FArrow ? " ->" : "")
+ opt(func.ret, printComplexType, ":")
+ opt(func.expr, printExpr, " ");
}

public function printVar(v:Var)
return v.name + opt(v.type, printComplexType, ":") + opt(v.expr, printExpr, " = ");
Expand Down Expand Up @@ -218,7 +224,7 @@ class Printer {
case EUnop(op, true, e1): printExpr(e1) + printUnop(op);
case EUnop(op, false, e1): printUnop(op) + printExpr(e1);
case EFunction(FNamed(no,inlined), func): (inlined ? 'inline ' : '') + 'function $no' + printFunction(func);
case EFunction(_, func): "function" + printFunction(func);
case EFunction(kind, func): (kind != FArrow ? "function" : "") + printFunction(func, kind);
case EVars(vl): "var " + vl.map(printVar).join(", ");
case EBlock([]): '{ }';
case EBlock(el):
Expand Down Expand Up @@ -256,6 +262,7 @@ class Printer {
case EDisplayNew(tp): '#DISPLAY(${printTypePath(tp)})';
case ETernary(econd, eif, eelse): '${printExpr(econd)} ? ${printExpr(eif)} : ${printExpr(eelse)}';
case ECheckType(e1, ct): '(${printExpr(e1)} : ${printComplexType(ct)})';
case EMeta({ name:":implicitReturn" }, { expr:EReturn(e1) }): printExpr(e1);
case EMeta(meta, e1): printMetadata(meta) + " " + printExpr(e1);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/unit/src/unit/TestMacro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,11 @@ class TestMacro extends Test {
// special case with 1 argument
parseAndPrint("var a:X -> Y");
parseAndPrint("var a:(X) -> Y");
// local functions
parseAndPrint('a -> b');
parseAndPrint('(a:Int) -> b');
parseAndPrint('(a, b) -> c');
parseAndPrint('function(a) return b');
parseAndPrint('function named(a) return b');
}
}

0 comments on commit 73c7949

Please sign in to comment.