eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

Rules improvements with the help of `@typescript-eslint/parser`

Open astoilkov opened this issue 6 years ago • 11 comments

I don't have experience with writing ESLint rules but I am wondering if @typescript-eslint/parser can help improve some of the rules. For example, prefer-event-key can extend its reach by detecting that the object type is KeyboardEvent and make the check.

function onKeyDown(e: KeyboardEvent) {
  if (e.keyCode) {
    // ...
  }
}

astoilkov avatar Aug 25 '19 07:08 astoilkov

I have been thinking a lot about this too. We have encountered many limitations with rules here that would not have happened with better type information. I'm open to exploring making rules better by using type information when available. I can even see us adding TypeScript-only rules where it's not feasible to do it without type information.

See this relevant discussion: https://github.com/typescript-eslint/typescript-eslint/issues/542#issuecomment-494465051

If we decide to do this, I think it would be best to start with one of the simpler rules, just to experiment.

@MrHen @futpib Thoughts?

sindresorhus avatar Sep 05 '19 19:09 sindresorhus

I'm for typing and TypeScript but I am against supporting gobs of different pseudo-JavaScript languages based on the whims of whatever maintainers happen to be around. Babel seems ubiquitous at this point and I have a bias toward TypeScript because I've used it in the past which is why I'm comfortable with those two. But, personally, I'm not remotely interested in Flow or CoffeeScript or whatever flavor of the month pops out next.

Perhaps we should explicitly create eslint-plugin-unicorn-xyz repos to store language specific rules? I'm more than willing to sign up for eslint-plugin-unicorn-typescript rules. Unless we are fine explicitly blessing TypeScript and that's it?

Not sure what long term plans @sindresorhus has for supporting TypeScript versus Flow, versus the next best thing. Are you all-in on TypeScript at this point?

MrHen avatar Sep 05 '19 19:09 MrHen

@MrHen This is only about TypeScript. I have no intention of supporting Flow.

Perhaps we should explicitly create eslint-plugin-unicorn-xyz repos to store language specific rules?

The problem with that is that the point here is to have one rule that works in both JS and TS, but works better in TS by taking advantage of the type information.

Unless we are fine explicitly blessing TypeScript and that's it?

Yes

sindresorhus avatar Sep 05 '19 20:09 sindresorhus

Them I'm all for it. Viva la typing.

MrHen avatar Sep 05 '19 20:09 MrHen

I'm gonna keep a list of things that could be improved with type information:

  • We could exclude built-in types here: https://github.com/sindresorhus/eslint-plugin-unicorn/issues/367
  • Auto-fixer for no-fn-reference-in-iterator (https://github.com/sindresorhus/eslint-plugin-unicorn/issues/418)

sindresorhus avatar Oct 08 '19 10:10 sindresorhus

This would be especially helpful for the "prevent-abbreviations" rule. At the moment, I can't use it with TypeScript because the fix removes the typings for function/method parameters which is disastrous for parameter objects.

Torhal avatar Nov 15 '19 20:11 Torhal

@Torhal Agreed. Moved it into a new issue: https://github.com/sindresorhus/eslint-plugin-unicorn/issues/438

sindresorhus avatar Nov 16 '19 08:11 sindresorhus

#608 Didn't fix this, I missed some info.

fisker avatar Mar 17 '20 08:03 fisker

I have another case where React.Children.map().flat() gets flagged by prefer-flatmap even though React.Children has no flatMap() 😕

felixfbecker avatar Apr 07 '20 08:04 felixfbecker

@felixfbecker Can you open a separate issue about this? Thank you

fisker avatar Apr 07 '20 08:04 fisker

I created a label to group all the issues that would benefit from type information: https://github.com/sindresorhus/eslint-plugin-unicorn/labels/types

fregante avatar Sep 04 '24 07:09 fregante