-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[explicit-member-accessibility] should not be part of recommended #201
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
Comments
cc @typescript-eslint/core-team |
I personally like them, because I'm a sucker for being explicit. Also coming from someone who has used C#/Java, I like being explicit because without it your code falls into the "default member accessibility" world which has a set of rules depending on where the caller is in relation to the callee. I hate the proposed JS-standard private syntax. The reason it was added originally was because the majority (if not all) of the community rulesets we looked at had it turned on. None of the ~5-10 community members that looked at the lists in the post objected to that rule, so it stuck. |
Are you describing behaviour in those other languages here? If so, I don’t think it should be a factor in this. Recommended shouldn’t be about making developers from other languages feel at home IMO |
Yes, this is how Java/C# work - there are tables you can look up to figure out what "default" actually means based on call context. Following from that, I like this rule because it forces people to be consistent in a codebase. Whilst This is especially good when developing code for others to consume, as defaulting everything to public means that it's a breaking change to bring it private. IMO enforcing a consistent accessibility modifier is a best practice, and should be recommended. |
We've been debating internally at my workplace if we should be requiring or forbidding the word I'm not convinced that it should be recommended (mostly as it's somewhat controversial which way it should be set, although I don't really mind, it's really easy for me to configure it and turning it on makes it obvious to me that it exists) but I think this rule definitely has a lot of value. |
This is just an anecdote, but personally I've only ever written it in constructor properties that are both public and mutable (so I can't use |
IMO we shouldn’t ban valid JS unless it’s either dangerous in TS or TS has a better alternative. |
My interpretation of @bradzacher's position is that the desired outcome is developers are more thoughtful about how they expose implementation details of classes. This is an admirable goal, I do not disagree with the motivation. However, the execution of that - forcing all methods to have access modifiers - is a very indirect way to achieve that aim. In my experience of working on the ESLint Team for the last 3 years, lint rules which try to enforce something to indirectly achieve a soft (hard to measure) outcome are what lead to developers declaring their hatred of linters and lead to a lot of frustration for everyone involved. If they write this particular syntax, their thought process will change/improve is a difficult hypothesis to validate. It is impossible to say how writing It also does not fit with common idioms and examples in the ecosystem, for example in Angular multiple lifecycle methods may be used on a Component's class. You won't see:
But that would be required by this rule. Common idioms can, of course, be based on faulty assumptions etc, but they should not be discounted when evaluating the impact of a deviation imposed by a linting rule. Let's also not forget that until private properties land in JavaScript, the notion of private is a false friend and should not be overvalued. Your properties are there at runtime, they are exposed. This is JavaScript. I see the question of whether or not something should be in recommended as: "Would the ecosystem be clearly better off if everyone wrote their code this way?" This is a very high bar, but it is definitely worth living up to wherever possible. With its clear cost and unclear benefit, forcing developers to deviate from JavaScript and write |
That's it :) I'm happy to remove it - I'm not overly opinionated for a recommended config (I've always been one to manually define my own rulesets). I tried to base it off of a combination of what the TS team does in their docs + what other ts eslint configs had done. Technically would be a breaking change though. |
Why would it be a breaking change? The only change would be a decrease in the number of reported issues when using the recommended config. If people want those reports back, they can simply enable the rule. |
The recommended config is rather opaque from an end-user's perspective. |
what do you think about making 3 configs:
with that we are giving users a little more granularity and easier way to set up environment |
Also note that actual JS syntax for |
@phaux - not necessarily.. it depends on how typescript handles the transpilation. They could very well transpile |
Unfortunately not ... microsoft/TypeScript#9950 (comment) |
That's an interesting read, thank you. Which makes me think that there is definite use cases for continuing to use |
The difference is that |
But what I'm wondering is - why you ever not use |
No, but it would be a breaking change to transpile |
…eslint#219) Fixes typescript-eslint#201 Fixes typescript-eslint#96 Fixes eslint/typescript-eslint-parser#577 The [base eslint implementation](https://github.com/eslint/eslint/blob/master/lib/rules/indent.js) purposely ignores nodes it doesn't know about (i.e. our TS nodes). Because of how the base rule is written, we have to override the implementation entirely.
Uh oh!
There was an error while loading. Please reload this page.
I have just noticed that currently we include
in
recommended.json
fromeslint-plugin
.I propose that we remove it.
Rationale
TypeScript has always positioned itself as an extension of JavaScript. It is no secret that I think it does a bloody good job at it :)
With it being an extension of JavaScript, there is an expectation that the majority of what you write is JavaScript code, and any syntactic additions provided by TypeScript are there to give you functionality you otherwise do not have.
I strongly believe this does not apply to the
public
access modifier.Classes have long been supported in JavaScript, and developers should understand how they work at runtime, regardless of if they use TypeScript or not. As we know, class properties are public in JavaScript.
The
private
andprotected
access modifiers do give you functionality you (currently**) otherwise do not have, so adding them to your otherwise valid JavaScript classes is a great capability.The
public
access modifier is not only unarguably functionally redundant based on my previous assertions about developers needing to understand JavaScript classes, it is arguably visual clutter when reading the code, and makes it harder to read.Readability is always subjective, I admit that, but I posit it is harder because having two words (
public
andprivate
) both beginning withp
, both of roughly equal length in front of every single class property makes them less likely to be explicitly distinguishable from one another when scanning code.An interesting piece of context to this - I often see one of the main reasons people use to justify not using TypeScript is that it is "so different to JavaScript" and "looks like Java". This is absolutely not the case when you approach it in the way I suggest above, and leverage it as an extension to JavaScript. Having a high-profile project like this one recommend a rule like this to developers does not help with that image IMO.
Finally, the rationale for the rule given in the description in the
README
of the rule is very unclear.It is conflating the decision of whether or not things should be
private
to a class, with whether or not public things should use apublic
access modifier.These are two completely different things:
Making everything public != Not using the
public
keywordI do understand why some people might want to use this rule, and I am not advocating that we should not support it.
I am however, strongly suggesting that it is not appropriate to include it in
recommended
.If people wish to provide counter-arguments to what I have said above please do, but please remember keep the focus on the justification as to why it should be in recommended.
**I say currently because of course private properties are coming to JavaScript very soon, and they have completely different syntax to what we are used to with TypeScript today. TypeScript will adapt to this in one way or another, but by leveraging
public
it means you are doubling down on custom TypeScript syntax in the interim, and so that is also a factor IMOThe text was updated successfully, but these errors were encountered: