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

Fix arguments and eval mangling #7163

Merged
merged 2 commits into from
Nov 20, 2024
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

17 changes: 16 additions & 1 deletion compiler/ext/js_reserved_map.ml
Original file line number Diff line number Diff line change
@@ -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";
3 changes: 0 additions & 3 deletions tests/tests/src/global_mangles.res

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 */
10 changes: 10 additions & 0 deletions tests/tests/src/ident_mangles.res
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions tests/tests/src/key_word_property_plus_test.mjs
Original file line number Diff line number Diff line change
@@ -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);

2 changes: 1 addition & 1 deletion tests/tests/src/key_word_property_plus_test.res
Original file line number Diff line number Diff line change
@@ -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
},
)