Преглед на файлове

[js] make js.Syntax.field generate dot access when possible

so we can do Syntax.field(obj, "$some") and have obj.$some in the output
Dan Korostelev преди 7 години
родител
ревизия
ba156c7cab
променени са 2 файла, в които са добавени 14 реда и са изтрити 4 реда
  1. 11 0
      src/generators/genjs.ml
  2. 3 4
      std/js/Syntax.hx

+ 11 - 0
src/generators/genjs.ml

@@ -951,6 +951,17 @@ and gen_syntax ctx meth args pos =
 			| _ ->
 				Codegen.interpolate_code ctx.com code args (spr ctx) (gen_expr ctx) code_pos
 		end
+	| "field" , [eobj;efield] ->
+		gen_value ctx eobj;
+		(match Texpr.skip efield with
+		| { eexpr = TConst(TString(s)) } when valid_js_ident s ->
+			spr ctx ".";
+			spr ctx s;
+		| _ ->
+			spr ctx "[";
+			gen_value ctx efield;
+			spr ctx "]";
+		)
 	| _ ->
 		abort (Printf.sprintf "Unknown js.Syntax method `%s` with %d arguments" meth (List.length args)) pos
 

+ 3 - 4
std/js/Syntax.hx

@@ -56,9 +56,8 @@ extern class Syntax {
 	static function delete(o:Dynamic, f:String):Bool;
 
 	/**
-		Generate `o[f]` expression
+		Generate `o.f` expression, if `f` is a constant string,
+		or `o[f]` if it's any other expression.
 	*/
-	static inline function field(o:Dynamic, f:String):Dynamic {
-		return code('{0}[{1}]', o, f);
-	}
+	static function field(o:Dynamic, f:String):Dynamic;
 }