typescript-eslint: Enhancement: [naming-convention] don't error for object keys in typed objects

Before You File a Proposal Please Confirm You Have Done The Following…

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule’s documentation

https://typescript-eslint.io/rules/naming-convention

Description

I have several projects that are using @typescript-eslint/naming-convention to enforce camelCase properties in object literals (among other places). However, there are quite a few libraries out there that take options objects with snake_case keys as part of their API. This means that any time I want to specify one of those options I need to eslint-disable the naming-convention rule, otherwise it errors.

My proposal is to either add an extra flag (or a separate rule, but it’s really an extension of naming-convention) that looks at the type information of the object literal being defined, and skips over any keys that are explicitly defined in that type. Type definitions, generic/typeless object literals, and unknown keys should still be subject to validation, just not known keys in explicitly typed object literals.

Fail

// With format set to not allow 'snake_case'

const someObj = {
  bad_key: 'hello',
};

interface SomeObjectType {
  something_here: string;
}

type MapType: Record<string, unknown>;
const someMap: MapType = {
  // The type does not dictate that this key must be snake_case,
  // so it should error.
  bad_key: true;
}

Pass

// With format set to not allow 'snake_case'

interface SomeObjectType {
  // eslint-disable-next-line @typescript-eslint/naming-convention
  something_here: string;
}

const anotherObject: SomeObjectType = {
  // This one should not error, since this format is _required_ for the object
  // to match the type, and so the programmer here has no choice but to use the
  // bad syntax.
  something_here: 'no error!',
};

function doSomething(obj: SomeObjectType) {}

// This should also pass since the key format is outside the caller's control.
doSomething({ something_here: 'just works!' });

type CombinedType = SomeObjectType & Record<string, unknown>;
const complex: CombinedType = {
  // Even though CombinedType has a generic `[key: string]` typing, it also
  // explicitly has the key `something_here`, which should be allowed.
  something_here: 'still works',
}

Additional Info

No response

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 22 (10 by maintainers)

Most upvoted comments

Yeah from my experience most codebases use camel case as do most libraries.

So the mismatches you have are:

  • codebases that use camel case (common) interfacing with libraries that don’t accept camel case (rare - aws and docker for example)
  • codebases that don’t use camel case (rare) interfacing with libraries that do (common)

For the second case - that’s where you’d really want this sort of feature. But it’s a small subset of our user base that uses that style - so there’s very little motivation there.

For the first (at least with docker and aws) the interaction surface area is often pretty small relative to the entire codebase size - meaning its either feasible to allowlist the few names needed, or its palatable enough to use disable comments on the dozen or so lines. So there’s not a huge amount of motivation there either. People kind of “make do” and move on.

If we could cheaply and quickly calculate types I would do this in a heartbeat. But as it stands naming-convention is already one of the slowest rules on large codebases due to the sheer volume of things it checks the AST for - so adding types is going to exacerbate that a lot. Anecdotal perf side note - it’s the slowest rule but 5x (about 30s - 15% of the lint time) on the internal Canva codebase which is only configured with the typeLike selector! Perf is an open todo for me to look into.

Got it, thanks for the clarification @bradzacher. I’ll put this back in pending then.

I.e. we’d need to see a lot of community interest to consider pulling it in. My guess is we won’t, and in a few months we can close this out. Unless someone makes a very compelling case in favor!