Skip to content

Commit 606ce62

Browse files
committed
Tests: expose es6 scoping bugs
1 parent b1729bd commit 606ce62

File tree

4 files changed

+239
-9
lines changed

4 files changed

+239
-9
lines changed

compiler/lib/js_assign.ml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,15 @@ let program' (module Strategy : Strategy) p =
389389
free;
390390
let ident = function
391391
| V v -> (
392-
let name = names.(Var.idx v) in
393-
match name, has_free_var with
394-
| "", true -> V v
395-
| "", false -> assert false
396-
| _, (true | false) -> ident ~var:v (Utf8_string.of_string_exn name))
392+
if Config.Flag.stable_var ()
393+
then
394+
ident ~var:v (Utf8_string.of_string_exn (Printf.sprintf "v%d" (Code.Var.idx v)))
395+
else
396+
let name = names.(Var.idx v) in
397+
match name, has_free_var with
398+
| "", true -> V v
399+
| "", false -> assert false
400+
| _, (true | false) -> ident ~var:v (Utf8_string.of_string_exn name))
397401
| x -> x
398402
in
399403
let label_printer = Var_printer.create Var_printer.Alphabet.javascript in

compiler/tests-compiler/dune.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,21 @@
599599
(preprocess
600600
(pps ppx_expect)))
601601

602+
(library
603+
;; compiler/tests-compiler/scopes.ml
604+
(name scopes_15)
605+
(enabled_if true)
606+
(modules scopes)
607+
(libraries js_of_ocaml_compiler unix str jsoo_compiler_expect_tests_helper)
608+
(inline_tests
609+
(enabled_if true)
610+
(deps
611+
(file %{project_root}/compiler/bin-js_of_ocaml/js_of_ocaml.exe)
612+
(file %{project_root}/compiler/bin-jsoo_minify/jsoo_minify.exe)))
613+
(flags (:standard -open Jsoo_compiler_expect_tests_helper))
614+
(preprocess
615+
(pps ppx_expect)))
616+
602617
(library
603618
;; compiler/tests-compiler/side_effect.ml
604619
(name side_effect_15)

compiler/tests-compiler/minify.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ console.log("xx =", xx);
5050
js_prog |> Filetype.js_text_of_string |> Filetype.write_js ~name:"test.js"
5151
in
5252
let js_min_file =
53-
js_file
54-
|> jsoo_minify
55-
~flags:[ "--enable"; "stable_var"; "--enable"; "shortvar" ]
56-
~pretty:false
53+
js_file |> jsoo_minify ~flags:[ "--enable"; "shortvar" ] ~pretty:false
5754
in
5855
print_file (Filetype.path_of_js_file js_file);
5956
run_javascript js_file |> print_endline;

compiler/tests-compiler/scopes.ml

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
(* Js_of_ocaml compiler
2+
* http://www.ocsigen.org/js_of_ocaml/
3+
* Copyright (C) 2022 Hugo Heuzard
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, with linking exception;
8+
* either version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18+
*)
19+
open Util
20+
21+
let test js_prog =
22+
let js_file =
23+
js_prog |> Filetype.js_text_of_string |> Filetype.write_js ~name:"test.js"
24+
in
25+
let js_min_file =
26+
js_file
27+
|> jsoo_minify
28+
~flags:[ "--enable"; "stable_var"; "--enable"; "shortvar" ]
29+
~pretty:true
30+
in
31+
print_file (Filetype.path_of_js_file js_min_file);
32+
run_javascript js_file |> print_endline
33+
34+
let%expect_test "let inside forloop" =
35+
test
36+
{|
37+
(function () {
38+
let x = 2
39+
var y = 0;
40+
for(let x = 0; x < 2; x ++) {
41+
y += x;
42+
}
43+
console.log(y);
44+
console.log(x);
45+
})()
46+
|};
47+
[%expect
48+
{|
49+
$ cat "test.min.js"
50+
1: (function(){
51+
2: let v1 = 2;
52+
3: var v2 = 0;
53+
4: for(let v1 = 0; v1 < 2; v1++) v2 += v1;
54+
5: console.log(v2);
55+
6: console.log(v1);
56+
7: }
57+
8: ());
58+
1
59+
2 |}]
60+
61+
let%expect_test "let inside forin" =
62+
test
63+
{|
64+
(function () {
65+
let x = 2
66+
var y = 0;
67+
var arr = [1,2,3];
68+
for(let x in arr) {
69+
console.log(x);
70+
y += arr[x];
71+
}
72+
console.log(y);
73+
console.log(x);
74+
})()
75+
|};
76+
[%expect
77+
{|
78+
$ cat "test.min.js"
79+
1: (function(){
80+
2: let v2 = 2;
81+
3: var v3 = 0, v1 = [1, 2, 3];
82+
4: for(let v2 in v1){console.log(v2); v3 += v1[v2];}
83+
5: console.log(v3);
84+
6: console.log(v2);
85+
7: }
86+
8: ());
87+
0
88+
1
89+
2
90+
6
91+
2 |}]
92+
93+
let%expect_test "let inside forof" =
94+
test
95+
{|
96+
(function () {
97+
let x = 2
98+
var y = 0;
99+
for(let x of [1,2,3]) {
100+
console.log(x);
101+
y += x;
102+
}
103+
console.log(y);
104+
console.log(x);
105+
})()
106+
|};
107+
[%expect
108+
{|
109+
$ cat "test.min.js"
110+
1: (function(){
111+
2: let v1 = 2;
112+
3: var v2 = 0;
113+
4: for(let v1 of [1, 2, 3]){console.log(v1); v2 += v1;}
114+
5: console.log(v2);
115+
6: console.log(v1);
116+
7: }
117+
8: ());
118+
1
119+
2
120+
3
121+
6
122+
2 |}]
123+
124+
let%expect_test "let inside switch" =
125+
test
126+
{|
127+
(function () {
128+
let x = 2
129+
var y = 0;
130+
switch(y){
131+
case 0:
132+
let x = 3;
133+
console.log(x);
134+
}
135+
console.log(x);
136+
})()
137+
|};
138+
[%expect
139+
{|
140+
$ cat "test.min.js"
141+
1: (function(){
142+
2: let v1 = 2;
143+
3: var v2 = 0;
144+
4: switch(v2){case 0: let v1 = 3; console.log(v1);
145+
5: }
146+
6: console.log(v1);
147+
7: }
148+
8: ());
149+
3
150+
2 |}]
151+
152+
let%expect_test "let and var inside class static block" =
153+
test
154+
{|
155+
(function () {
156+
let x = 2
157+
var y = 0;
158+
class z {
159+
static z = 2;
160+
static {
161+
let x = 3;
162+
var y = x;
163+
this.z = y
164+
}
165+
getZ(){ return this.z }
166+
}
167+
var t = new z;
168+
console.log(y, x, z.z);
169+
})()
170+
|};
171+
[%expect
172+
{|
173+
$ cat "test.min.js"
174+
1: (function(){
175+
2: let v2 = 2;
176+
3: var v3 = 0;
177+
4: class
178+
5: v4{static
179+
6: z
180+
7: =
181+
8: 2
182+
9: static{let v2 = 3; var v3 = v2; this.z = v3;}
183+
10: getZ(){return this.z;}
184+
11: }
185+
12: var v1 = new v4;
186+
13: console.log(v3, v2, v4.z);
187+
14: }
188+
15: ());
189+
0 2 3 |}]
190+
191+
let%expect_test "let inside block" =
192+
test
193+
{|
194+
(function () {
195+
let x = 2
196+
var y = 0;
197+
{
198+
let x = 3;
199+
var y = 4;
200+
}
201+
console.log(y, x);
202+
})()
203+
|};
204+
[%expect
205+
{|
206+
$ cat "test.min.js"
207+
1: (function(){
208+
2: let v1 = 2;
209+
3: var v2 = 0;
210+
4: {let v3 = 3; var v2 = 4;}
211+
5: console.log(v2, v1);
212+
6: }
213+
7: ());
214+
4 2 |}]

0 commit comments

Comments
 (0)