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 reserved words #6831

Merged
merged 5 commits into from
Jun 29, 2024
Merged

fix reserved words #6831

merged 5 commits into from
Jun 29, 2024

Conversation

cometkim
Copy link
Member

@cometkim cometkim commented Jun 26, 2024

The compiler prefixes conflicting ident names with $$ to prevent the output from containing a SyntaxError or unintentionally declarying conflicting names.

However, some of the managed keywords are outdated (keywords listed on the Future reserved words in older standards section), some are not actually keywords, and the list of Chrome global variables is far too extensive.

Changes

  • Clarify actual JavaScript's keywords and other reserved words
  • Includes only the JavaScript standard builtins. classnames from Chrome are no longer prefixed
  • Refactor the keywords scripts and manage a single regular OCaml module.
  • Use OCaml Hashtbl instead of binary search (keywords are small enough)

Future works

If a name is not an actual JS keyword, there should be no problem defining the identifier with the name inside the module scope, even if it is used in the globalThis scope. But the SyntaxError only occurs in the CommonJS system.

IMHO we need a way to allow using those names if users want. In a previous PR I proposed a syntax using exotic ident.

// "require" is a reserved word

let require = ""
// This will be `$$require` in the output

let \"require" = ""
// This will be `require` in the output

However, this cannot be implemented in the current parser. If necessary, the scanner would have to be modified to keep exotic tokens, which would significantly increase the complexity of printers.

@cometkim
Copy link
Member Author

But the SyntaxError only occurs in the CommonJS system.

I just modified the test and now they only throw a SyntaxError for names that are directly related to CommonJS module system.

  • module
  • exprots
  • require
  • __dirname
  • __filename

Others should be fine to allow.

@cometkim
Copy link
Member Author

cometkim commented Jun 28, 2024

I removed the codegen script at b1275cb since now we can manage keywords manually.

I ran the syntax benchmark and there were no noticeable perf changes by using OCaml Set instead of binary search.

@cometkim
Copy link
Member Author

I noticed that it isn't BST. Also, it is a list that can never be changed, it does not benefit from using an immutable tree. I replaced it with a Hashtbl to optimize lookups.

Copy link
Member

@cknitt cknitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks a lot!

@cknitt cknitt merged commit 4ef6668 into rescript-lang:master Jun 29, 2024
19 checks passed
@cknitt cknitt mentioned this pull request Jun 29, 2024
@cometkim cometkim deleted the unmangle-globals branch June 29, 2024 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants