Skip to content

Commit a3c6dab

Browse files
authored
Rename avoidCapture to getAvailableVariableName (#2539)
1 parent c9c056c commit a3c6dab

9 files changed

+45
-35
lines changed

rules/catch-error-name.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {isRegExp} from 'node:util/types';
22
import {findVariable} from '@eslint-community/eslint-utils';
3-
import avoidCapture from './utils/avoid-capture.js';
3+
import {getAvailableVariableName} from './utils/index.js';
44
import {renameVariable} from './fix/index.js';
55
import {isMethodCall} from './ast/index.js';
66

@@ -85,7 +85,7 @@ const create = context => {
8585
variable.scope,
8686
...variable.references.map(({from}) => from),
8787
];
88-
const fixedName = avoidCapture(expectedName, scopes);
88+
const fixedName = getAvailableVariableName(expectedName, scopes);
8989

9090
const problem = {
9191
node,

rules/consistent-destructuring.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import avoidCapture from './utils/avoid-capture.js';
2-
import isLeftHandSide from './utils/is-left-hand-side.js';
1+
import {getAvailableVariableName, isLeftHandSide} from './utils/index.js';
32
import {isCallOrNewExpression} from './ast/index.js';
43

54
const MESSAGE_ID = 'consistentDestructuring';
@@ -107,7 +106,7 @@ const create = context => {
107106
}
108107

109108
// Destructured member collides with an existing identifier
110-
if (avoidCapture(member, [memberScope]) !== member) {
109+
if (getAvailableVariableName(member, [memberScope]) !== member) {
111110
return;
112111
}
113112
}

rules/no-anonymous-default-export.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import helperValidatorIdentifier from '@babel/helper-validator-identifier';
44
import getClassHeadLocation from './utils/get-class-head-location.js';
55
import {upperFirst, camelCase} from './utils/lodash.js';
66
import {getParenthesizedRange} from './utils/parentheses.js';
7-
import {getScopes, avoidCapture} from './utils/index.js';
7+
import {getScopes, getAvailableVariableName} from './utils/index.js';
88
import {isMemberExpression} from './ast/index.js';
99

1010
const {isIdentifierName} = helperValidatorIdentifier;
@@ -42,7 +42,7 @@ function getSuggestionName(node, filename, sourceCode) {
4242
}
4343

4444
name = node.type === 'ClassDeclaration' || node.type === 'ClassExpression' ? upperFirst(name) : name;
45-
name = avoidCapture(name, getScopes(sourceCode.getScope(node)));
45+
name = getAvailableVariableName(name, getScopes(sourceCode.getScope(node)));
4646

4747
return name;
4848
}

rules/no-for-loop.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {isClosingParenToken, getStaticValue} from '@eslint-community/eslint-utils';
2-
import avoidCapture from './utils/avoid-capture.js';
3-
import getScopes from './utils/get-scopes.js';
4-
import singular from './utils/singular.js';
5-
import toLocation from './utils/to-location.js';
6-
import getReferences from './utils/get-references.js';
2+
import {
3+
getAvailableVariableName,
4+
getScopes,
5+
singular,
6+
toLocation,
7+
getReferences,
8+
} from './utils/index.js';
79
import {isLiteral} from './ast/index.js';
810

911
const MESSAGE_ID = 'no-for-loop';
@@ -348,7 +350,7 @@ const create = context => {
348350
const shouldGenerateIndex = isIndexVariableUsedElsewhereInTheLoopBody(indexVariable, bodyScope, arrayIdentifierName);
349351
const index = indexIdentifierName;
350352
const element = elementIdentifierName
351-
|| avoidCapture(singular(arrayIdentifierName) || defaultElementName, getScopes(bodyScope));
353+
|| getAvailableVariableName(singular(arrayIdentifierName) || defaultElementName, getScopes(bodyScope));
352354
const array = arrayIdentifierName;
353355

354356
let declarationElement = element;

rules/prefer-array-find.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
isLeftHandSide,
1010
singular,
1111
getScopes,
12-
avoidCapture,
12+
getAvailableVariableName,
1313
getVariableIdentifiers,
1414
} from './utils/index.js';
1515
import {isMethodCall} from './ast/index.js';
@@ -316,7 +316,7 @@ const create = context => {
316316
const singularName = singular(node.id.name);
317317
if (singularName) {
318318
// Rename variable to be singularized now that it refers to a single item in the array instead of the entire array.
319-
const singularizedName = avoidCapture(singularName, getScopes(scope));
319+
const singularizedName = getAvailableVariableName(singularName, getScopes(scope));
320320
yield * renameVariable(variable, singularizedName, fixer);
321321

322322
// Prevent possible variable conflicts

rules/prefer-ternary.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import {isParenthesized} from '@eslint-community/eslint-utils';
2-
import avoidCapture from './utils/avoid-capture.js';
3-
import needsSemicolon from './utils/needs-semicolon.js';
4-
import isSameReference from './utils/is-same-reference.js';
5-
import getIndentString from './utils/get-indent-string.js';
6-
import {getParenthesizedText} from './utils/parentheses.js';
7-
import shouldAddParenthesesToConditionalExpressionChild from './utils/should-add-parentheses-to-conditional-expression-child.js';
2+
import {
3+
getAvailableVariableName,
4+
needsSemicolon,
5+
isSameReference,
6+
getIndentString,
7+
getParenthesizedText,
8+
shouldAddParenthesesToConditionalExpressionChild,
9+
getScopes,
10+
} from './utils/index.js';
811
import {extendFixRange} from './fix/index.js';
9-
import getScopes from './utils/get-scopes.js';
1012

1113
const messageId = 'prefer-ternary';
1214

@@ -216,7 +218,7 @@ const create = context => {
216218
let generateNewVariables = false;
217219
if (type === 'ThrowStatement') {
218220
const scopes = getScopes(scope);
219-
const errorName = avoidCapture('error', scopes, isSafeName);
221+
const errorName = getAvailableVariableName('error', scopes, isSafeName);
220222

221223
for (const scope of scopes) {
222224
if (!scopeToNamesGeneratedByFixer.has(scope)) {

rules/prevent-abbreviations.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import path from 'node:path';
22
import {isRegExp} from 'node:util/types';
33
import {defaultsDeep, upperFirst, lowerFirst} from './utils/lodash.js';
4-
import avoidCapture from './utils/avoid-capture.js';
5-
import cartesianProductSamples from './utils/cartesian-product-samples.js';
6-
import isShorthandPropertyValue from './utils/is-shorthand-property-value.js';
7-
import isShorthandImportLocal from './utils/is-shorthand-import-local.js';
8-
import getVariableIdentifiers from './utils/get-variable-identifiers.js';
4+
import {
5+
getAvailableVariableName,
6+
cartesianProductSamples,
7+
isShorthandPropertyValue,
8+
isShorthandImportLocal,
9+
getVariableIdentifiers,
10+
getScopes,
11+
} from './utils/index.js';
912
import {defaultReplacements, defaultAllowList, defaultIgnore} from './shared/abbreviations.js';
1013
import {renameVariable} from './fix/index.js';
11-
import getScopes from './utils/get-scopes.js';
1214
import {isStaticRequire} from './ast/index.js';
1315

1416
const MESSAGE_ID_REPLACE = 'replace';
@@ -439,7 +441,7 @@ const create = context => {
439441
variable.scope,
440442
];
441443
variableReplacements.samples = variableReplacements.samples.map(
442-
name => avoidCapture(name, scopes, isSafeName),
444+
name => getAvailableVariableName(name, scopes, isSafeName),
443445
);
444446

445447
const problem = {

rules/utils/avoid-capture.js renamed to rules/utils/get-available-variable-name.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Rule-specific name check function.
112112
113113
@callback isSafe
114114
@param {string} name - The generated candidate name.
115-
@param {Scope[]} scopes - The same list of scopes you pass to `avoidCapture`.
115+
@param {Scope[]} scopes - The same list of scopes you pass to `getAvailableVariableName`.
116116
@returns {boolean} - `true` if the `name` is ok.
117117
*/
118118

@@ -130,7 +130,7 @@ Useful when you want to rename a variable (or create a new variable) while being
130130
@param {isSafe} [isSafe] - Rule-specific name check function.
131131
@returns {string} - Either `name` as is, or a string like `${name}_` suffixed with underscores to make the name unique.
132132
*/
133-
export default function avoidCapture(name, scopes, isSafe = alwaysTrue) {
133+
export default function getAvailableVariableName(name, scopes, isSafe = alwaysTrue) {
134134
if (!isValidIdentifier(name)) {
135135
name += '_';
136136

rules/utils/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ export {
2121
getBooleanAncestor,
2222
} from './boolean.js';
2323

24-
export {default as avoidCapture} from './avoid-capture.js';
24+
export {default as cartesianProductSamples} from './cartesian-product-samples.js';
2525
export {default as escapeString} from './escape-string.js';
26+
export {default as getAvailableVariableName} from './get-available-variable-name.js';
2627
export {default as getCallExpressionArgumentsText} from './get-call-expression-arguments-text.js';
2728
export {default as getCallExpressionTokens} from './get-call-expression-tokens.js';
29+
export {default as getIndentString} from './get-indent-string.js';
2830
export {default as getReferences} from './get-references.js';
2931
export {default as getScopes} from './get-scopes.js';
3032
export {default as getVariableIdentifiers} from './get-variable-identifiers.js';
@@ -39,11 +41,14 @@ export {default as isOnSameLine} from './is-on-same-line.js';
3941
export {default as isSameIdentifier} from './is-same-identifier.js';
4042
export {default as isSameReference} from './is-same-reference.js';
4143
export {default as isShadowed} from './is-shadowed.js';
44+
export {default as isShorthandImportLocal} from './is-shorthand-import-local.js';
45+
export {default as isShorthandPropertyValue} from './is-shorthand-property-value.js';
4246
export {default as isValueNotUsable} from './is-value-not-usable.js';
4347
export {default as needsSemicolon} from './needs-semicolon.js';
44-
export {default as shouldAddParenthesesToMemberExpressionObject} from './should-add-parentheses-to-member-expression-object.js';
45-
export {default as shouldAddParenthesesToCallExpressionCallee} from './should-add-parentheses-to-call-expression-callee.js';
4648
export {default as shouldAddParenthesesToAwaitExpressionArgument} from './should-add-parentheses-to-await-expression-argument.js';
49+
export {default as shouldAddParenthesesToCallExpressionCallee} from './should-add-parentheses-to-call-expression-callee.js';
50+
export {default as shouldAddParenthesesToConditionalExpressionChild} from './should-add-parentheses-to-conditional-expression-child.js';
51+
export {default as shouldAddParenthesesToMemberExpressionObject} from './should-add-parentheses-to-member-expression-object.js';
4752
export {default as singular} from './singular.js';
4853
export {default as toLocation} from './to-location.js';
4954
export {default as getAncestor} from './get-ancestor.js';

0 commit comments

Comments
 (0)