|
| 1 | +(* Copyright (C) 2025 - Authors of ReScript |
| 2 | + * |
| 3 | + * This program is free software: you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU Lesser General Public License as published by |
| 5 | + * the Free Software Foundation, either version 3 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * In addition to the permissions granted to you by the LGPL, you may combine |
| 9 | + * or link a "work that uses the Library" with a publicly distributed version |
| 10 | + * of this file to produce a combined library or application, then distribute |
| 11 | + * that combined work under the terms of your choosing, with no requirement |
| 12 | + * to comply with the obligations normally placed on you by section 4 of the |
| 13 | + * LGPL version 3 (or the corresponding section of a later version of the LGPL |
| 14 | + * should you choose to use a later version). |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Lesser General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Lesser General Public License |
| 22 | + * along with this program; if not, write to the Free Software |
| 23 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) |
| 24 | + |
| 25 | +module E = Js_exp_make |
| 26 | + |
| 27 | +let global_this = E.js_global "globalThis" |
| 28 | + |
| 29 | +(* Only rewrite for lexical bindings (let-style). *) |
| 30 | +let is_lexical_binding_kind (property : J.property) = |
| 31 | + match property with |
| 32 | + | Strict | StrictOpt | Alias -> true |
| 33 | + | Variable -> false |
| 34 | + |
| 35 | +(* Skip JS globals/keywords and compiler-reserved JS ids. *) |
| 36 | +let should_rewrite_binding (ident : Ident.t) = |
| 37 | + (not (Ext_ident.is_js ident)) |
| 38 | + && (not (Js_reserved_map.is_js_keyword ident.name)) |
| 39 | + && not (Js_reserved_map.is_js_global ident.name) |
| 40 | + |
| 41 | +let rewrite_shadowed_global_in_expr ~(name : string) (expr : J.expression) : |
| 42 | + J.expression = |
| 43 | + let super = Js_record_map.super in |
| 44 | + let self = |
| 45 | + { |
| 46 | + super with |
| 47 | + expression = |
| 48 | + (fun self expr -> |
| 49 | + match expr.expression_desc with |
| 50 | + | Var (Id id) when Ext_ident.is_js id && String.equal id.name name -> |
| 51 | + E.dot global_this name |
| 52 | + | _ -> super.expression self expr); |
| 53 | + } |
| 54 | + in |
| 55 | + self.expression self expr |
| 56 | + |
| 57 | +let program (js : J.program) : J.program = |
| 58 | + let super = Js_record_map.super in |
| 59 | + let self = |
| 60 | + { |
| 61 | + super with |
| 62 | + variable_declaration = |
| 63 | + (fun self (vd : J.variable_declaration) -> |
| 64 | + match vd with |
| 65 | + | {ident; value = Some expr; property} |
| 66 | + when is_lexical_binding_kind property |
| 67 | + && should_rewrite_binding ident -> |
| 68 | + let expr = rewrite_shadowed_global_in_expr ~name:ident.name expr in |
| 69 | + {vd with value = Some expr} |
| 70 | + | _ -> super.variable_declaration self vd); |
| 71 | + } |
| 72 | + in |
| 73 | + self.program self js |
0 commit comments