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)

Most upvoted comments

I’d love to see naming-convention not 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 const declarations). 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 } = def matching 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.
    • for parity with property, this should not match property signatures with a function type.
  • propertyDeclaration, which only matches object literal’s property declarations.
    • this selector would be a rename of the existing property selector, minus matching property signatures.
  • methodSignature, which only matches an object type’s method signatures.
    • for parity with property, this should match property signatures with a function type.
  • methodDeclaration, which only matches object literal’s method declarations.
    • this selector would be a rename of the existing method selector, minus matching method signatures.

Define some new group selectors for backwards compatibility and easy of consumption:

  • property, which groups propertySignature and propertyDeclaration.
  • method, which groups methodSignature and methodDeclaration.

Also update the memberLike group 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:

const x: {[k: string]: unknown} = {
 aaaaa: 1,
 BBBBB: 2,
 AbAbA: 3,
 AA_BB: 4,
 aa_bb: 5,
 // etc
}

The problem here is “what constitutes usage?”

In my opinion, I expect this linting rule to only validate cases which are explicitly defined by the user, basically:

  • Variable definition
const correctCase = 1; // ok
const WrongCase = 2; // error
  • Function parameters
const myFunction = (param1, WrongParam2) => { // error at WrongParam2
    // function body
}
  • Interface definition:
interface MyContract {
    correctCase: string; // ok
    WrongCase: string; // error
    myFunction1: (param1, param2) => void; // ok
    myFunction2: (param1, WrongParam2) => void; // error at WrongParam2
}

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):

  • variable declaration without a type annotation const x = {a: 1}
  • variable declaration with a type annotation const x: Foo = {a: 1}
  • type assertion ({a: 1} as Foo)
  • return statement return {a: 1}
  • variable declaration without a type annotation that’s later used in one of the above cases const x = {a : 1}; const y: Foo = x

For object destructuring, it should validate as variables (not as properties), which is the 1st case (variable definition):

// The below cases should validate only the variables names, not the properties names
const { CONSTANT1, CONSTANT2 } = someLibrary.constants; // error
const { CONSTANT1: constant1, CONSTANT2: constant2 } = someLibrary.constants; // ok

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.

Why does CONSTANT1 match the “property” selector?

I believe it’ll actually try to match both the variable and property selectors. This is a bug. If you wouldn’t mind filing a new issue for this.

Here you go: https://github.com/typescript-eslint/typescript-eslint/issues/2250

There could be an additional selector for “property signatures” to distinguish between type declarations and object literals.

Though at first glance, this doesn’t seem like a great idea, as this would make it easy to configure a naming convention that is impossible to satisfy. I.e. “typeProperty format is snake_case” and “objectProperty format is camelCase” together would mean you can never declare an object literal against a known object type.

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-convention configuration, 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. 👍

The problem here is “what constitutes usage?”

Is it just an object literal inline in a call that constitutes usage? What about other cases?

* variable declaration without a type annotation `const x = {a: 1}`

We don’t want to lint { a: 1 } because @typescript-eslint/typedef already enforces naming via x.

* variable declaration with a type annotation `const x: Foo = {a: 1}`

We don’t want to lint { a: 1 } because @typescript-eslint/typedef already enforces naming via Foo

* type assertion `({a: 1} as Foo)`

We don’t want to lint { a: 1 } because @typescript-eslint/typedef already enforces naming via Foo

* return statement `return {a: 1}`

We don’t want to lint { a: 1 } because @typescript-eslint/explicit-function-return-type already does that.

* variable declaration without a type annotation that's later used in one of the above cases `const x = {a : 1}; const y: Foo = x`

Not allowed because of @typescript-eslint/typedef.

are all of these cases “usages”? Just some of them? It’s hard to define this exactly, and likely whatever definition you arrive at would feel kind of broken, as the rule would ignore some properties but not others.

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.