eslint-plugin-vue
Version:
Official ESLint plugin for Vue.js
98 lines (97 loc) • 3.18 kB
JavaScript
const require_index = require("../utils/index.js");
// lib/rules/no-unused-vars.ts
/**
* @fileoverview disallow unused variable definitions of v-for directives or scope attributes.
* @author 薛定谔的猫<hh_2013@foxmail.com>
*/
/**
* Groups variables by directive kind.
*/
function groupingVariables(node) {
const result = {};
for (const variable of node.variables) {
const kind = variable.kind;
result[kind] ||= [];
result[kind].push(variable);
}
return result;
}
/**
* Checks if the given variable was defined by destructuring.
*/
function isDestructuringVar(variable) {
let parent = variable.id.parent;
while (parent) {
if (parent.type === "VForExpression" || parent.type === "VSlotScopeExpression") return false;
if (parent.type === "Property" || parent.type === "ArrayPattern") return true;
parent = parent.parent;
}
return false;
}
/**
* Checks if the given variable name is used as a component tag in the element's descendants.
*/
function isUsedAsComponentTag(variableName, element) {
for (const child of element.children) {
if (child.type !== "VElement") continue;
if (child.rawName === variableName || isUsedAsComponentTag(variableName, child)) return true;
}
return false;
}
var no_unused_vars_default = {
meta: {
type: "suggestion",
docs: {
description: "disallow unused variable definitions of v-for directives or scope attributes",
categories: ["vue3-essential", "vue2-essential"],
url: "https://eslint.vuejs.org/rules/no-unused-vars.html"
},
fixable: null,
hasSuggestions: true,
schema: [{
type: "object",
properties: { ignorePattern: { type: "string" } },
additionalProperties: false
}],
messages: {
unusedVariable: "'{{name}}' is defined but never used.",
replaceWithUnderscore: "Replace `{{name}}` with `_{{name}}` to ignore the unused variable."
}
},
create(context) {
const ignorePattern = (context.options[0] || {}).ignorePattern;
const ignoreRegEx = ignorePattern ? new RegExp(ignorePattern, "u") : null;
/**
* Checks the variables of a single directive kind.
*/
function checkVariableGroup(variables, node) {
let hasAfterUsed = false;
for (let i = variables.length - 1; i >= 0; i--) {
const variable = variables[i];
if (variable.references.length > 0) {
hasAfterUsed = true;
continue;
}
if (ignoreRegEx != null && ignoreRegEx.test(variable.id.name) || hasAfterUsed && !isDestructuringVar(variable) || isUsedAsComponentTag(variable.id.name, node)) continue;
context.report({
node: variable.id,
loc: variable.id.loc,
messageId: "unusedVariable",
data: { name: variable.id.name },
suggest: ignorePattern === "^_" ? [{
messageId: "replaceWithUnderscore",
data: { name: variable.id.name },
fix(fixer) {
return fixer.replaceText(variable.id, `_${variable.id.name}`);
}
}] : []
});
}
}
return require_index.default.defineTemplateBodyVisitor(context, { VElement(node) {
const vars = groupingVariables(node);
for (const variables of Object.values(vars)) if (variables) checkVariableGroup(variables, node);
} });
}
};
exports.default = no_unused_vars_default;