Skip to content

Commit 4897202

Browse files
authored
Rename getChildScopesRecursive to getScopes and reuse it (#1424)
1 parent 507b18b commit 4897202

File tree

6 files changed

+21
-29
lines changed

6 files changed

+21
-29
lines changed

rules/no-for-loop.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
const {isClosingParenToken, getStaticValue} = require('eslint-utils');
33
const isLiteralValue = require('./utils/is-literal-value.js');
44
const avoidCapture = require('./utils/avoid-capture.js');
5-
const getChildScopesRecursive = require('./utils/get-child-scopes-recursive.js');
5+
const getScopes = require('./utils/get-scopes.js');
66
const singular = require('./utils/singular.js');
77
const toLocation = require('./utils/to-location.js');
8+
const getReferences = require('./utils/get-references.js');
89

910
const MESSAGE_ID = 'no-for-loop';
1011
const messages = {
@@ -261,13 +262,8 @@ const someVariablesLeakOutOfTheLoop = (forStatement, variables, forScope) => {
261262
});
262263
};
263264

264-
const getReferencesInChildScopes = (scope, name) => {
265-
const references = scope.references.filter(reference => reference.identifier.name === name);
266-
return [
267-
...references,
268-
...scope.childScopes.flatMap(s => getReferencesInChildScopes(s, name)),
269-
];
270-
};
265+
const getReferencesInChildScopes = (scope, name) =>
266+
getReferences(scope).filter(reference => reference.identifier.name === name);
271267

272268
const create = context => {
273269
const sourceCode = context.getSourceCode();
@@ -355,7 +351,7 @@ const create = context => {
355351

356352
const index = indexIdentifierName;
357353
const element = elementIdentifierName ||
358-
avoidCapture(singular(arrayIdentifierName) || defaultElementName, getChildScopesRecursive(bodyScope));
354+
avoidCapture(singular(arrayIdentifierName) || defaultElementName, getScopes(bodyScope));
359355
const array = arrayIdentifierName;
360356

361357
let declarationElement = element;

rules/prefer-array-find.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
} = require('./selectors/index.js');
88
const getVariableIdentifiers = require('./utils/get-variable-identifiers.js');
99
const avoidCapture = require('./utils/avoid-capture.js');
10-
const getChildScopesRecursive = require('./utils/get-child-scopes-recursive.js');
10+
const getScopes = require('./utils/get-scopes.js');
1111
const singular = require('./utils/singular.js');
1212
const {
1313
extendFixRange,
@@ -299,7 +299,7 @@ const create = context => {
299299
const singularName = singular(node.id.name);
300300
if (singularName) {
301301
// Rename variable to be singularized now that it refers to a single item in the array instead of the entire array.
302-
const singularizedName = avoidCapture(singularName, getChildScopesRecursive(scope));
302+
const singularizedName = avoidCapture(singularName, getScopes(scope));
303303
yield * renameVariable(variable, singularizedName, fixer);
304304

305305
// Prevent possible variable conflicts

rules/prevent-abbreviations.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const getVariableIdentifiers = require('./utils/get-variable-identifiers.js');
1010
const isStaticRequire = require('./utils/is-static-require.js');
1111
const {defaultReplacements, defaultAllowList} = require('./shared/abbreviations.js');
1212
const {renameVariable} = require('./fix/index.js');
13+
const getScopes = require('./utils/get-scopes.js');
1314

1415
const isUpperCase = string => string === string.toUpperCase();
1516
const isUpperFirst = string => isUpperCase(string[0]);
@@ -447,16 +448,11 @@ const create = context => {
447448
}
448449
};
449450

450-
const checkChildScopes = scope => {
451-
for (const childScope of scope.childScopes) {
452-
checkScope(childScope);
453-
}
454-
};
455-
456451
const checkScope = scope => {
457-
checkVariables(scope);
458-
459-
return checkChildScopes(scope);
452+
const scopes = getScopes(scope);
453+
for (const scope of scopes) {
454+
checkVariables(scope);
455+
}
460456
};
461457

462458
return {

rules/utils/avoid-capture.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
isKeyword,
66
} = require('@babel/helper-validator-identifier');
77
const resolveVariableName = require('./resolve-variable-name.js');
8+
const getReferences = require('./get-references.js');
89

910
// https://github.com/microsoft/TypeScript/issues/2536#issuecomment-87194347
1011
const typescriptReservedWords = new Set([
@@ -98,8 +99,7 @@ function unicorn() {
9899
```
99100
*/
100101
const isUnresolvedName = (name, scope) =>
101-
scope.references.some(reference => reference.identifier && reference.identifier.name === name && !reference.resolved) ||
102-
scope.childScopes.some(scope => isUnresolvedName(name, scope));
102+
getReferences(scope).some(({identifier, resolved}) => identifier && identifier.name === name && !resolved);
103103

104104
const isSafeName = (name, scopes) =>
105105
!scopes.some(scope => resolveVariableName(name, scope) || isUnresolvedName(name, scope));

rules/utils/get-references.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22
const {uniq} = require('lodash');
3+
const getScopes = require('./get-scopes.js');
34

4-
const getReferences = scope => uniq([
5-
...scope.references,
6-
...scope.childScopes.flatMap(scope => getReferences(scope)),
7-
]);
5+
const getReferences = scope => uniq(
6+
getScopes(scope).flatMap(({references}) => references),
7+
);
88

99
module.exports = getReferences;

rules/utils/get-child-scopes-recursive.js renamed to rules/utils/get-scopes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Gather a list of all Scopes starting recursively from the input Scope.
66
@param {Scope} scope - The Scope to start checking from.
77
@returns {Scope[]} - The resulting Scopes.
88
*/
9-
const getChildScopesRecursive = scope => [
9+
const getScopes = scope => [
1010
scope,
11-
...scope.childScopes.flatMap(scope => getChildScopesRecursive(scope)),
11+
...scope.childScopes.flatMap(scope => getScopes(scope)),
1212
];
1313

14-
module.exports = getChildScopesRecursive;
14+
module.exports = getScopes;

0 commit comments

Comments
 (0)