Forráskód Böngészése

[js] add parens to compound exprs passed to Syntax.code() (fixes #9027)

Aleksandr Kuzmenko 5 éve
szülő
commit
3b233956ea
2 módosított fájl, 26 hozzáadás és 0 törlés
  1. 13 0
      src/generators/genjs.ml
  2. 13 0
      tests/unit/src/unit/issues/Issue9027.hx

+ 13 - 0
src/generators/genjs.ml

@@ -965,6 +965,19 @@ and gen_syntax ctx meth args pos =
 			| [] when code = "this" ->
 				spr ctx (this ctx)
 			| _ ->
+				let rec reveal_expr expr =
+					match expr.eexpr with
+						| TCast (e, _) | TMeta (_, e) -> reveal_expr e
+						| _ -> expr
+				in
+				let args = List.map
+					(fun arg ->
+						match (reveal_expr arg).eexpr with
+							| TIf _ | TBinop _ | TUnop _ -> { arg with eexpr = TParenthesis arg }
+							| _ -> arg
+					)
+					args
+				in
 				Codegen.interpolate_code ctx.com code args (spr ctx) (gen_value ctx) code_pos
 		end
 	| "field" , [eobj;efield] ->

+ 13 - 0
tests/unit/src/unit/issues/Issue9027.hx

@@ -0,0 +1,13 @@
+package unit.issues;
+
+class Issue9027 extends unit.Test {
+#if js
+	static var a:String = "a";
+	static var b:String = "b";
+
+	function test() {
+		eq(2, js.Syntax.code('{0}.length', a + b));
+		eq(2, js.Syntax.code('{0}.length', a < b ? "cd" : "efg"));
+	}
+#end
+}