UNPKG

eslint-plugin-vue

Version:

Official ESLint plugin for Vue.js

91 lines (90 loc) • 3.35 kB
const require_index = require("../utils/index.js"); const require_ref_object_references = require("../utils/ref-object-references.js"); // lib/rules/no-ref-object-reactivity-loss.ts /** * @author Yosuke Ota <https://github.com/ota-meshi> * See LICENSE file in root directory for full license. */ /** * Checks whether writing assigns a value to the given pattern. */ function isUpdate(node) { const parent = node.parent; return parent.type === "UpdateExpression" && parent.argument === node || parent.type === "AssignmentExpression" && parent.left === node || (parent.type === "Property" && parent.value === node || parent.type === "ArrayPattern" || parent.type === "ObjectPattern" && parent.properties.includes(node) || parent.type === "AssignmentPattern" && parent.left === node || parent.type === "RestElement" || parent.type === "MemberExpression" && parent.object === node) && isUpdate(parent); } /** * Checks whether the given function is immediately invoked (IIFE). * The body of such a function runs synchronously within the enclosing scope, * so getting a value there loses reactivity in the same way. * `async` functions and generators are excluded because their bodies are not * guaranteed to run synchronously to completion. */ function isImmediatelyInvokedFunction(node) { if (node.type === "FunctionDeclaration" || node.async || node.type === "FunctionExpression" && node.generator) return false; const parent = node.parent; return parent.type === "CallExpression" && parent.callee === node; } var no_ref_object_reactivity_loss_default = { meta: { type: "problem", docs: { description: "disallow usages of ref objects that can lead to loss of reactivity", categories: void 0, url: "https://eslint.vuejs.org/rules/no-ref-object-reactivity-loss.html" }, fixable: null, schema: [], messages: { getValueInSameScope: "Getting a value from the ref object in the same scope will cause the value to lose reactivity." } }, create(context) { let scopeStack = { upper: null, node: context.sourceCode.ast }; const scopes = /* @__PURE__ */ new Map(); const refObjectReferences = require_ref_object_references.extractRefObjectReferences(context); /** * Verify the given ref object value. `refObj = ref(); refObj.value;` */ function verifyRefObjectValue(node) { const ref = refObjectReferences.get(node); if (!ref || scopes.get(ref.define) !== scopeStack) return; context.report({ node, messageId: "getValueInSameScope" }); } return { ":function"(node) { if (isImmediatelyInvokedFunction(node)) return; scopeStack = { upper: scopeStack, node }; }, ":function:exit"(node) { if (isImmediatelyInvokedFunction(node)) return; scopeStack = scopeStack.upper || scopeStack; }, CallExpression(node) { scopes.set(node, scopeStack); }, /** * Check for `refObj.value`. */ "MemberExpression:exit"(node) { if (isUpdate(node)) return; if (require_index.default.getStaticPropertyName(node) !== "value") return; verifyRefObjectValue(node.object); }, /** * Check for `{value} = refObj`. */ "ObjectPattern:exit"(node) { if (!require_index.default.findAssignmentProperty(node, "value")) return; verifyRefObjectValue(node); } }; } }; exports.default = no_ref_object_reactivity_loss_default;