Skip to content

Commit 92477bd

Browse files
authored
Fix arguments and eval mangling (#7163)
* Fix `arguments` and `eval` mangling Fixed #7162 * add changelog
1 parent ee8092a commit 92477bd

7 files changed

+40
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#### :bug: Bug fix
2121

2222
- 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
23+
- Fix identifiers with name `arguments` and `eval` to be mangled. https://github.com/rescript-lang/rescript/pull/7163
2324

2425
#### :nail_care: Polish
2526

compiler/ext/js_reserved_map.ml

+16-1
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,15 @@ let js_keywords = STbl.of_array [|
8080
"void";
8181
"while";
8282
"with";
83+
8384
(* The following are also reserved in strict context, including ESM *)
8485
"let";
8586
"static";
8687
"yield";
88+
8789
(* `await` is reserved in async context, including ESM *)
8890
"await";
91+
8992
(* Future reserved words *)
9093
"enum";
9194
"implements";
@@ -94,6 +97,18 @@ let js_keywords = STbl.of_array [|
9497
"private";
9598
"protected";
9699
"public";
100+
101+
(* Special identifiers
102+
103+
`arguments` and `eval` is not real *keywords*
104+
105+
However, they cannot be as identifiers in the strict mode,
106+
and the compiler output is always in strict mode.
107+
108+
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers_with_special_meanings
109+
*)
110+
"arguments";
111+
"eval";
97112
|]
98113

99114
let is_js_keyword s = STbl.mem js_keywords s
@@ -104,7 +119,7 @@ let is_js_keyword s = STbl.mem js_keywords s
104119

105120
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers_with_special_meanings
106121

107-
However, these names are actually used with no problems today. Preventing this can be annoying.
122+
However, these names are actually used with no problems today. (Except `arguments` and `eval`)
108123
*)
109124
let js_special_words = STbl.of_array [|
110125
"arguments";

tests/tests/src/global_mangles.res

-3
This file was deleted.

tests/tests/src/global_mangles.mjs tests/tests/src/ident_mangles.mjs

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

33

4+
function fn1($$arguments) {
5+
console.log($$arguments);
6+
}
7+
8+
function fn2($$eval) {
9+
console.log($$eval);
10+
}
11+
412
let $$__dirname = 1;
513

614
let $$__filename = 2;
@@ -14,5 +22,7 @@ export {
1422
$$__filename,
1523
$$exports,
1624
$$require,
25+
fn1,
26+
fn2,
1727
}
1828
/* No side effect */

tests/tests/src/ident_mangles.res

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Reserved in CommonJS */
2+
let (__dirname, __filename, exports, require) = (1, 2, 3, 4)
3+
4+
let fn1 = arguments => {
5+
Console.log(arguments)
6+
}
7+
8+
let fn2 = eval => {
9+
Console.log(eval)
10+
}

tests/tests/src/key_word_property_plus_test.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

33
import * as Mt from "./mt.mjs";
4-
import * as Global_mangles from "./global_mangles.mjs";
4+
import * as Ident_mangles from "./ident_mangles.mjs";
55

66
let suites = {
77
contents: /* [] */0
@@ -31,7 +31,7 @@ eq("File \"key_word_property_plus_test.res\", line 13, characters 2-9", [
3131
2,
3232
3,
3333
4
34-
].reduce((prim0, prim1) => prim0 + prim1 | 0, 0), ((Global_mangles.$$__dirname + Global_mangles.$$__filename | 0) + Global_mangles.$$exports | 0) + Global_mangles.$$require | 0);
34+
].reduce((prim0, prim1) => prim0 + prim1 | 0, 0), ((Ident_mangles.$$__dirname + Ident_mangles.$$__filename | 0) + Ident_mangles.$$exports | 0) + Ident_mangles.$$require | 0);
3535

3636
Mt.from_pair_suites("Key_word_property_plus_test", suites.contents);
3737

tests/tests/src/key_word_property_plus_test.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let () = eq(
1313
__LOC__,
1414
Js.Array2.reduce([1, 2, 3, 4], \"+", 0),
1515
{
16-
open Global_mangles
16+
open Ident_mangles
1717
__dirname + __filename + exports + require
1818
},
1919
)

0 commit comments

Comments
 (0)