Skip to content

Commit

Permalink
Refactor message data handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Pearce-Ropion committed Mar 27, 2024
1 parent 418a092 commit a91ea4f
Showing 1 changed file with 70 additions and 53 deletions.
123 changes: 70 additions & 53 deletions lib/rules/no-unused-vars.js
Expand Up @@ -15,6 +15,11 @@ const astUtils = require("./utils/ast-utils");
// Typedefs
//------------------------------------------------------------------------------

/**
* A colloquial name for type of variables that this rule handles
* @typedef {'array-destructure'|'catch-clause'|'parameter'|'variable'} VariableType
*/

/**
* Bag of data used for formatting the `unusedVar` lint message.
* @typedef {Object} UnusedVarMessageData
Expand Down Expand Up @@ -142,6 +147,51 @@ module.exports = {
}
}

/**
* Gets a given variable's description and configured ignore pattern
* based on the provided variableType
* @param {VariableType} variableType a colloquial name for type of variables that this rule handles
* @throws {Error} (Unreachable)
* @returns {[string | undefined, string | undefined]} the given variable's description and
* ignore pattern
*/
function getVariableDescription(variableType) {
let pattern;
let variableDescription;

switch (variableType) {

case "array-destructure":
pattern = config.destructuredArrayIgnorePattern;
variableDescription = "elements of array destructuring";
break;

case "catch-clause":
pattern = config.caughtErrorsIgnorePattern;
variableDescription = "args";
break;

case "parameter":
pattern = config.argsIgnorePattern;
variableDescription = "args";
break;

case "variable":
pattern = config.varsIgnorePattern;
variableDescription = "vars";
break;

default:
throw new Error(`Unexpected variable type: ${variableType}`);
}

if (pattern) {
pattern = pattern.toString();
}

return [variableDescription, pattern];
}

/**
* Generates the message data about the variable being defined and unused,
* including the ignore pattern if configured.
Expand All @@ -150,44 +200,41 @@ module.exports = {
*/
function getDefinedMessageData(unusedVar) {
const def = unusedVar.defs && unusedVar.defs[0];
let additional = "";
let additionalMessageData = "";

if (def) {
let type;
let pattern;
let variableDescription;

switch (def.type) {
case "CatchClause":
if (config.caughtErrorsIgnorePattern) {
type = "args";
pattern = config.caughtErrorsIgnorePattern;
[variableDescription, pattern] = getVariableDescription("catch-clause");
}
break;

case "Parameter":
if (config.argsIgnorePattern) {
type = "args";
pattern = config.argsIgnorePattern;
[variableDescription, pattern] = getVariableDescription("parameter");
}
break;

default:
if (config.varsIgnorePattern) {
type = "vars";
pattern = config.varsIgnorePattern;
[variableDescription, pattern] = getVariableDescription("variable");
}
break;
}

if (type) {
additional = `. Allowed unused ${type} must match ${pattern.toString()}`;
if (pattern && variableDescription) {
additionalMessageData = `. Allowed unused ${variableDescription} must match ${pattern}`;
}
}

return {
varName: unusedVar.name,
action: "defined",
additional
additional: additionalMessageData
};
}

Expand All @@ -199,75 +246,45 @@ module.exports = {
*/
function getAssignedMessageData(unusedVar) {
const def = unusedVar.defs && unusedVar.defs[0];
let additional = "";
let additionalMessageData = "";

if (def) {
let type;
let pattern;
let variableDescription;

if (def.name.parent.type === "ArrayPattern" && config.destructuredArrayIgnorePattern) {
type = "elements of array destructuring";
pattern = config.destructuredArrayIgnorePattern;
[variableDescription, pattern] = getVariableDescription("array-destructure");
} else if (config.varsIgnorePattern) {
type = "vars";
pattern = config.varsIgnorePattern;
[variableDescription, pattern] = getVariableDescription("variable");
}

if (type) {
additional = `. Allowed unused ${type} must match ${pattern.toString()}`;
if (pattern && variableDescription) {
additionalMessageData = `. Allowed unused ${variableDescription} must match ${pattern}`;
}
}

return {
varName: unusedVar.name,
action: "assigned a value",
additional
additional: additionalMessageData
};
}

/**
* Generate the warning message about a variable being used even though
* it is marked as being ignored.
* @param {Variable} variable eslint-scope variable object
* @param {"catch-clause" | "args" | "array-destructure" | "vars"} variableType
* the type of the unused variable
* @throws {Error} (Unreachable)
* @param {VariableType} variableType a colloquial name for type of variables that this rule handles
* @returns {UsedIgnoredVarMessageData} The message data to be used with
* this used ignored variable.
*/
function getUsedIgnoredMessageData(variable, variableType) {
let pattern;
let variableDescription;

switch (variableType) {
case "catch-clause":
pattern = config.caughtErrorsIgnorePattern;
variableDescription = "args";
break;

case "args":
pattern = config.argsIgnorePattern;
variableDescription = "args";
break;

case "array-destructure":
pattern = config.destructuredArrayIgnorePattern;
variableDescription = "elements of array destructuring";
break;

case "vars":
pattern = config.varsIgnorePattern;
variableDescription = "vars";
break;

default:
throw new Error(`Unexpected variable type: ${variableType}`);
}
const [variableDescription, pattern] = getVariableDescription(variableType);

let additionalMessageData = "";

if (pattern && variableDescription) {
additionalMessageData = `. Used ${variableDescription} must not match ${pattern.toString()}`;
additionalMessageData = `. Used ${variableDescription} must not match ${pattern}`;
}

return {
Expand Down Expand Up @@ -773,7 +790,7 @@ module.exports = {
context.report({
node: def.name,
messageId: "usedIgnoredVar",
data: getUsedIgnoredMessageData(variable, "args")
data: getUsedIgnoredMessageData(variable, "parameter")
});
}

Expand All @@ -792,7 +809,7 @@ module.exports = {
context.report({
node: def.name,
messageId: "usedIgnoredVar",
data: getUsedIgnoredMessageData(variable, "vars")
data: getUsedIgnoredMessageData(variable, "variable")
});
}

Expand Down

0 comments on commit a91ea4f

Please sign in to comment.