eslint-plugin-vue
Version:
Official ESLint plugin for Vue.js
133 lines (132 loc) • 4.53 kB
JavaScript
const require_index = require("../utils/index.js");
// lib/rules/no-unused-refs.ts
/**
* @fileoverview Disallow unused refs.
* @author Yosuke Ota
*/
/**
* Extract names from references objects.
*/
function getReferences(references) {
return references.filter((ref) => ref.variable == null).map((ref) => ref.id);
}
var no_unused_refs_default = {
meta: {
type: "suggestion",
docs: {
description: "disallow unused refs",
categories: void 0,
url: "https://eslint.vuejs.org/rules/no-unused-refs.html"
},
fixable: null,
schema: [],
messages: { unused: "'{{name}}' is defined as ref, but never used." }
},
create(context) {
const usedRefs = /* @__PURE__ */ new Set();
const defineRefs = [];
let hasUnknown = false;
/**
* Report all unused refs.
*/
function reportUnusedRefs() {
for (const defineRef of defineRefs) {
if (usedRefs.has(defineRef.value)) continue;
context.report({
node: defineRef,
messageId: "unused",
data: { name: defineRef.value }
});
}
}
/**
* Extract the use ref names for ObjectPattern.
*/
function extractUsedForObjectPattern(node) {
for (const prop of node.properties) if (prop.type === "Property") {
const name = require_index.default.getStaticPropertyName(prop);
if (name) usedRefs.add(name);
else {
hasUnknown = true;
return;
}
} else {
hasUnknown = true;
return;
}
}
/**
* Extract the use ref names.
*/
function extractUsedForPattern(refsNode) {
let node = refsNode;
while (node.parent.type === "ChainExpression") node = node.parent;
const parent = node.parent;
switch (parent.type) {
case "AssignmentExpression":
if (parent.right === node) {
if (parent.left.type === "ObjectPattern") extractUsedForObjectPattern(parent.left);
else if (parent.left.type === "Identifier") hasUnknown = true;
}
break;
case "VariableDeclarator":
if (parent.init === node) {
if (parent.id.type === "ObjectPattern") extractUsedForObjectPattern(parent.id);
else if (parent.id.type === "Identifier") hasUnknown = true;
}
break;
case "MemberExpression":
if (parent.object === node) {
const name = require_index.default.getStaticPropertyName(parent);
if (name) usedRefs.add(name);
else hasUnknown = true;
}
break;
case "CallExpression":
if (parent.arguments.indexOf(node) !== -1) hasUnknown = true;
break;
case "ForInStatement":
case "ReturnStatement": hasUnknown = true;
}
}
return require_index.default.defineTemplateBodyVisitor(context, {
VExpressionContainer(node) {
if (hasUnknown) return;
for (const id of getReferences(node.references)) {
if (id.name !== "$refs") continue;
extractUsedForPattern(id);
}
},
"VAttribute[directive=false]"(node) {
if (hasUnknown) return;
if (node.key.name === "ref" && node.value != null) defineRefs.push(node.value);
},
"VElement[parent.type!='VElement']:exit"() {
if (hasUnknown) return;
reportUnusedRefs();
}
}, require_index.default.compositingVisitors(require_index.default.isScriptSetup(context) ? { Program() {
const globalScope = context.sourceCode.scopeManager.globalScope;
if (!globalScope) return;
for (const variable of globalScope.variables) if (variable.defs.length > 0) usedRefs.add(variable.name);
const moduleScope = globalScope.childScopes.find((scope) => scope.type === "module");
if (!moduleScope) return;
for (const variable of moduleScope.variables) if (variable.defs.length > 0) usedRefs.add(variable.name);
} } : {}, require_index.default.defineVueVisitor(context, { onVueObjectEnter(node) {
const setupProperties = require_index.default.iterateProperties(node, /* @__PURE__ */ new Set(["setup"]));
for (const prop of setupProperties) usedRefs.add(prop.name);
} }), {
Identifier(id) {
if (hasUnknown || id.name !== "$refs") return;
extractUsedForPattern(id.parent.type === "MemberExpression" && id.parent.property === id ? id.parent : id);
},
CallExpression(callExpression) {
const firstArgument = callExpression.arguments[0];
if (!firstArgument || !require_index.default.isStringLiteral(firstArgument) || callExpression.callee.type !== "Identifier" || callExpression.callee.name !== "useTemplateRef") return;
const name = require_index.default.getStringLiteralValue(firstArgument);
if (name !== null) usedRefs.add(name);
}
}));
}
};
exports.default = no_unused_refs_default;