Skip to content

Commit 18d1ac2

Browse files
authored
no-array-for-each: Improve fixable parameter detection (sindresorhus#1177)
1 parent f9262d9 commit 18d1ac2

File tree

4 files changed

+20
-32
lines changed

4 files changed

+20
-32
lines changed

rules/no-array-for-each.js

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const extendFixRange = require('./utils/extend-fix-range');
1616
const isFunctionSelfUsedInside = require('./utils/is-function-self-used-inside');
1717
const {isNodeMatches} = require('./utils/is-node-matches');
1818
const assertToken = require('./utils/assert-token');
19+
const referenceIdentifierSelector = require('./utils/reference-identifier-selector');
1920

2021
const MESSAGE_ID = 'no-array-for-each';
2122
const messages = {
@@ -231,7 +232,7 @@ function isParameterSafeToFix(parameter, {scope, array, allIdentifiers}) {
231232

232233
const [arrayStart, arrayEnd] = array.range;
233234
for (const identifier of allIdentifiers) {
234-
const {name, range: [start, end], parent} = identifier;
235+
const {name, range: [start, end]} = identifier;
235236
if (
236237
name !== parameterName ||
237238
start < arrayStart ||
@@ -240,31 +241,6 @@ function isParameterSafeToFix(parameter, {scope, array, allIdentifiers}) {
240241
continue;
241242
}
242243

243-
if (
244-
(
245-
(
246-
parent.type === 'FunctionExpression' ||
247-
parent.type === 'ClassExpression' ||
248-
parent.type === 'FunctionDeclaration' ||
249-
parent.type === 'ClassDeclaration'
250-
) &&
251-
parent.id === identifier
252-
) ||
253-
(
254-
parent.type === 'MemberExpression' &&
255-
!parent.computed &&
256-
parent.property === identifier
257-
) ||
258-
(
259-
parent.type === 'Property' &&
260-
!parent.shorthand &&
261-
!parent.computed &&
262-
parent.key === identifier
263-
)
264-
) {
265-
continue;
266-
}
267-
268244
const variable = findVariable(scope, identifier);
269245
if (!variable || variable.scope === scope || isChildScope(scope, variable.scope)) {
270246
return false;
@@ -366,7 +342,7 @@ const create = context => {
366342
':function:exit'() {
367343
functionStack.pop();
368344
},
369-
Identifier(node) {
345+
[referenceIdentifierSelector()](node) {
370346
allIdentifiers.push(node);
371347
},
372348
ReturnStatement(node) {

rules/utils/reference-identifier-selector.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ function referenceIdentifierSelector(nameOrNames) {
7171
`:not(${nonReferenceSelector})`
7272
];
7373

74-
/* istanbul ignore else: no rule use single name yet */
75-
if (Array.isArray(nameOrNames)) {
76-
selector.push(`:matches(${nameOrNames.map(name => `[name="${name}"]`).join(', ')})`);
77-
} else {
78-
selector.push(`[name="${nameOrNames}"]`);
74+
if (nameOrNames) {
75+
/* istanbul ignore else: no rule use single name yet */
76+
if (Array.isArray(nameOrNames)) {
77+
selector.push(`:matches(${nameOrNames.map(name => `[name="${name}"]`).join(', ')})`);
78+
} else {
79+
selector.push(`[name="${nameOrNames}"]`);
80+
}
7981
}
8082

8183
return selector.join('');

test/snapshots/no-array-for-each.mjs.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,16 @@ Generated by [AVA](https://avajs.dev).
13941394
4 | }
13951395
5 | ].forEach((Foo, bar) => process(Foo, bar))
13961396

1397+
> Output
1398+
1399+
`␊
1400+
1 | for (const [bar, Foo] of [␊
1401+
2 | class Foo {␊
1402+
3 | bar() {}␊
1403+
4 | }␊
1404+
5 | ].entries()) process(Foo, bar)␊
1405+
`
1406+
13971407
> Error 1/1
13981408
13991409
`␊
21 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)