-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add rename_all = "camelCase"
option to automatically rename methods and functions
#4215
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mostly don't like the name
rename_all
, but it's not clear that it:
- Only affects methods/functions and fields,
- Don't affect explicitly given
js_name
s.
I think your current implementation sounds good to me: affects methods, static methods and fields and explicit js_name
s aren't overwritten.
camelify_by_default
sounds a bit too funny to me. How about default_rename_camel_case
? Kinda terrible as well, let me know if you have any better ideas.
This comment suggests Also, maybe we could use something like What do you think? On a different note: static properties. Static properties and readonly variables sometimes use SCREAMING_SNAKE_CASE similar Rust In general, the renaming assumes snake_case, but doesn't verify it. Should we only rename names that we can verify to be snake_case? |
Hm, good point. In which case wouldn't we be looking at something like |
Not really. My point was the JS devs sometimes use camelCase and sometimes SCREAMING_SNAKE_CASE for constants/static variables/getters, so we probably shouldn't make everything camelCase. Thinking about this, JS devs generally don't use lower_snake_case for anything. So how about we make the behavior of What do you think? 🐍💼➡️🐪💼 |
Is there no standard? If the goal is to do this automatically in the next breaking change, what are we going to convert it to then?
Basically boils down to the question above. We should figure out what we want to do automatically in the future and implement this here. But yes, I agree we should "fix" all names, not just methods. But I think we should do it regardless of what the user names their items, explicit names should be set with the |
No. In JS, the best we have are common naming conventions. JS itself uses SCREAMING_SNAKE_CASE for constants (e.g. This means that names that are already in PascalCase, camelCase, or SCREAMING_SNAKE_CASE, are most likely correct. So we only really have to rename snake_case names, because snake_case is most likely "wrong".
I don't think that what you're saying is wrong, but I think it might not be practical - for us and our users. If you want to convert more than just snake_case names, then you have to be able to detect and convert different naming conversions. That's not easy, because there are many many edge cases (e.g. a constant called For users, it's also less practical. Renaming is essentially guessing the correct name for the user. If we guess wrong, the user has to fix it with |
Hm, that's bad.
I don't think I'm following here. It seems much more straightforward to me that we simply rename everything regardless, instead of making any exceptions in specific cases that users have to understand in the first place. No guessing required. It seems more predictable to me. I'm also not concerned about the practicality, as we should generally discourage users to name anything in Rust not following the Rust naming conventions. E.g. if users want their static properties to be PascalCase, they should still name them in Rust in I'm also unsure about edge-cases, I feel like anything should be possible to correctly convert to camelCase, whats the problem with Please let me know if and why you don't agree with my point of view here! |
Yep. But if we take SCREAMING_SNAKE_CASE for static constants, naming isn't too different from Rust:
My point was that to convert everything to one naming convention, you first need to detect the casing of the identifier. This is why I said that But that's not really a problem IF we assume Rust naming conventions. I just didn't want to make that assumption, because there are a lot of WBG users that use camelCase for methods in Rust right now due to the lack of automatic renaming. Even if we discourage not following Rust naming conventions, I think we should still support it to some degree for compatibility with existing camelCase identifiers. That said, it feels like we're not 100% talking about the same thing here. I'm not quite sure what you mean by "rename everything regardless". As you can see in the above table, we only need to rename snake_case identifiers, so what's everything here? |
Sure, lets just go with that. However, we might want to consider not converting anything that matches naming conventions. E.g. Struct and enum types should be pascal case in both languages, so if the user doesn't use pascal case in Rust, we can take that as being set explicit. So no conversion necessary. The same would apply to static constants. That might just resolve your concern of practicality here.
We only need to detect the casing and spacing, not necessarily which convention its following. I think its pretty clear how
Compatibility with current code is not a concern, right? Users still need to explicitly set the attribute.
I was explicitly responding to this:
So regardless of what naming convention a user has used, we should convert every property and method to the naming convention according to the table you set above. Though keep in mind my suggestion at the start of the post, where we could just not do any conversion for static constants, as they should be SCREAMING_SNAKE_CASE for both languages. In which case if a user is using PascalCase for their static constants, we won't convert it to SCREAMING_SNAKE_CASE. They should get a Rust warning though. |
I agree. This is also what I wanted to do. I also agree that users not following Rust naming conventions is a strong signal that they are doing it on purpose. So to summarize, the renaming strategy for identifiers will be:
This is what you mean, right? If so, then I 100% agree with it. On a related note: should we rename function parameters? They fulfill the criteria, but parameter names aren't really observable in JS (kinda, you can observe them with hacks, but you must not rely on them). Renaming them might also cause problems with user-defined JSDoc We may also want to change the attribute from
Clippy already emits a warning for not following Rust naming conventions. So we don't have to do anything, I think. |
Yes, I see now that there was a misunderstanding on my part before. I was somehow assuming that we need to figure out which naming convention e.g. enum types are to check if they follow the Rust convention. But because there is nothing for us to do if they don't, we don't actually need to that.
Yes. Lets move the user-defined JSDoc conversation to real-time because this needs a bit of a longer back and forth.
Yes ... finally a good name.
Yeah, we just need to check via UI tests, but I know this is hard to get right, so its not a requirement for a PR. |
What's left to do:
|
fixes #1818
This adds support for
rename_all = "camelCase"
to camelify names inside a block.Under the hood, I added support for a more general renaming system mimicking serde's renaming system. If we want to camelify methods by default in the next breaking change, we just have to change the default values for those renaming rules.
Examples:
Open questions
I mostly don't like the name
rename_all
, because it's not clear that it:js_name
s.