Skip to content
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

Compiler: fix free variable for classes #1524

Merged
merged 2 commits into from
Nov 11, 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 @@ -16,6 +16,7 @@
* 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)
* Compiler: fix free variables for classes

# 5.4.0 (2023-07-06) - Lille

Expand Down
22 changes: 21 additions & 1 deletion compiler/lib/js_traverse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,15 @@ class free =
tbody#record_block (Params params);
m#merge_info tbody;
EFun (ident, (k, params, body, nid))
| EClass (ident_o, cl_decl) ->
let ident_o =
Option.map
~f:(fun id ->
m#def_var id;
m#ident id)
ident_o
in
EClass (ident_o, m#class_decl cl_decl)
| _ -> super#expression x

method record_block _ = ()
Expand Down Expand Up @@ -873,6 +882,9 @@ class free =
m#def_var id;
m#merge_info tbody;
Function_declaration (id, (k, params, body, nid))
| Class_declaration (id, cl_decl) ->
m#def_var id;
Class_declaration (id, m#class_decl cl_decl)
| Block b -> Block (m#block b)
| Try_statement (b, w, f) ->
let same_level = level in
Expand Down Expand Up @@ -946,7 +958,12 @@ class rename_variable =

inherit iter as super

method expression _ = ()
method expression e =
match e with
| EClass (ido, _) ->
Option.iter ido ~f:decl_var;
super#expression e
| _ -> super#expression e

method fun_decl _ = ()

Expand All @@ -956,6 +973,9 @@ class rename_variable =
decl_var id;
self#fun_decl fd
| Lexical_block, Function_declaration (_, fd) -> self#fun_decl fd
| (Fun_block _ | Lexical_block), Class_declaration (id, _) ->
decl_var id;
super#statement x
| (Fun_block _ | Lexical_block), _ -> super#statement x

method variable_declaration k l =
Expand Down
12 changes: 6 additions & 6 deletions compiler/tests-compiler/minify.ml
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ let%expect_test _ =
let js_prog =
{|
(function () {
class f {
f() {
class longname {
longname() {
const y = 2;
return v
}
Expand All @@ -327,8 +327,8 @@ let%expect_test _ =
$ cat "test.js"
1:
2: (function () {
3: class f {
4: f() {
3: class longname {
4: longname() {
5: const y = 2;
6: return v
7: }
Expand All @@ -338,9 +338,9 @@ let%expect_test _ =
11:
$ cat "test.min.js"
1: (function(){class
2: f{f(){const
2: a{longname(){const
3: a=2;return v}}const
4: a=y}()); |}])
4: b=y}()); |}])

let%expect_test _ =
with_temp_dir ~f:(fun () ->
Expand Down