Parcourir la source

[cs] Add using statements via @:csUsing (#8487)

* [cs] Add using statements via @:csUsing

* [cs] Only allow @:cs.using on first type of the module

Otherwise we get a cs compilation error about using clauses needing to
preced all other elements defined in the namespace.

* [cs] Use @:cs.using instead of @csUsing

* Add test for @:cs.using

* Fix gencs.ml
Rudy Ges il y a 5 ans
Parent
commit
da40176a17

+ 7 - 0
src-json/meta.json

@@ -195,6 +195,13 @@
 		"targets": ["TClass", "TEnum"],
 		"internal": true
 	},
+	{
+		"name": "CsUsing",
+		"metadata": ":cs.using",
+		"doc": "Add using directives to your module",
+		"platforms": ["cs"],
+		"targets": ["TClass"]
+	},
 	{
 		"name": "Dce",
 		"metadata": ":dce",

+ 14 - 0
src/generators/gencs.ml

@@ -2792,6 +2792,20 @@ let generate con =
 				| TClassDecl cl ->
 					if not cl.cl_extern then begin
 						(if requires_root then write w "using haxe.root;\n"; newline w;);
+						(if (Meta.has Meta.CsUsing cl.cl_meta) then
+							match (Meta.get Meta.CsUsing cl.cl_meta) with
+								| _,_,p when not file_start ->
+									gen.gcon.error "@:cs.using can only be used on the first type of a module" p
+								| _,[],p ->
+									gen.gcon.error "One or several string constants expected" p
+								| _,e,_ ->
+									(List.iter (fun e ->
+										match e with
+										| (EConst(String(s,_))),_ -> write w (Printf.sprintf "using %s;\n" s)
+										| _,p -> gen.gcon.error "One or several string constants expected" p
+									) e);
+									newline w
+						);
 						gen_class w cl;
 						newline w;
 						newline w

+ 6 - 0
tests/misc/cs/projects/Issue8487/Main1.hx

@@ -0,0 +1,6 @@
+@:cs.using("System")
+class Main1 {
+	public static function main():Void {
+		trace('ok');
+	}
+}

+ 8 - 0
tests/misc/cs/projects/Issue8487/Main2.hx

@@ -0,0 +1,8 @@
+interface I {}
+
+@:cs.using("System")
+class Main2 {
+	public static function main():Void {
+		trace('ko');
+	}
+}

+ 2 - 0
tests/misc/cs/projects/Issue8487/compile-fail.hxml

@@ -0,0 +1,2 @@
+-cs bin
+-main Main2

+ 1 - 0
tests/misc/cs/projects/Issue8487/compile-fail.hxml.stderr

@@ -0,0 +1 @@
+Main2.hx:3: characters 1-11 : @:cs.using can only be used on the first type of a module

+ 2 - 0
tests/misc/cs/projects/Issue8487/compile.hxml

@@ -0,0 +1,2 @@
+-cs bin
+-main Main1