Skip to content

Commit

Permalink
use existing rename-variable
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Sep 9, 2024
1 parent f83e53d commit 00b2c0f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 224 deletions.
16 changes: 12 additions & 4 deletions rules/better-boolean-variable-names.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const renameVariable = require('./utils/rename-variable.js');
const renameVariable = require('./fix/rename-variable.js');
const {isBooleanExpression, isBooleanTypeAnnotation} = require('./utils/is-boolean.js');

const MESSAGE_ID_ERROR = 'better-boolean-variable-names/error';
Expand Down Expand Up @@ -96,7 +96,7 @@ const create = context => {
/**
*
* @param {import('eslint').Rule.RuleContext} context
* @param {import('estree').Node} node
* @param {import('estree').Identifier} node
* @param {string} variableName
*/
function report(context, node, variableName) {
Expand All @@ -120,8 +120,16 @@ const create = context => {
value: variableName,
replacement: expectedVariableName,
},
* fix(fixer) {
yield * renameVariable(context.sourceCode, context.sourceCode.getScope(node), fixer, node, expectedVariableName);
fix(fixer) {
const scope = context.sourceCode.getScope(node);

const variable = scope.variables.find(variable => variable.name === node.name);

if (!variable) {
return;
}

return renameVariable(variable, expectedVariableName, fixer);
},
};
}),
Expand Down
46 changes: 45 additions & 1 deletion rules/fix/rename-variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,52 @@
const getVariableIdentifiers = require('../utils/get-variable-identifiers.js');
const replaceReferenceIdentifier = require('./replace-reference-identifier.js');

/**
Incrementing a variable
@param {string} variableName
@returns {string}
*/
function incrementVariableName(variableName) {
const match = variableName.match(/(\d+)$/);

if (match) {
const number = Number.parseInt(match[1], 10);
return variableName.replace(/\d+$/, number + 1);
}

return `${variableName}1`;
}

/**
Get a variable name that does not conflict with existing variables in the scope.
@param {import('eslint').Scope.Scope} scope
@param {string} variableName
@param {Array<import('estree').Identifier>} ignores
@returns {string}
*/
function getNonConflictingVariableName(scope, variableName, ignores) {
for (const variable of scope.variables) {
// Skip ignored variables
if (variable.defs.some(definition => ignores.includes(definition.node))) {
continue;
}

if (variable.name === variableName) {
return getNonConflictingVariableName(scope, incrementVariableName(variableName), ignores);
}
}

return variableName;
}

const renameVariable = (variable, name, fixer) =>
getVariableIdentifiers(variable)
.map(identifier => replaceReferenceIdentifier(identifier, name, fixer));
.map(identifier => {
const nonConflictingName = getNonConflictingVariableName(variable.scope, name, [identifier]);

return replaceReferenceIdentifier(identifier, nonConflictingName, fixer);
});

module.exports = renameVariable;
219 changes: 0 additions & 219 deletions rules/utils/rename-variable.js

This file was deleted.

0 comments on commit 00b2c0f

Please sign in to comment.