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

#2806 bugfix: Added checks for JS keywords in signatures. #2855

Merged
merged 2 commits into from
Jun 17, 2022

Conversation

petrstudynka
Copy link
Contributor

Added checks in macro crate for those cases downstream crate uses idents, which would result in invalid JS code generation.

"finally",
"function",
"import",
"instaceof",
Copy link

Choose a reason for hiding this comment

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

typo: instanceof


thread_local!(static ATTRS: AttributeParseState = Default::default());

/// Javascript keywords which are not keywords in Rust.
const JS_KEYWORDS: [&str; 19] = [
Copy link

Choose a reason for hiding this comment

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

null is missing

@y21
Copy link

y21 commented Apr 7, 2022

Not sure if it's just a visual bug on my side or if it actually submitted the same review twice. No idea how that happened. Sorry for the spam if it did.

@petrstudynka
Copy link
Contributor Author

@y21 Thanks for the review. Submitted fix.

@alexcrichton
Copy link
Contributor

Personally I think this would be best solved in the JS code generation rather than by forbidding this in Rust. Would it be possible to move the fix there instead?

@petrstudynka
Copy link
Contributor Author

Sure, I will change that.

@petrstudynka
Copy link
Contributor Author

I found out those keywords do not conflict when there are in defined as method name. So thats the reason for the if-else in the last commit. Otherwise those idents are mangled with underscore.

@alexcrichton
Copy link
Contributor

Can some tests be added for this to ensure that it works?

@alexcrichton
Copy link
Contributor

Ok looks great, thanks!

_ => "",
};
let name = if prefix.is_empty() && opts.method().is_none() && is_js_keyword(js_name) {
format!("_{}", js_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not silently replace the name of exported and imported identifiers where they make a semantic difference. Accessing them by bracket instead of dot syntax sounds like a reasonable workaround if the user actually does use a js keyword. I.e. wasm["class"] instead of silently changing to wasm._class against explicit annotation in

https://github.com/rustwasm/wasm-bindgen/pull/2855/files#diff-f7d340b325e0e9f30bd18d30036698d1edfece6974682dd3480457e4383806d3R17

Copy link
Collaborator

Choose a reason for hiding this comment

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

That sounds like a reasonable solution that should be easy to implement, would you like to make a PR? Happy to review it.

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.

None yet

5 participants