diff --git a/CHANGELOG.md b/CHANGELOG.md index 2985a80e16..cbead7007c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ #### :bug: Bug fix - Fix and clean up boolean and/or optimizations. https://github.com/rescript-lang/rescript-compiler/pull/7134 https://github.com/rescript-lang/rescript-compiler/pull/7151 +- Fix identifiers with name `arguments` and `eval` to be mangled. https://github.com/rescript-lang/rescript/pull/7163 #### :nail_care: Polish diff --git a/compiler/ext/js_reserved_map.ml b/compiler/ext/js_reserved_map.ml index 6c03752586..6daaff9760 100644 --- a/compiler/ext/js_reserved_map.ml +++ b/compiler/ext/js_reserved_map.ml @@ -80,12 +80,15 @@ let js_keywords = STbl.of_array [| "void"; "while"; "with"; + (* The following are also reserved in strict context, including ESM *) "let"; "static"; "yield"; + (* `await` is reserved in async context, including ESM *) "await"; + (* Future reserved words *) "enum"; "implements"; @@ -94,6 +97,18 @@ let js_keywords = STbl.of_array [| "private"; "protected"; "public"; + + (* Special identifiers + + `arguments` and `eval` is not real *keywords* + + However, they cannot be as identifiers in the strict mode, + and the compiler output is always in strict mode. + + See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers_with_special_meanings + *) + "arguments"; + "eval"; |] let is_js_keyword s = STbl.mem js_keywords s @@ -104,7 +119,7 @@ let is_js_keyword s = STbl.mem js_keywords s See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers_with_special_meanings - However, these names are actually used with no problems today. Preventing this can be annoying. + However, these names are actually used with no problems today. (Except `arguments` and `eval`) *) let js_special_words = STbl.of_array [| "arguments"; diff --git a/tests/tests/src/global_mangles.res b/tests/tests/src/global_mangles.res deleted file mode 100644 index d20d20dac2..0000000000 --- a/tests/tests/src/global_mangles.res +++ /dev/null @@ -1,3 +0,0 @@ -/* Reserved in CommonJS */ - -let (__dirname, __filename, exports, require) = (1, 2, 3, 4) diff --git a/tests/tests/src/global_mangles.mjs b/tests/tests/src/ident_mangles.mjs similarity index 64% rename from tests/tests/src/global_mangles.mjs rename to tests/tests/src/ident_mangles.mjs index e507b65f58..1a96fba6ac 100644 --- a/tests/tests/src/global_mangles.mjs +++ b/tests/tests/src/ident_mangles.mjs @@ -1,6 +1,14 @@ // Generated by ReScript, PLEASE EDIT WITH CARE +function fn1($$arguments) { + console.log($$arguments); +} + +function fn2($$eval) { + console.log($$eval); +} + let $$__dirname = 1; let $$__filename = 2; @@ -14,5 +22,7 @@ export { $$__filename, $$exports, $$require, + fn1, + fn2, } /* No side effect */ diff --git a/tests/tests/src/ident_mangles.res b/tests/tests/src/ident_mangles.res new file mode 100644 index 0000000000..97b7f26644 --- /dev/null +++ b/tests/tests/src/ident_mangles.res @@ -0,0 +1,10 @@ +/* Reserved in CommonJS */ +let (__dirname, __filename, exports, require) = (1, 2, 3, 4) + +let fn1 = arguments => { + Console.log(arguments) +} + +let fn2 = eval => { + Console.log(eval) +} diff --git a/tests/tests/src/key_word_property_plus_test.mjs b/tests/tests/src/key_word_property_plus_test.mjs index 1a7bc49b29..285af5f0d1 100644 --- a/tests/tests/src/key_word_property_plus_test.mjs +++ b/tests/tests/src/key_word_property_plus_test.mjs @@ -1,7 +1,7 @@ // Generated by ReScript, PLEASE EDIT WITH CARE import * as Mt from "./mt.mjs"; -import * as Global_mangles from "./global_mangles.mjs"; +import * as Ident_mangles from "./ident_mangles.mjs"; let suites = { contents: /* [] */0 @@ -31,7 +31,7 @@ eq("File \"key_word_property_plus_test.res\", line 13, characters 2-9", [ 2, 3, 4 -].reduce((prim0, prim1) => prim0 + prim1 | 0, 0), ((Global_mangles.$$__dirname + Global_mangles.$$__filename | 0) + Global_mangles.$$exports | 0) + Global_mangles.$$require | 0); +].reduce((prim0, prim1) => prim0 + prim1 | 0, 0), ((Ident_mangles.$$__dirname + Ident_mangles.$$__filename | 0) + Ident_mangles.$$exports | 0) + Ident_mangles.$$require | 0); Mt.from_pair_suites("Key_word_property_plus_test", suites.contents); diff --git a/tests/tests/src/key_word_property_plus_test.res b/tests/tests/src/key_word_property_plus_test.res index 9dc07bac19..73f53fd6e3 100644 --- a/tests/tests/src/key_word_property_plus_test.res +++ b/tests/tests/src/key_word_property_plus_test.res @@ -13,7 +13,7 @@ let () = eq( __LOC__, Js.Array2.reduce([1, 2, 3, 4], \"+", 0), { - open Global_mangles + open Ident_mangles __dirname + __filename + exports + require }, )