typescript-eslint: [naming-convention] Missing distinction between "declaring" versus "using" a property
Regarding the property selector, the naming-convention docs say:
property- matches any object, class, or object type property. Does not match properties that have direct function expression or arrow function expression values.- Allowed
modifiers:private,protected,public,static,readonly,abstract.- Allowed
types:boolean,string,number,function,array.
In other words, there is no distinction between an interface property versus an object property. Consider this example:
interface IMyContract {
// We very much need ESLint to forbid this:
WrongCapitalization: string;
}
ancientLegacyThing.doSomething({
// But we have absolutely no control over this contract,
// so ESLint should leave it alone:
AlsoWrongCapitalization: 'hello'
});
Expected Result
The naming-convention rule should provide selectors that distinguish between declaring an “interface”, versus making an “object” that will fulfill an existing interface.
Actual Result
As far as I can tell, there is no distinction. In a code base with lots of legacy APIs, it would be completely impractical to add a lint suppression for every such function call.
As a result, we basically have to disable the naming-convention validation for any selector that could potentially be used to fulfill a legacy contract. This seems like a fairly severe limitation.
Additional Info
@typescript-eslint/naming-convention replaces 4 earlier rules which were recently deleted (which is how I got involved with this all the sudden):
@typescript-eslint/camelcase@typescript-eslint/class-name-casing@typescript-eslint/interface-name-prefix@typescript-eslint/member-naming
I am trying to understand why those earlier rules did not seem to exhibit this problem. Maybe they actually were not validating interface property names?
Versions
| package | version |
|---|---|
@typescript-eslint/eslint-plugin |
3.3.0 |
@typescript-eslint/parser |
3.3.0 |
TypeScript |
3.5.3 |
ESLint |
7.2.0 |
node |
12.17.0 |
npm |
6.14.4 |
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 29
- Comments: 17 (9 by maintainers)
I’d love to see
naming-conventionnot complain about any names that are declared elsewhere and are typechecked against some interface. There’s so many third-party libraries expecting weird parameter, often when interfacing with different ecosystems. I wish the plugin could just ignore names over which I have no control anyway.I admit this would be quite complicated. We can’t just check whether a property is part of the type that the object has, as the type might be inferred from the very object literal itself (e.g. in
as constdeclarations). It would actually need to find the declaration of that type, and complain only if there exists none.While I agree that false negatives reduce the trust in a rule, I think false positives are way worse than that. It’s better to have a rule that covers at least some of what you want to do than have to deal with a rule that keeps complaining about unimportant issues, or issues that just aren’t in your control.
@lucasbasquerotto I would validate “variable declaration without a type annotation” because by doing that you’re de facto defining an inline type. But even without this adjustment I still think your suggestion is great. Overall, problems should only be reported where the type is defined, not where it’s used. That just creates a completely unnecessary snowball effect of lint errors coming from one type definition. For a lint rule, I think having false negatives is better than having false positives.
Is
const { abc } = defmatching on both property and variable declarations still considered a bug? I can send a PR if so.I think then the best course of action would be the following:
Define some new selectors:
propertySignature, which only matches an object type property signatures.property, this should not match property signatures with a function type.propertyDeclaration, which only matches object literal’s property declarations.propertyselector, minus matching property signatures.methodSignature, which only matches an object type’s method signatures.property, this should match property signatures with a function type.methodDeclaration, which only matches object literal’s method declarations.methodselector, minus matching method signatures.Define some new group selectors for backwards compatibility and easy of consumption:
property, which groupspropertySignatureandpropertyDeclaration.method, which groupsmethodSignatureandmethodDeclaration.Also update the
memberLikegroup selector to include all of the above selectors.This would give you the flexibility to define a naming convention that ignores object literal properties altogether.
One caveat to note here is that without a convention defined for
*Declaration, there will be no way to be able to save you from index types:In my opinion, I expect this linting rule to only validate cases which are explicitly defined by the user, basically:
It should validate only in the above cases (I would be ok with a flag to validate only in the cases above only if the flag is enabled, for backwards compatibility, to allow the lint to work as it’s working today for the people that want the validation in every place).
The following cases wouldn’t be validated, because those cases could be usage (and, in this case, it should not be a responsibility of the user the proper casing):
const x = {a: 1}const x: Foo = {a: 1}({a: 1} as Foo)return {a: 1}const x = {a : 1}; const y: Foo = xFor object destructuring, it should validate as variables (not as properties), which is the 1st case (variable definition):
For now, I’m using broader rules (instead of stricter rules, as I wanted) because of this issue, otherwise I would have to add several comments to disable this rule in several lines along my code to avoid errors that are outside my control.
With any issue in opened in this project - it either has a visible progress in the form of an attached PR, or it has no progress.
We are a community run project. The volunteer maintainers spend most of their time triaging issues and reviewing PRs. This means that most issues will not progress unless a member of the community steps up and champions it.
If this issue is important to you - consider being that champion.
If not - please just subscribe to the issue and wait patiently. Commenting asking for status updates does not bump issue priority in any way and just serves to spam everyone subscribed to the issue.
Here you go: https://github.com/typescript-eslint/typescript-eslint/issues/2250
This is maybe an unavoidable reality of the language, that will keep coming up over time. Type binding fuses language elements together in all sorts of crazy ways.
For what it’s worth, the frustration in this exercise was with not being able to express what we needed. I didn’t mind having to debug the interactions, or carefully think through all the cases. Most of those issues come to light pretty quickly when you test on a large code base.
In fact, I ended up having to make a macro to generate the
naming-conventionconfiguration, and consider each single selector individually. The “grouping selectors” only seemed to muddy the waters. This might not be representative of a casual user, though. 🙂Still, the end result was superior to forking naming-convention.ts (which we considered at one point heheh). Your all-in-one declarative approach does seem like a good design. 👍
We don’t want to lint
{ a: 1 }because@typescript-eslint/typedefalready enforces naming viax.We don’t want to lint
{ a: 1 }because@typescript-eslint/typedefalready enforces naming viaFooWe don’t want to lint
{ a: 1 }because@typescript-eslint/typedefalready enforces naming viaFooWe don’t want to lint
{ a: 1 }because@typescript-eslint/explicit-function-return-typealready does that.Not allowed because of
@typescript-eslint/typedef.Your view here seems biased by a coding style that relies heavily on type inference. It’s a very popular style, particularly for smaller teams who optimize for “writing code really concisely.” Larger teams often find themselves optimizing for “ensuring that an unknown person’s code will be easy for me to review/maintain.” Which approach is best probably depends on the situation.