Skip to content
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

Rule enforcing typeAnnotation on a complex query is correct #340

Open
JoshuaKGoldberg opened this issue Feb 10, 2023 · 1 comment
Open

Comments

@JoshuaKGoldberg
Copy link
Contributor

Copying conversation from typescript-eslint/typescript-eslint#4065 (comment) & typescript-eslint/typescript-eslint#6444 (comment): typescript-eslint doesn't have a way to infer the types of nodes when the ESQuery selector is complex:

return {
  'ClassDeclaration, ClassExpression[id]'(
    node
    // ^? TSESTree.Node
    // Expected: something like...
    // TSESTree.ClassDeclaration | TSESTree.ClassExpression & { id: TSESTree.Identifier }
  ) {

Even with these two not(-yet?)-implemented helpers (neither of which may ever be useful for public consumers):

...there's no way for custom rules written in TypeScript to enforce that the type of node matches its AST shape. Which means devs can write blatantly wrong code like:

return {
  'ClassDeclaration, ClassExpression[id]'(
    node: TSESTree.Identifier

Is eslint-plugin-eslint-plugin an appropriate place to write a lint rule that makes sure the type annotation on a node matches the node itself?

This rule might be tricky to write given that microsoft/TypeScript#9879 blocks having a TypeScript API to check type assignability. The rule could use basic string comparisons in the meantime.

@bmish
Copy link
Member

bmish commented Feb 10, 2023

I'm open to accepting rules targeted at TypeScript rules/syntax, including this one.

I previously added general compatibility with TypeScript rules in v4.

Any new rule targeted at TypeScript rules should just no-op when running against a JavaScript rule.

We can even use meta.docs.category: "TypeScript" to have a separate group/list of our TypeScript-focused rules (related: #341).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants