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

'typedef/arrayDestructuring': impossible to follow this rule in for/of statement #1564

Closed
TokugawaTakeshi opened this issue Feb 4, 2020 · 5 comments · Fixed by #1570
Closed
Labels
bug Something isn't working good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@TokugawaTakeshi
Copy link

TokugawaTakeshi commented Feb 4, 2020

The rule @typescript-eslint/typedef with arrayDestructuring options tell me to annotate the type inside for/of statement:

image

However, it's impossible in TypeScript:

image

It's required to define the exclusion for for/of (and, I suppose, for/in) statements.

{
  "rules": {
    "@typescript-eslint/typedef": [
      "error",
      {
	  "arrayDestructuring": true,
	  "arrowParameter": true,
	  "memberVariableDeclaration": true,
	  "objectDestructuring": true,
	  "parameter": true,
	  "propertyDeclaration": true,
	  "variableDeclaration": true
	}
    ]
  }
}
const test: { 
  alpha: string, 
  bravo: string 
} = {
  alpha: "foo", 
  bravo: "bar"
};

for (const [ key, value ] of Object.entries(test)) {
  console.log(key);
  console.log(value);
}

What did you expect to happen?

Row for (const [ key, value ] of Object.entries(test)) { does not break the rule 'typedef/arrayDestructuring' because it's impossible to annotate types here in TypeScript.

What actually happened?
It is.

Versions

package version
@typescript-eslint/eslint-plugin 2.19.0 (newest for the moment when I opened this issue)
@typescript-eslint/parser 2.19.0 (newest for the moment when I opened this issue)
@typescript-eslint/typescript-estree 2.19.0
@typescript-eslint/experimental-utils 2.19.0
TypeScript 3.7.5 (newest for the moment when I opened this issue)
node 12.14.1
npm 6.3.14
@bradzacher bradzacher added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin labels Feb 4, 2020
@bradzacher
Copy link
Member

The problem here is that the rule selects on ArrayPattern for arrayDestructuring, and doesn't check the parent like it does for VariableDeclarator.

Similar problem for the objectDestructuring option.

@TokugawaTakeshi
Copy link
Author

@bradzacher Thank you for the explanation about cause of this rule.
But also ask your team to fix this because it conflicts with TypeScript rules.

@bradzacher
Copy link
Member

happy to accept a PR if you need it fixed urgently. It's a simple enough fix.

@TokugawaTakeshi
Copy link
Author

TokugawaTakeshi commented Feb 9, 2020

@bradzacher

happy to accept a PR if you need it fixed urgently. It's a simple enough fix.

I am very sorry to decline you invitation to @typescript/eslint development, but because of time shortage and fatigue, I have to do it FOR NOW. I want collaborate with you, but currently all can I do for you is bug reporting. Please understand, and see you next time.

@a-tarasyuk
Copy link
Contributor

a-tarasyuk commented Feb 11, 2020

Additional cases

for (const [[key]] of [[['key']]]) {}
for (const [[{ key }]] of [[[{ key: 'value' }]]]) {}

for (const {p1: {p2: { p3 }}} of [{p1: {p2: {p3: 'value'}}}]) {}
for (const {p1: {p2: { p3: [key] }}} of [{p1: {p2: {p3: ['value']}}}]) {}
const [[[{ key }]]]: [[[{ key: string }]]] = [[[{ key: 'value' }]]]

Taken from #1570 (comment)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
3 participants