variable-name
Ast Rule: assignment
variable-name
const NAMES_TO_REPLACE = new Map([
['blacklist', 'denylist'],
['whitelist', 'allowlist'],
['master', 'primary'],
['slave', 'secondary'],
['dummy', 'placeholder'],
]);
const checkString = (node, avoidString, replaceString) => {
if (node && node.value && node.value.toLowerCase().includes(avoidString)) {
const originalName = node.value;
const startIndex = node.value.toLowerCase().indexOf(avoidString);
const isUpperCase = node.value.charAt(startIndex) == node.value.charAt(startIndex).toUpperCase();
if (isUpperCase) {
replaceString = replaceString[0].toUpperCase() + replaceString.substring(1);
}
const endIndex = startIndex + avoidString.length;
const updatedName = originalName.substring(0, startIndex) + replaceString + originalName.substring(endIndex, originalName.length);
const error = buildError(node.start.line, node.start.col,
node.end.line, node.end.col,
`do not use word ${avoidString}`, "INFO", "BEST_PRACTICES");
const edit = buildEditUpdate(node.start.line, node.start.col,
node.end.line, node.end.col,
updatedName);
const fix = buildFix(`use ${updatedName} instead`, [edit]);
addError(error.addFix(fix));
}
}
function visit(node, filename, code) {
if (!node.left) {
return;
}
for (const name of NAMES_TO_REPLACE.keys()) {
const replacement = NAMES_TO_REPLACE.get(name);
if (node.left.astType === "string") {
checkString(node.left, name, replacement);
}
if (node.left.astType === "list") {
if (node.left.elements) {
node.left.elements.forEach(element => {
if (element.astType === "string") {
checkString(element, name, replacement);
}
});
}
}
}
}
const-declaration.js
Expected test result: has error
check-capital.js
Expected test result: has error
variable-assignment.js
Expected test result: has error