Răsfoiți Sursa

allow TypePath reification (closes #2634)

Simon Krajewski 11 ani în urmă
părinte
comite
83ca4b6e8c
2 a modificat fișierele cu 37 adăugiri și 6 ștergeri
  1. 11 6
      parser.ml
  2. 26 0
      tests/unit/issues/Issue2634.hx

+ 11 - 6
parser.ml

@@ -215,12 +215,17 @@ let reify in_macro =
 		) in
 		mk_enum "TypeParam" n [v] p
 	and to_tpath t p =
-		let fields = [
-			("pack", to_array to_string t.tpackage p);
-			("name", to_string t.tname p);
-			("params", to_array to_tparam t.tparams p);
-		] in
-		to_obj (match t.tsub with None -> fields | Some s -> fields @ ["sub",to_string s p]) p
+		let len = String.length t.tname in
+		if t.tpackage = [] && len > 1 && t.tname.[0] = '$' then
+			(EConst (Ident (String.sub t.tname 1 (len - 1))),p)
+		else begin
+			let fields = [
+				("pack", to_array to_string t.tpackage p);
+				("name", to_string t.tname p);
+				("params", to_array to_tparam t.tparams p);
+			] in
+			to_obj (match t.tsub with None -> fields | Some s -> fields @ ["sub",to_string s p]) p
+		end
 	and to_ctype t p =
 		let ct n vl = mk_enum "ComplexType" n vl p in
 		match t with

+ 26 - 0
tests/unit/issues/Issue2634.hx

@@ -0,0 +1,26 @@
+package unit.issues;
+import haxe.macro.Expr;
+import unit.Test;
+
+class Issue2634 extends Test {
+
+	function test() {
+		var s = mkNew("String", "foo");
+		eq(s, "foo");
+		var t = mkNew("haxe.Template", "bar");
+		eq(t.execute({}), "bar");
+	}
+	
+	macro static function mkNew(dotPath:String, args:Array<Expr>) {
+		var split = dotPath.split(".");
+		var name = split.pop();
+		var tPath = {
+			name: name,
+			pack: split,
+			params: [],
+			sub: null
+		}
+		var cType = TPath(tPath);
+		return macro (new $tPath($a{args}) : $cType);
+	}
+}