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 handling of JS keyword for imported functions, types and statics, and when causing invalid code gen #4329

Merged
merged 19 commits into from
Dec 8, 2024

Conversation

RunDevelopment
Copy link
Contributor

Fixes #4317
Fixes #4328

As I said in #4317, keyword handling was kind of a mess. Keywords are now handled correctly for both imports and exports AFAICT.

Changes:

  1. Stricter parsing for js_namespace. Now, the first identifier cannot be a JS keyword and empty lists are no longer allowed. This is not a breaking change, because keyword identifiers resulted in invalid JS code gen, and empty lists crashed the CLI.
  2. Keywords are now generally allowed for imported functions, types, and statics. However, they will cause an error if the resulting JS code gen would be invalid.
  3. Keywords are now always correctly escaped when used as function parameters.
  4. Exported classes and enums named after keywords now result in an error instead of invalid code gen. Since all JS keywords are lower-case, I don't export anyone to have used keywords for them anyway.
  5. I fixed the keyword list. Whoever decided that we don't need to check Rust keywords forgot that raw identifiers and js_name exist. The list now includes all JS keywords.

About the keyword list update: I also add keywords that are only keywords in strict mode. JavaScript has 2 execution modes: strict mode and sloppy mode. Sloppy mode is the legacy mode and shouldn't be used anymore. The key takeaway here is that we should only generate code that works in strict mode, so we must not allow identifiers that are keywords in strict mode. This is important, because all JS files that use ESM (import/export) implicitly use strict mode. I don't want WBG to be in the situation where it only generates valid JS for some CLI targets.

Lastly, await. await is a keyword ONLY is async functions in JS. So it can generally be used as an identifier, but this can cause problems. If a class is named await, then this class cannot be used inside async functions. It's a weird edge case, and I'm not sure whether we should consider await a reserved keyword too. Right now, I just allowed it.

Copy link
Collaborator

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

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

Good stuff!

Missing a changelog entry.

crates/cli/tests/reference/import.rs Show resolved Hide resolved
crates/cli/tests/reference/keyword.rs Outdated Show resolved Hide resolved
crates/macro-support/src/parser.rs Outdated Show resolved Hide resolved
crates/cli/tests/reference/keyword.rs Outdated Show resolved Hide resolved
crates/macro-support/src/parser.rs Outdated Show resolved Hide resolved
crates/macro-support/src/parser.rs Outdated Show resolved Hide resolved
crates/macro-support/src/parser.rs Outdated Show resolved Hide resolved
crates/macro-support/src/parser.rs Outdated Show resolved Hide resolved
crates/macro-support/src/parser.rs Outdated Show resolved Hide resolved
crates/macro-support/src/parser.rs Outdated Show resolved Hide resolved
@daxpedda daxpedda added the waiting for author Waiting for author to respond label Dec 6, 2024
@RunDevelopment
Copy link
Contributor Author

Done. Thanks for the suggestions!

CHANGELOG.md Outdated Show resolved Hide resolved
crates/macro-support/src/parser.rs Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@daxpedda daxpedda 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 for all the work!

@daxpedda daxpedda merged commit 5f288bd into rustwasm:main Dec 8, 2024
55 checks passed
@alockey
Copy link

alockey commented Dec 12, 2024

Thanks for the prompt fix - much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for author Waiting for author to respond
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using js_namespace = [] causes a crash in the CLI Import "new" becomes "_new"
3 participants