Skip to content

Commit

Permalink
fix(eslint-plugin): [typedef] Support nested array destructuring with…
Browse files Browse the repository at this point in the history
… type annotation (#5311)
  • Loading branch information
amorimr committed Jul 22, 2022
1 parent 48f996e commit 6d19efe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/eslint-plugin/src/rules/typedef.ts
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions packages/eslint-plugin/tests/rules/typedef.test.ts
Expand Up @@ -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: [
Expand Down

0 comments on commit 6d19efe

Please sign in to comment.