Skip to content

Compiler: fix free variable wrt parameter's default value #1521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Runtime: fix Unix.lstat, Unix.LargeFile.lstat (#1519)
* Compiler: fix global flow analysis (#1494)
* Compiler: fix js parser/printer wrt async functions (#1515)
* Compiler: fix free variables pass wrt parameters' default value (#1521)

# 5.4.0 (2023-07-06) - Lille

Expand Down
3 changes: 3 additions & 0 deletions compiler/lib/js_traverse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ class free =
let ids = bound_idents_of_params params in
List.iter ids ~f:tbody#def_var;
let body = tbody#function_body body in
let params = tbody#formal_parameter_list params in
tbody#record_block (Params params);
m#merge_info tbody;
k, params, body, nid
Expand All @@ -827,6 +828,7 @@ class free =
let ids = bound_idents_of_params params in
List.iter ids ~f:tbody#def_var;
let body = tbody#function_body body in
let params = tbody#formal_parameter_list params in
let ident =
match ident with
| Some i ->
Expand Down Expand Up @@ -866,6 +868,7 @@ class free =
let ids = bound_idents_of_params params in
List.iter ids ~f:tbody#def_var;
let body = tbody#function_body body in
let params = tbody#formal_parameter_list params in
tbody#record_block (Params params);
m#def_var id;
m#merge_info tbody;
Expand Down
42 changes: 42 additions & 0 deletions compiler/tests-compiler/minify.ml
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,45 @@ function f () {
2: f(){const
3: a=2;return function(){var
4: a=a+2;return a}} |}])

let%expect_test _ =
with_temp_dir ~f:(fun () ->
let js_prog =
{|
function test() {
var x = {a:1,b:2}
function f (a, b = x.b) {
return (a + b);
}
console.log(f(1));
}
test()
|}
in
let js_file =
js_prog |> Filetype.js_text_of_string |> Filetype.write_js ~name:"test.js"
in
let js_min_file =
js_file |> jsoo_minify ~flags:[ "--enable"; "shortvar" ] ~pretty:false
in
print_file (Filetype.path_of_js_file js_file);
print_file (Filetype.path_of_js_file js_min_file);
[%expect
{|
$ cat "test.js"
1:
2: function test() {
3: var x = {a:1,b:2}
4: function f (a, b = x.b) {
5: return (a + b);
6: }
7: console.log(f(1));
8: }
9: test()
$ cat "test.min.js"
1: function
2: test(){var
3: c={a:1,b:2};function
4: a(a,b=c.b){return a+b}console.log(a(1))}test(); |}];
print_endline (run_javascript js_min_file);
[%expect {| 3 |}])