Browse Source

Fix field access print order (#9361)

* Improve function type printing, fixes #9353

* Add regression tests for #9353

* Fix TestMacro.hx

* Always print AFinal as last access modifier, fixes #9349

* Add regression test for #9349
George Corney 5 years ago
parent
commit
03326c1b9b
2 changed files with 19 additions and 2 deletions
  1. 8 2
      std/haxe/macro/Printer.hx
  2. 11 0
      tests/unit/src/unit/TestMacro.hx

+ 8 - 2
std/haxe/macro/Printer.hx

@@ -153,7 +153,12 @@ class Printer {
 			case AExtern: "extern";
 		}
 
-	public function printField(field:Field)
+	public function printField(field:Field) {
+		inline function orderAccess(access: Array<Access>) {
+			// final should always be printed last
+			// (does not modify input array)
+			return access.has(AFinal) ? access.filter(a -> !a.match(AFinal)).concat([AFinal]) : access;
+		}
 		return (field.doc != null
 			&& field.doc != "" ? "/**\n"
 				+ tabs
@@ -164,12 +169,13 @@ class Printer {
 				+ "**/\n"
 				+ tabs : "")
 			+ (field.meta != null && field.meta.length > 0 ? field.meta.map(printMetadata).join('\n$tabs') + '\n$tabs' : "")
-			+ (field.access != null && field.access.length > 0 ? field.access.map(printAccess).join(" ") + " " : "")
+			+ (field.access != null && field.access.length > 0 ? orderAccess(field.access).map(printAccess).join(" ") + " " : "")
 			+ switch (field.kind) {
 				case FVar(t, eo): ((field.access != null && field.access.has(AFinal)) ? '' : 'var ') + '${field.name}' + opt(t, printComplexType, " : ") + opt(eo, printExpr, " = ");
 				case FProp(get, set, t, eo): 'var ${field.name}($get, $set)' + opt(t, printComplexType, " : ") + opt(eo, printExpr, " = ");
 				case FFun(func): 'function ${field.name}' + printFunction(func);
 			}
+	}
 
 	public function printTypeParamDecl(tpd:TypeParamDecl)
 		return tpd.name

+ 11 - 0
tests/unit/src/unit/TestMacro.hx

@@ -91,5 +91,16 @@ class TestMacro extends Test {
 		eq(p.printComplexType(macro :(X, Y) -> Z), "(X, Y) -> Z");
 		eq(p.printComplexType(macro :X -> Y -> Z), "(X, Y) -> Z");
 		eq(p.printComplexType(macro :(X -> Y) -> Z), "(X -> Y) -> Z");
+
+		// access order, see #9349
+		eq(
+			p.printField({
+				name: 'x',
+				pos: null,
+				kind: FVar(macro :Any, null),
+				access: [AFinal, AStatic]
+			}),
+			'static final x : Any'
+		);
 	}
 }