Selaa lähdekoodia

[js] Use "let" for ES6 mode (#9280)

* use "let"

* update to the new var renaming filter
Aleksandr Kuzmenko 5 vuotta sitten
vanhempi
commit
49918598ce
3 muutettua tiedostoa jossa 35 lisäystä ja 10 poistoa
  1. 11 7
      src/context/common.ml
  2. 18 0
      src/filters/renameVars.ml
  3. 6 3
      src/generators/genjs.ml

+ 11 - 7
src/context/common.ml

@@ -125,6 +125,10 @@ type var_scoping_flags =
 		List of names cannot be taken by local vars
 	*)
 	| ReserveNames of string list
+	(**
+		Cases in a `switch` won't have blocks, but will share the same outer scope.
+	*)
+	| SwitchCasesNoBlocks
 
 type var_scoping_config = {
 	vs_flags : var_scoping_flags list;
@@ -394,13 +398,14 @@ let get_config com =
 	| Cross ->
 		default_config
 	| Js ->
+		let es6 = get_es_version com >= 6 in
 		{
 			default_config with
 			pf_static = false;
 			pf_sys = false;
-			pf_capture_policy = CPLoopVars;
+			pf_capture_policy = if es6 then CPNone else CPLoopVars;
 			pf_reserved_type_paths = [([],"Object");([],"Error")];
-			pf_this_before_super = (get_es_version com) < 6; (* cannot access `this` before `super()` when generating ES6 classes *)
+			pf_this_before_super = not es6; (* cannot access `this` before `super()` when generating ES6 classes *)
 			pf_exceptions = { default_config.pf_exceptions with
 				ec_native_throws = [
 					["js";"lib"],"Error";
@@ -408,11 +413,10 @@ let get_config com =
 				];
 			};
 			pf_scoping = {
-				vs_scope = FunctionScope;
-				vs_flags = [
-					VarHoisting;
-					if defined Define.JsUnflatten then ReserveAllTopLevelSymbols else ReserveAllTypesFlat
-				];
+				vs_scope = if es6 then BlockScope else FunctionScope;
+				vs_flags =
+					(if defined Define.JsUnflatten then ReserveAllTopLevelSymbols else ReserveAllTypesFlat)
+					:: if es6 then [NoShadowing; SwitchCasesNoBlocks;] else [VarHoisting];
 			}
 		}
 	| Lua ->

+ 18 - 0
src/filters/renameVars.ml

@@ -8,6 +8,7 @@ type rename_init = {
 	mutable ri_scope : var_scope;
 	mutable ri_hoisting : bool;
 	mutable ri_no_shadowing : bool;
+	mutable ri_switch_cases_no_blocks : bool;
 	mutable ri_reserved : bool StringMap.t;
 	mutable ri_reserve_current_top_level_symbol : bool;
 }
@@ -49,6 +50,7 @@ let init com =
 		ri_reserved = StringMap.empty;
 		ri_hoisting = false;
 		ri_no_shadowing = false;
+		ri_switch_cases_no_blocks = false;
 		ri_reserve_current_top_level_symbol = false;
 	} in
 	reserve_init ri "this";
@@ -58,6 +60,8 @@ let init com =
 			ri.ri_hoisting <- true;
 		| NoShadowing ->
 			ri.ri_no_shadowing <- true;
+		| SwitchCasesNoBlocks ->
+			ri.ri_switch_cases_no_blocks <- true;
 		| ReserveNames names ->
 			List.iter (reserve_init ri) names
 		| ReserveAllTopLevelSymbols ->
@@ -97,6 +101,7 @@ type scope = {
 type rename_context = {
 	rc_hoisting : bool;
 	rc_no_shadowing : bool;
+	rc_switch_cases_no_blocks : bool;
 	rc_scope : var_scope;
 	mutable rc_reserved : bool StringMap.t;
 }
@@ -207,6 +212,13 @@ let rec collect_vars ?(in_block=false) rc scope e =
 			declare_var rc scope v;
 			collect_vars rc scope catch_expr
 		) catches
+	| TSwitch (target, cases, default_opt) when rc.rc_switch_cases_no_blocks ->
+		collect_vars rc scope target;
+		List.iter (fun (el,e) ->
+			List.iter (collect_vars rc scope) el;
+			collect_ignore_block ~in_block rc scope e
+		) cases;
+		Option.may (collect_ignore_block ~in_block rc scope) default_opt
 	| TBlock exprs when rc.rc_scope = BlockScope ->
 		let scope =
 			if in_block then scope (* parent expression is a block too, that means current block will be merged into the parent one *)
@@ -237,6 +249,11 @@ let rec collect_vars ?(in_block=false) rc scope e =
 	| _ ->
 		iter (collect_vars rc scope) e
 
+and collect_ignore_block ?(in_block=false) rc scope e =
+	match e.eexpr with
+	| TBlock el -> List.iter (collect_vars ~in_block rc scope) el
+	| _ -> collect_vars ~in_block rc scope e
+
 let trailing_numbers = Str.regexp "[0-9]+$"
 
 (**
@@ -279,6 +296,7 @@ let run ctx ri e =
 		rc_scope = ri.ri_scope;
 		rc_hoisting = ri.ri_hoisting;
 		rc_no_shadowing = ri.ri_no_shadowing;
+		rc_switch_cases_no_blocks = ri.ri_switch_cases_no_blocks;
 		rc_reserved = ri.ri_reserved;
 	} in
 	if ri.ri_reserve_current_top_level_symbol then begin

+ 6 - 3
src/generators/genjs.ml

@@ -380,6 +380,9 @@ let is_code_injection_function e =
 	| _ ->
 		false
 
+let var ctx =
+	if ctx.es_version >= 6 then "let" else "var"
+
 let rec gen_call ctx e el in_value =
 	match e.eexpr , el with
 	| TConst TSuper , params when ctx.es_version < 6 ->
@@ -648,7 +651,7 @@ and gen_expr ctx e =
 		spr ctx "throw ";
 		gen_value ctx e;
 	| TVar (v,eo) ->
-		spr ctx "var ";
+		spr ctx ((var ctx) ^ " ");
 		check_var_declaration v;
 		spr ctx (ident v.v_name);
 		begin match eo with
@@ -722,7 +725,7 @@ and gen_expr ctx e =
 				let id = ctx.id_counter in
 				ctx.id_counter <- ctx.id_counter + 1;
 				let name = "$it" ^ string_of_int id in
-				print ctx "var %s = " name;
+				print ctx "%s %s = " (var ctx) name;
 				gen_value ctx it;
 				newline ctx;
 				name
@@ -730,7 +733,7 @@ and gen_expr ctx e =
 		print ctx "while( %s.hasNext() ) {" it;
 		let bend = open_block ctx in
 		newline ctx;
-		print ctx "var %s = %s.next()" (ident v.v_name) it;
+		print ctx "%s %s = %s.next()" (var ctx) (ident v.v_name) it;
 		gen_block_element ctx e;
 		bend();
 		newline ctx;