From d68261eb1de104c6d2bd853f133efcc5d61065f0 Mon Sep 17 00:00:00 2001 From: Raquel Amorim Date: Tue, 5 Jul 2022 18:27:43 -0400 Subject: [PATCH] fix typedef rule for deep array destructuring --- packages/eslint-plugin/src/rules/typedef.ts | 6 +++-- .../eslint-plugin/tests/rules/typedef.test.ts | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/typedef.ts b/packages/eslint-plugin/src/rules/typedef.ts index a995fe41b37f..4f5fe6eae654 100644 --- a/packages/eslint-plugin/src/rules/typedef.ts +++ b/packages/eslint-plugin/src/rules/typedef.ts @@ -150,13 +150,14 @@ export default util.createRule<[Options], MessageIds>({ } function isAncestorHasTypeAnnotation( - node: TSESTree.ObjectPattern, + node: TSESTree.ObjectPattern | TSESTree.ArrayPattern, ): boolean { let ancestor = node.parent; while (ancestor) { if ( - ancestor.type === AST_NODE_TYPES.ObjectPattern && + (ancestor.type === AST_NODE_TYPES.ObjectPattern || + ancestor.type === AST_NODE_TYPES.ArrayPattern) && ancestor.typeAnnotation ) { return true; @@ -181,6 +182,7 @@ export default util.createRule<[Options], MessageIds>({ if ( !node.typeAnnotation && !isForOfStatementContext(node) && + !isAncestorHasTypeAnnotation(node) && node.parent?.type !== AST_NODE_TYPES.AssignmentExpression ) { report(node); diff --git a/packages/eslint-plugin/tests/rules/typedef.test.ts b/packages/eslint-plugin/tests/rules/typedef.test.ts index 19b6ff5a543b..d6f724bfd0b4 100644 --- a/packages/eslint-plugin/tests/rules/typedef.test.ts +++ b/packages/eslint-plugin/tests/rules/typedef.test.ts @@ -70,6 +70,32 @@ ruleTester.run('typedef', rule, { }, ], }, + { + code: 'const [[a]]: number[][] = [[1]];', + options: [ + { + arrayDestructuring: true, + }, + ], + }, + { + code: 'const foo = ([{ bar }]: { bar: string }[]) => {};', + options: [ + { + arrayDestructuring: true, + objectDestructuring: true, + }, + ], + }, + { + code: 'const foo = ([{ bar }]: [{ bar: string }]) => {};', + options: [ + { + arrayDestructuring: true, + objectDestructuring: true, + }, + ], + }, { code: 'const [a] = [1];', options: [ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy