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

fix(eslint-plugin): [typedef] allow array/object destructuring in for/of #1570

Merged
merged 3 commits into from Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/eslint-plugin/docs/rules/typedef.md
Expand Up @@ -76,6 +76,9 @@ Examples of **correct** code with `{ "arrayDestructuring": true }`:
const [a]: number[] = [1];
const [b]: [number] = [2];
const [c, d]: [boolean, string] = [true, 'text'];

for (const [key, val] of new Map([['key', 1]])) {
}
```

### `arrowParameter`
Expand Down Expand Up @@ -144,6 +147,9 @@ Examples of **correct** code with `{ "objectDestructuring": true }`:
```ts
const { length }: { length: number } = 'text';
const [b, c]: [number, number] = Math.random() ? [1, 2] : [3, 4];

for (const { key, val } of [{ key: 'key', val: 1 }]) {
}
```

### `parameter`
Expand Down
37 changes: 35 additions & 2 deletions packages/eslint-plugin/src/rules/typedef.ts
Expand Up @@ -69,6 +69,31 @@ export default util.createRule<[Options], MessageIds>({
return node.type === AST_NODE_TYPES.Identifier ? node.name : undefined;
}

function isForOfStatementContext(
node: TSESTree.ArrayPattern | TSESTree.ObjectPattern,
): boolean {
let current: TSESTree.Node | undefined = node.parent;
while (current) {
switch (current.type) {
case AST_NODE_TYPES.VariableDeclarator:
case AST_NODE_TYPES.VariableDeclaration:
case AST_NODE_TYPES.ObjectPattern:
case AST_NODE_TYPES.ArrayPattern:
case AST_NODE_TYPES.Property:
current = current.parent;
break;

case AST_NODE_TYPES.ForOfStatement:
return true;

default:
current = undefined;
}
}

return false;
}

function checkParameters(params: TSESTree.Parameter[]): void {
for (const param of params) {
let annotationNode: TSESTree.Node | undefined;
Expand Down Expand Up @@ -102,7 +127,11 @@ export default util.createRule<[Options], MessageIds>({

return {
ArrayPattern(node): void {
if (options[OptionKeys.ArrayDestructuring] && !node.typeAnnotation) {
if (
options[OptionKeys.ArrayDestructuring] &&
!node.typeAnnotation &&
!isForOfStatementContext(node)
) {
report(node);
}
},
Expand Down Expand Up @@ -132,7 +161,11 @@ export default util.createRule<[Options], MessageIds>({
}
},
ObjectPattern(node): void {
if (options[OptionKeys.ObjectDestructuring] && !node.typeAnnotation) {
if (
options[OptionKeys.ObjectDestructuring] &&
!node.typeAnnotation &&
!isForOfStatementContext(node)
) {
report(node);
}
},
Expand Down
48 changes: 48 additions & 0 deletions packages/eslint-plugin/tests/rules/typedef.test.ts
Expand Up @@ -38,6 +38,30 @@ ruleTester.run('typedef', rule, {
},
],
},
{
code: `for (const [key, val] of new Map([['key', 1]])) {}`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add a test for nested destructuring

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']}}}]) {}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it - won't the rule break on these cases anyways?

  const [[[{ key }]]]: [[[{ key: string }]]] = [[[{ key: 'value' }]]]
//         ^^^^^^^    report ❌
//        ^^^^^^^^^   report ❌
//       ^^^^^^^^^^^  report ❌
//      ^^^^^^^^^^^^^ no report ✅ 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bradzacher Added additional tests. About the last case, I think the current implementation doesn't handle that, and the proposed changes will not handle/break that behavior.

options: [
{
arrayDestructuring: true,
},
],
},
{
code: `for (const [[key]] of [[['key']]]) {}`,
options: [
{
arrayDestructuring: true,
},
],
},
{
code: `for (const [[{ key }]] of [[[{ key: 'value' }]]]) {}`,
options: [
{
arrayDestructuring: true,
},
],
},
`let a: number;
[a] = [1];`,
// Arrow parameters
Expand Down Expand Up @@ -93,6 +117,22 @@ ruleTester.run('typedef', rule, {
},
],
},
{
code: `for (const {p1: {p2: { p3 }}} of [{p1: {p2: {p3: 'value'}}}]) {}`,
options: [
{
objectDestructuring: true,
},
],
},
{
code: `for (const {p1: {p2: { p3: [key] }}} of [{p1: {p2: {p3: ['value']}}}]) {}`,
options: [
{
objectDestructuring: true,
},
],
},
{
code: `const { a } = { a: 1 };`,
options: [
Expand All @@ -101,6 +141,14 @@ ruleTester.run('typedef', rule, {
},
],
},
{
code: `for (const { key, val } of [{ key: 'key', val: 1 }]) {}`,
options: [
{
objectDestructuring: true,
},
],
},
// Function parameters
`function receivesNumber(a: number): void { }`,
`function receivesStrings(a: string, b: string): void { }`,
Expand Down