-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
New rule request: naming conventions of methods #7065
Comments
Could this be simply an extension of the |
@vitorbal yes I think so, provided that we choose to limit this to underscores. My current implementation is wider and allows defining a regex to test against. If that's deemed to be interesting we could have a |
I definitely think |
Concerning the possibility of extending this to arbitrary regexes, I think it wouldn't unreasonable for a team to want to enforce a convention like: class Foo() {
foo_bar() {}
} as well as const foo_bar = 42 which seems impossible with ESLint as of today. After all, many linters of other languages support matching a regex for enforcing naming conventions. The reason I'm bringing this up here, is that a "regex-checking" rule is introduced, it would overlap with |
@platinumazure totally missed it, let me check |
@platinumazure apparently That being said, I think it's fairly difficult to use as there's no way of limiting its scope to methods. Also, it already overlaps with So my proposal would be to extend Does it sound reasonable? |
@gabro To be honest, I would prefer extending Long term, it would be great to deprecate and remove |
I don't want to have to configure |
Hmm... I think I had mixed up I'm not really sure what the best approach is here, though. @gabro Could you flesh out your proposal a little bit? What would really help us out is:
Thanks in advance! |
I also don't want to have to configure |
Okay, we've gone off track. That's my fault, I apologize, but let's please get back on track. If we can get a full proposal for extending no-underscore-dangle to optionally support method names and class properties, then the team can evaluate. Sound good? 😄 |
Sounds good to me! I'll get back to this later today |
Straw-man proposal (outdated, see below)
|
Thanks, @gabro! I think Also, class properties are still stage 2 so I don't think we should/can support them yet. One last thing, could you add some examples for concise object method names as well? |
Yes, I agree in disambiguating. About the class properties matter, is this a general policy of ESLint? My perception is that class properties are quite standard in the React world for binding methods to the class, so this rule would be sort of incomplete without them. After all, babel-eslint supports them, and it seems harmless to include the option (defaulting to I updated my proposal above to include examples of concise object method names and renaming |
Small thing: the class definitions in your proposal have a typo: - class Foo() {
+ class Foo { |
woops, too many languages at the same time. That comes from scala :) Thanks, fixed |
Unfortunately, code with class properties won't even parse correctly with the default ESLint parser. Example: https://astexplorer.net/#/dBfzkR927u. This is only supported by babel-eslint for now, and we have a policy of only supporting features that have reached stage 4. I understand this can be frustrating, but a stage 2 proposal is still prone to change, and the committee has even mentioned that the syntax for class properties may change before it's finalized. This means potentially more work for us to keep the parser up-to-date with specs that are not even final yet. |
I see you point and understand the concern. I guess I can always roll my own plugin for supporting this while the feature reaches a more mature stage. Should I go ahead and remove it from the proposal? |
@gabro Yes, I think we can remove it. After that, from my part this looks good! Thanks for the quick replies. Now let's wait to see what the rest of the team thinks :) |
Straw-man proposal
|
It's unfortunate to have options where absence is not the same as |
Ok, since it seems we reached an agreement here, I've submitted a PR. |
We still need a champion and one more thumbs up from the team to mark this as accepted (assuming me and @platinumazure are the current +1's) |
@vitorbal hey there, any news? Still missing a thumb up? :) |
@gabro Unfortunately I can't really tell which post is supposed to be the "canonical proposal" anymore. Any chance you could edit the initial post with whatever we're currently agreed on? Then @vitorbal and I can 👍 it and hopefully get a third team member and a champion. Sorry for losing track of this. It's not much of an excuse, but with 200-ish issues and 40-ish PRs open and only about 5-10 active team members, this is bound to happen more often than we would like. Thanks for following up and we greatly appreciate your patience. |
@platinumazure thanks for your reply. I updated the original post with my final proposal! No need to apologize, I understand this is a big project and it's easy to lose track of older issues. Mine was a reminder, not a complaint :) Thanks for your work! |
@gabro thanks for the poke! I'm still 👍 on the updated proposal to add a new option to |
It's hard to tell where we stand on this - @not-an-aardvark @vitorbal @platinumazure do you three support this? If so, then we just need a champion. If not, it might unfortunately be time to close this as per our policy. |
I'm 👍-ing this under the assumption that the main goal is to augment |
@platinumazure I can assure that's my intent |
I'm still 👍 to the proposal of enhancing |
@eslint/eslint-team we need one more +1 to mark this as accepted. Here's the final proposal (under "Updated proposal"). Any takers? |
@vitorbal actually the final proposal is in the issue description, as per @platinumazure suggestion :) |
@gabro oops, thanks, fixed the link! |
…t#7065) When enforceInMethodNames is true, the rule checks for dangling underscores in method names too. This includes methods of classes and method properties of objects. The rule is false by default.
…t#7065) When enforceInMethodNames is true, the rule checks for dangling underscores in method names too. This includes methods of classes and method properties of objects. The rule is false by default.
…t#7065) When enforceInMethodNames is true, the rule checks for dangling underscores in method names too. This includes methods of classes and method properties of objects. The rule is false by default.
I came across this today and was sadden when I saw this turned into no-underscore-dangle. Would you mind changing the title @gabro? I really would like the original proposal for regex based method/function nameíng conventions. And I'm hesitant to open a new issue basically named the same. In my case I'm still rocking ES5, and there I'm using something like: function MyClass() {}
MyClass.prototype.method = function MyClass$method() {}
// All fine and dandy
MyClass.prototype._privateMethod = function MyClass$_privateMethod() {}
// ^ This is where the camelcase chokes I can open a new issue when this is renamed, but like to avoid confusing where possible. |
@eslint/eslint-team We need a champion for this proposal, otherwise it's time to close it. |
I will champion this, but we still need one more thumbs-up. Any takers? |
…t#7065) When enforceInMethodNames is true, the rule checks for dangling underscores in method names too. This includes methods of classes and method properties of objects. The rule is false by default.
…t#7065) When enforceInMethodNames is true, the rule checks for dangling underscores in method names too. This includes methods of classes and method properties of objects. The rule is false by default.
…t#7065) When enforceInMethodNames is true, the rule checks for dangling underscores in method names too. This includes methods of classes and method properties of objects. The rule is false by default.
…t#7065) When enforceInMethodNames is true, the rule checks for dangling underscores in method names too. This includes methods of classes and method properties of objects. The rule is false by default.
…t#7065) When enforceInMethodNames is true, the rule checks for dangling underscores in method names too. This includes methods of classes and method properties of objects. The rule is false by default.
I looked for a rule for enforcing naming conventions on methods of a class but I couldn't find any.
As an example, the AirBnb style guide for React forbids prefixing methods with an underscore, but there is no way in ESLint to enforce it.
The rule I'm proposing would be an equivalent of
no-underscore-dangle
but for methods.Valid examples
Invalid examples
The rule has two purposes: enforcing a common style and preventing errors. The rationale behind the error prevention is that prefixing with an underscore may give a false sense of privacy, which in turn can cause mistakes, e.g. breaking changes to a method which was thought to be private.
I have already a working implementation of a more generic version of this rule https://github.com/buildo/eslint-plugin-method-names, which allows specifying a regex to which method names should confirm.
I think at least the underscore-specific version should be in the core rules, to match the
no-underscore-dangle
rule.Open questions (if the proposed rule is valid):
should the rule scope be limited at the underscore case or should it be generic and allow a regex to be specified?
should the rule scope include class properties defining a function? (my plugin currently does).
E.g.
would be both invalid. This is a fairly common use case when using React components, in order to bind handlers.
Updated proposal, after the discussion below
no-underscore-dangle
will have one additional option:enforceInMethodNames
(default:false
)enforceInMethodNames
Examples of correct code for this rule with the
{ "enforceInMethodNames": true }
option:Examples of incorrect code for this rule with the
{ "enforceInMethodNames": true }
option:The rationale between the additional option and its default value is to make this change non-breaking.
The text was updated successfully, but these errors were encountered: