Skip to content

Commit

Permalink
Rename getChildScopesRecursive to getScopes and reuse it (#1424)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Jul 14, 2021
1 parent 507b18b commit 4897202
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 29 deletions.
14 changes: 5 additions & 9 deletions rules/no-for-loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
const {isClosingParenToken, getStaticValue} = require('eslint-utils');
const isLiteralValue = require('./utils/is-literal-value.js');
const avoidCapture = require('./utils/avoid-capture.js');
const getChildScopesRecursive = require('./utils/get-child-scopes-recursive.js');
const getScopes = require('./utils/get-scopes.js');
const singular = require('./utils/singular.js');
const toLocation = require('./utils/to-location.js');
const getReferences = require('./utils/get-references.js');

const MESSAGE_ID = 'no-for-loop';
const messages = {
Expand Down Expand Up @@ -261,13 +262,8 @@ const someVariablesLeakOutOfTheLoop = (forStatement, variables, forScope) => {
});
};

const getReferencesInChildScopes = (scope, name) => {
const references = scope.references.filter(reference => reference.identifier.name === name);
return [
...references,
...scope.childScopes.flatMap(s => getReferencesInChildScopes(s, name)),
];
};
const getReferencesInChildScopes = (scope, name) =>
getReferences(scope).filter(reference => reference.identifier.name === name);

const create = context => {
const sourceCode = context.getSourceCode();
Expand Down Expand Up @@ -355,7 +351,7 @@ const create = context => {

const index = indexIdentifierName;
const element = elementIdentifierName ||
avoidCapture(singular(arrayIdentifierName) || defaultElementName, getChildScopesRecursive(bodyScope));
avoidCapture(singular(arrayIdentifierName) || defaultElementName, getScopes(bodyScope));
const array = arrayIdentifierName;

let declarationElement = element;
Expand Down
4 changes: 2 additions & 2 deletions rules/prefer-array-find.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
} = require('./selectors/index.js');
const getVariableIdentifiers = require('./utils/get-variable-identifiers.js');
const avoidCapture = require('./utils/avoid-capture.js');
const getChildScopesRecursive = require('./utils/get-child-scopes-recursive.js');
const getScopes = require('./utils/get-scopes.js');
const singular = require('./utils/singular.js');
const {
extendFixRange,
Expand Down Expand Up @@ -299,7 +299,7 @@ const create = context => {
const singularName = singular(node.id.name);
if (singularName) {
// Rename variable to be singularized now that it refers to a single item in the array instead of the entire array.
const singularizedName = avoidCapture(singularName, getChildScopesRecursive(scope));
const singularizedName = avoidCapture(singularName, getScopes(scope));
yield * renameVariable(variable, singularizedName, fixer);

// Prevent possible variable conflicts
Expand Down
14 changes: 5 additions & 9 deletions rules/prevent-abbreviations.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const getVariableIdentifiers = require('./utils/get-variable-identifiers.js');
const isStaticRequire = require('./utils/is-static-require.js');
const {defaultReplacements, defaultAllowList} = require('./shared/abbreviations.js');
const {renameVariable} = require('./fix/index.js');
const getScopes = require('./utils/get-scopes.js');

const isUpperCase = string => string === string.toUpperCase();
const isUpperFirst = string => isUpperCase(string[0]);
Expand Down Expand Up @@ -447,16 +448,11 @@ const create = context => {
}
};

const checkChildScopes = scope => {
for (const childScope of scope.childScopes) {
checkScope(childScope);
}
};

const checkScope = scope => {
checkVariables(scope);

return checkChildScopes(scope);
const scopes = getScopes(scope);
for (const scope of scopes) {
checkVariables(scope);
}
};

return {
Expand Down
4 changes: 2 additions & 2 deletions rules/utils/avoid-capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
isKeyword,
} = require('@babel/helper-validator-identifier');
const resolveVariableName = require('./resolve-variable-name.js');
const getReferences = require('./get-references.js');

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

const isSafeName = (name, scopes) =>
!scopes.some(scope => resolveVariableName(name, scope) || isUnresolvedName(name, scope));
Expand Down
8 changes: 4 additions & 4 deletions rules/utils/get-references.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
const {uniq} = require('lodash');
const getScopes = require('./get-scopes.js');

const getReferences = scope => uniq([
...scope.references,
...scope.childScopes.flatMap(scope => getReferences(scope)),
]);
const getReferences = scope => uniq(
getScopes(scope).flatMap(({references}) => references),
);

module.exports = getReferences;
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Gather a list of all Scopes starting recursively from the input Scope.
@param {Scope} scope - The Scope to start checking from.
@returns {Scope[]} - The resulting Scopes.
*/
const getChildScopesRecursive = scope => [
const getScopes = scope => [
scope,
...scope.childScopes.flatMap(scope => getChildScopesRecursive(scope)),
...scope.childScopes.flatMap(scope => getScopes(scope)),
];

module.exports = getChildScopesRecursive;
module.exports = getScopes;

0 comments on commit 4897202

Please sign in to comment.