|
24 | 24 |
|
25 | 25 | module E = Js_exp_make |
26 | 26 |
|
| 27 | +module StringSet = Set.Make (String) |
| 28 | + |
27 | 29 | let global_this = E.js_global "globalThis" |
28 | 30 |
|
29 | 31 | (* Only rewrite for lexical bindings (let-style). *) |
@@ -55,18 +57,42 @@ let rewrite_shadowed_global_in_expr ~(name : string) (expr : J.expression) : |
55 | 57 | self.expression self expr |
56 | 58 |
|
57 | 59 | let program (js : J.program) : J.program = |
| 60 | + let shadowed_globals = |
| 61 | + Ext_list.fold_left js.block StringSet.empty (fun acc (st : J.statement) -> |
| 62 | + match st.statement_desc with |
| 63 | + | Variable {ident; property} |
| 64 | + when is_lexical_binding_kind property && should_rewrite_binding ident |
| 65 | + -> |
| 66 | + StringSet.add ident.name acc |
| 67 | + | _ -> acc) |
| 68 | + in |
58 | 69 | let super = Js_record_map.super in |
59 | 70 | let self = |
60 | 71 | { |
61 | 72 | super with |
| 73 | + expression = |
| 74 | + (fun self expr -> |
| 75 | + match expr.expression_desc with |
| 76 | + | Static_index (obj, field, pos) -> |
| 77 | + let obj = self.expression self obj in |
| 78 | + let obj = |
| 79 | + match obj.expression_desc with |
| 80 | + | Var (Id id) |
| 81 | + when Ext_ident.is_js id |
| 82 | + && StringSet.mem id.name shadowed_globals -> |
| 83 | + E.dot global_this id.name |
| 84 | + | _ -> obj |
| 85 | + in |
| 86 | + {expr with expression_desc = Static_index (obj, field, pos)} |
| 87 | + | _ -> super.expression self expr); |
62 | 88 | variable_declaration = |
63 | 89 | (fun self (vd : J.variable_declaration) -> |
64 | 90 | match vd with |
65 | 91 | | {ident; value = Some expr; property} |
66 | 92 | when is_lexical_binding_kind property |
67 | 93 | && should_rewrite_binding ident -> |
68 | 94 | let expr = rewrite_shadowed_global_in_expr ~name:ident.name expr in |
69 | | - {vd with value = Some expr} |
| 95 | + super.variable_declaration self {vd with value = Some expr} |
70 | 96 | | _ -> super.variable_declaration self vd); |
71 | 97 | } |
72 | 98 | in |
|
0 commit comments