UNPKG

eslint-plugin-vue

Version:

Official ESLint plugin for Vue.js

185 lines (184 loc) • 6.74 kB
const require_ts_types = require("../utils/ts-utils/ts-types.js"); const require_ref_object_references = require("../utils/ref-object-references.js"); const require_index = require("../utils/index.js"); require("../utils/ts-utils/index.js"); let _eslint_community_eslint_utils = require("@eslint-community/eslint-utils"); // lib/rules/no-ref-as-operand.ts /** * Checks whether the given identifier reference has been initialized with a ref object. */ function isRefInit(data) { const init = data && data.variableDeclarator && data.variableDeclarator.init; return init ? data.defineChain.includes(init) : false; } /** * Get the callee member node from the given CallExpression */ function getNameParamNode(node) { const nameLiteralNode = node.arguments[0]; if (nameLiteralNode && require_index.default.isStringLiteral(nameLiteralNode)) { const name = require_index.default.getStringLiteralValue(nameLiteralNode); if (name != null) return { name, loc: nameLiteralNode.loc }; } return null; } /** * Get the callee member node from the given CallExpression */ function getCalleeMemberNode(node) { const callee = require_index.default.skipChainExpression(node.callee); if (callee.type === "MemberExpression") { const name = require_index.default.getStaticPropertyName(callee); if (name) return { name, member: callee }; } return null; } var no_ref_as_operand_default = { meta: { type: "suggestion", docs: { description: "disallow use of value wrapped by `ref()` (Composition API) as an operand", categories: ["vue3-essential", "vue2-essential"], url: "https://eslint.vuejs.org/rules/no-ref-as-operand.html" }, fixable: "code", schema: [], messages: { requireDotValue: "Must use `.value` to read or write the value wrapped by `{{method}}()`.", requireDotValueOfRef: "Must use `.value` to read or write the value of a ref." } }, create(context) { let refReferences; const setupContexts = /* @__PURE__ */ new Map(); /** * Collect identifier id */ function collectReferenceIds(node, referenceIds) { const variable = (0, _eslint_community_eslint_utils.findVariable)(require_index.default.getScope(context, node), node); if (!variable) return; for (const reference of variable.references) referenceIds.add(reference.identifier); } function reportIfRefWrapped(node, shouldCheckType = true) { const data = refReferences.get(node); if (isRefInit(data)) context.report({ node, messageId: "requireDotValue", data: { method: data.method }, fix(fixer) { return fixer.insertTextAfter(node, ".value"); } }); else if (shouldCheckType && require_ts_types.isRefObjectType(context, node)) context.report({ node, messageId: "requireDotValueOfRef", fix(fixer) { return fixer.insertTextAfter(node, ".value"); } }); } function reportWrappedIdentifiers(node) { const nodes = node.arguments.filter((node) => node.type === "Identifier"); for (const node of nodes) reportIfRefWrapped(node); } const programNode = context.sourceCode.ast; const callVisitor = { CallExpression(node, info) { if (!getNameParamNode(node)) return; const setupContext = setupContexts.get(info ? info.node : programNode); if (!setupContext) return; const { contextReferenceIds, emitReferenceIds } = setupContext; if (node.callee.type === "Identifier" && emitReferenceIds.has(node.callee)) reportWrappedIdentifiers(node); else { const emit = getCalleeMemberNode(node); if (emit && emit.name === "emit" && emit.member.object.type === "Identifier" && contextReferenceIds.has(emit.member.object)) reportWrappedIdentifiers(node); } } }; return require_index.default.compositingVisitors({ Program() { refReferences = require_ref_object_references.extractRefObjectReferences(context); }, "IfStatement>Identifier"(node) { reportIfRefWrapped(node); }, "SwitchStatement>Identifier"(node) { reportIfRefWrapped(node); }, "UnaryExpression>Identifier"(node) { reportIfRefWrapped(node); }, "UpdateExpression>Identifier"(node) { reportIfRefWrapped(node); }, "BinaryExpression>Identifier"(node) { reportIfRefWrapped(node); }, "AssignmentExpression>Identifier"(node) { if (node.parent.operator === "=") { if (node.parent.left !== node) return; reportIfRefWrapped(node, false); return; } reportIfRefWrapped(node); }, "LogicalExpression>Identifier"(node) { if (node.parent.left !== node) return; const data = refReferences.get(node); if (!data || !data.variableDeclaration || data.variableDeclaration.kind !== "const") return; reportIfRefWrapped(node); }, "ConditionalExpression>Identifier"(node) { if (node.parent.test !== node) return; reportIfRefWrapped(node); }, ":not(TaggedTemplateExpression)>TemplateLiteral>Identifier"(node) { reportIfRefWrapped(node); }, "MemberExpression>Identifier"(node) { if (node.parent.object !== node) return; const name = require_index.default.getStaticPropertyName(node.parent); if (name === "value" || name === "effect" || name == null) return; reportIfRefWrapped(node); } }, require_index.default.defineScriptSetupVisitor(context, { onDefineEmitsEnter(node) { if (!node.parent || node.parent.type !== "VariableDeclarator" || node.parent.init !== node) return; const emitParam = node.parent.id; if (emitParam.type !== "Identifier") return; const emitReferenceIds = /* @__PURE__ */ new Set(); collectReferenceIds(emitParam, emitReferenceIds); setupContexts.set(programNode, { contextReferenceIds: /* @__PURE__ */ new Set(), emitReferenceIds }); }, ...callVisitor }), require_index.default.defineVueVisitor(context, { onSetupFunctionEnter(node, { node: vueNode }) { const contextParam = require_index.default.skipDefaultParamValue(node.params[1]); if (!contextParam || contextParam.type === "RestElement" || contextParam.type === "ArrayPattern") return; const contextReferenceIds = /* @__PURE__ */ new Set(); const emitReferenceIds = /* @__PURE__ */ new Set(); if (contextParam.type === "ObjectPattern") { const emitProperty = require_index.default.findAssignmentProperty(contextParam, "emit"); if (!emitProperty || emitProperty.value.type !== "Identifier") return; collectReferenceIds(emitProperty.value, emitReferenceIds); } else collectReferenceIds(contextParam, contextReferenceIds); setupContexts.set(vueNode, { contextReferenceIds, emitReferenceIds }); }, ...callVisitor, onVueObjectExit(node) { setupContexts.delete(node); } })); } }; exports.default = no_ref_as_operand_default;