Skip to content

Commit 5603473

Browse files
Josh Goldbergbradzacher
Josh Goldberg
authored andcommitted
fix(eslint-plugin): [typedef] don't flag destructuring when variables is disabled (typescript-eslint#819)
1 parent 16136f3 commit 5603473

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

packages/eslint-plugin/src/rules/typedef.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -141,36 +141,35 @@ export default util.createRule<[Options], MessageIds>({
141141
},
142142
VariableDeclarator(node): void {
143143
if (
144-
options[OptionKeys.VariableDeclaration] &&
145-
!node.id.typeAnnotation
144+
!options[OptionKeys.VariableDeclaration] ||
145+
node.id.typeAnnotation ||
146+
(node.id.type === AST_NODE_TYPES.ArrayPattern &&
147+
!options[OptionKeys.ArrayDestructuring]) ||
148+
(node.id.type === AST_NODE_TYPES.ObjectPattern &&
149+
!options[OptionKeys.ObjectDestructuring])
146150
) {
147-
// Are we inside a context that does not allow type annotations?
148-
let typeAnnotationRequired = true;
149-
150-
let current: TSESTree.Node | undefined = node.parent;
151-
while (current) {
152-
switch (current.type) {
153-
case AST_NODE_TYPES.VariableDeclaration:
154-
// Keep looking upwards
155-
current = current.parent;
156-
break;
157-
case AST_NODE_TYPES.ForOfStatement:
158-
case AST_NODE_TYPES.ForInStatement:
159-
// Stop traversing and don't report an error
160-
typeAnnotationRequired = false;
161-
current = undefined;
162-
break;
163-
default:
164-
// Stop traversing
165-
current = undefined;
166-
break;
167-
}
168-
}
151+
return;
152+
}
169153

170-
if (typeAnnotationRequired) {
171-
report(node, getNodeName(node.id));
154+
let current: TSESTree.Node | undefined = node.parent;
155+
while (current) {
156+
switch (current.type) {
157+
case AST_NODE_TYPES.VariableDeclaration:
158+
// Keep looking upwards
159+
current = current.parent;
160+
break;
161+
case AST_NODE_TYPES.ForOfStatement:
162+
case AST_NODE_TYPES.ForInStatement:
163+
// Stop traversing and don't report an error
164+
return;
165+
default:
166+
// Stop traversing
167+
current = undefined;
168+
break;
172169
}
173170
}
171+
172+
report(node, getNodeName(node.id));
174173
},
175174
};
176175
},

packages/eslint-plugin/tests/rules/typedef.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,24 @@ ruleTester.run('typedef', rule, {
198198
},
199199
],
200200
},
201+
{
202+
code: `const [a, b] = [1, 2];`,
203+
options: [
204+
{
205+
objectDestructuring: false,
206+
variableDeclaration: true,
207+
},
208+
],
209+
},
210+
{
211+
code: `const { a, b } = { a: '', b: '' };`,
212+
options: [
213+
{
214+
objectDestructuring: false,
215+
variableDeclaration: true,
216+
},
217+
],
218+
},
201219
// Contexts where TypeScript doesn't allow annotations
202220
{
203221
code: `for (x of [1, 2, 3]) { }`,

0 commit comments

Comments
 (0)
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