From 6d19efed16d1cf0357ad363b6373d2021c49a8c8 Mon Sep 17 00:00:00 2001 From: Raquel Amorim <36057168+amorimr@users.noreply.github.com> Date: Fri, 22 Jul 2022 13:28:17 -0400 Subject: [PATCH] fix(eslint-plugin): [typedef] Support nested array destructuring with type annotation (#5311) --- 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 a995fe41b37..4f5fe6eae65 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 19b6ff5a543..d6f724bfd0b 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: [