Skip to content

Commit 73c7949

Browse files
print arrow functions correctly in haxe.macro.Printer (fixes #9151)
1 parent 9dd62f1 commit 73c7949

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

std/haxe/macro/Printer.hx

+11-4
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,19 @@ class Printer {
178178
public function printFunctionArg(arg:FunctionArg)
179179
return (arg.opt ? "?" : "") + arg.name + opt(arg.type, printComplexType, ":") + opt(arg.value, printExpr, " = ");
180180

181-
public function printFunction(func:Function)
181+
public function printFunction(func:Function, ?kind:FunctionKind) {
182+
var skipParentheses = switch func.args {
183+
case [{ type:null }]: kind == FArrow;
184+
case _: false;
185+
}
182186
return (func.params == null ? "" : func.params.length > 0 ? "<" + func.params.map(printTypeParamDecl).join(", ") + ">" : "")
183-
+ "("
187+
+ (skipParentheses ? "" : "(")
184188
+ func.args.map(printFunctionArg).join(", ")
185-
+ ")"
189+
+ (skipParentheses ? "" : ")")
190+
+ (kind == FArrow ? " ->" : "")
186191
+ opt(func.ret, printComplexType, ":")
187192
+ opt(func.expr, printExpr, " ");
193+
}
188194

189195
public function printVar(v:Var)
190196
return v.name + opt(v.type, printComplexType, ":") + opt(v.expr, printExpr, " = ");
@@ -218,7 +224,7 @@ class Printer {
218224
case EUnop(op, true, e1): printExpr(e1) + printUnop(op);
219225
case EUnop(op, false, e1): printUnop(op) + printExpr(e1);
220226
case EFunction(FNamed(no,inlined), func): (inlined ? 'inline ' : '') + 'function $no' + printFunction(func);
221-
case EFunction(_, func): "function" + printFunction(func);
227+
case EFunction(kind, func): (kind != FArrow ? "function" : "") + printFunction(func, kind);
222228
case EVars(vl): "var " + vl.map(printVar).join(", ");
223229
case EBlock([]): '{ }';
224230
case EBlock(el):
@@ -256,6 +262,7 @@ class Printer {
256262
case EDisplayNew(tp): '#DISPLAY(${printTypePath(tp)})';
257263
case ETernary(econd, eif, eelse): '${printExpr(econd)} ? ${printExpr(eif)} : ${printExpr(eelse)}';
258264
case ECheckType(e1, ct): '(${printExpr(e1)} : ${printComplexType(ct)})';
265+
case EMeta({ name:":implicitReturn" }, { expr:EReturn(e1) }): printExpr(e1);
259266
case EMeta(meta, e1): printMetadata(meta) + " " + printExpr(e1);
260267
}
261268

tests/unit/src/unit/TestMacro.hx

+6
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,11 @@ class TestMacro extends Test {
6666
// special case with 1 argument
6767
parseAndPrint("var a:X -> Y");
6868
parseAndPrint("var a:(X) -> Y");
69+
// local functions
70+
parseAndPrint('a -> b');
71+
parseAndPrint('(a:Int) -> b');
72+
parseAndPrint('(a, b) -> c');
73+
parseAndPrint('function(a) return b');
74+
parseAndPrint('function named(a) return b');
6975
}
7076
}

0 commit comments

Comments
 (0)