Browse Source

[es6]fix same name locals in different __init__ methods (#9426)

Aleksandr Kuzmenko 5 years ago
parent
commit
2e9ef25d0f
2 changed files with 26 additions and 10 deletions
  1. 10 10
      src/generators/genjs.ml
  2. 16 0
      tests/misc/es6/Test.hx

+ 10 - 10
src/generators/genjs.ml

@@ -818,25 +818,25 @@ and gen_function ?(keyword="function") ctx f pos =
 	ctx.in_loop <- snd old;
 	ctx.separator <- true
 
-and gen_block_element ?(after=false) ctx e =
+and gen_block_element ?(newline_after=false) ?(keep_blocks=false) ctx e =
 	match e.eexpr with
-	| TBlock el ->
-		List.iter (gen_block_element ~after ctx) el
+	| TBlock el when not keep_blocks ->
+		List.iter (gen_block_element ~newline_after ctx) el
 	| TCall ({ eexpr = TIdent "__feature__" }, { eexpr = TConst (TString f) } :: eif :: eelse) ->
 		if has_feature ctx f then
-			gen_block_element ~after ctx eif
+			gen_block_element ~newline_after ctx eif
 		else (match eelse with
 			| [] -> ()
-			| [e] -> gen_block_element ~after ctx e
+			| [e] -> gen_block_element ~newline_after ctx e
 			| _ -> die "" __LOC__)
 	| TFunction _ ->
-		gen_block_element ~after ctx (mk (TParenthesis e) e.etype e.epos)
+		gen_block_element ~newline_after ctx (mk (TParenthesis e) e.etype e.epos)
 	| TObjectDecl fl ->
-		List.iter (fun (_,e) -> gen_block_element ~after ctx e) fl
+		List.iter (fun (_,e) -> gen_block_element ~newline_after ctx e) fl
 	| _ ->
-		if not after then newline ctx;
+		if not newline_after then newline ctx;
 		gen_expr ctx e;
-		if after then newline ctx
+		if newline_after then newline ctx
 
 and gen_value ctx e =
 	let clear_mapping = add_mapping ctx e in
@@ -1895,7 +1895,7 @@ let generate com =
 		add_feature ctx "js.Lib.global";
 		print ctx "$global.$haxeUID |= 0;\n";
 	end;
-	List.iter (gen_block_element ~after:true ctx) (List.rev ctx.inits);
+	List.iter (gen_block_element ~newline_after:true ~keep_blocks:(ctx.es_version >= 6) ctx) (List.rev ctx.inits);
 	List.iter (generate_static ctx) (List.rev ctx.statics);
 	(match com.main with
 	| None -> ()

+ 16 - 0
tests/misc/es6/Test.hx

@@ -68,6 +68,22 @@ class GrandChildNoArgs extends ChildOneArg {
 	}
 }
 
+class Issue9426_1 {
+	static function __init__() {
+		var sameName = Std.random(10);
+		Test.use(sameName);
+		Test.use(sameName);
+	}
+}
+
+class Issue9426_2 {
+	static function __init__() {
+		var sameName = Std.random(10);
+		Test.use(sameName);
+		Test.use(sameName);
+	}
+}
+
 class Test {
 	public static var calls:Array<String>;
 	@:pure(false) public static function use(v:Any) {}