UNPKG

eslint-plugin-vue

Version:

Official ESLint plugin for Vue.js

100 lines (99 loc) • 3.38 kB
const require_index = require("../utils/index.js"); // lib/rules/valid-v-bind-sync.ts /** * @fileoverview enforce valid `.sync` modifier on `v-bind` directives * @author Yosuke Ota */ /** * Check whether the given node is valid or not. */ function isValidElement(node) { return require_index.default.isCustomComponent(node); } /** * Check whether the given node is a MemberExpression containing an optional chaining. * e.g. * - `a?.b` * - `a?.b.c` */ function isOptionalChainingMemberExpression(node) { return node.type === "ChainExpression" && node.expression.type === "MemberExpression"; } /** * Check whether the given node can be LHS (left-hand side). */ function isLhs(node) { return node.type === "Identifier" || node.type === "MemberExpression"; } /** * Check whether the given node is a MemberExpression of a possibly null object. * e.g. * - `(a?.b).c` * - `(null).foo` */ function isMaybeNullObjectMemberExpression(node) { if (node.type !== "MemberExpression") return false; switch (node.object.type) { case "ChainExpression": return true; case "Literal": return node.object.value === null && !node.object.bigint; case "MemberExpression": return isMaybeNullObjectMemberExpression(node.object); default: return false; } } var valid_v_bind_sync_default = { meta: { type: "problem", docs: { description: "enforce valid `.sync` modifier on `v-bind` directives", categories: ["vue2-essential"], url: "https://eslint.vuejs.org/rules/valid-v-bind-sync.html" }, fixable: null, deprecated: true, schema: [], messages: { unexpectedInvalidElement: "'.sync' modifiers aren't supported on <{{name}}> non Vue-components.", unexpectedOptionalChaining: "Optional chaining cannot appear in 'v-bind' with '.sync' modifiers.", unexpectedNonLhsExpression: "'.sync' modifiers require the attribute value which is valid as LHS.", unexpectedNullObject: "'.sync' modifier has potential null object property access.", unexpectedUpdateIterationVariable: "'.sync' modifiers cannot update the iteration variable '{{varName}}' itself." } }, create(context) { return require_index.default.defineTemplateBodyVisitor(context, { "VAttribute[directive=true][key.name.name='bind']"(node) { if (!node.key.modifiers.map((mod) => mod.name).includes("sync")) return; const element = node.parent.parent; const name = element.name; if (!isValidElement(element)) context.report({ node, messageId: "unexpectedInvalidElement", data: { name } }); if (!node.value) return; const expression = node.value.expression; if (!expression) return; if (isOptionalChainingMemberExpression(expression)) context.report({ node: expression, messageId: "unexpectedOptionalChaining" }); else if (!isLhs(expression)) context.report({ node: expression, messageId: "unexpectedNonLhsExpression" }); else if (isMaybeNullObjectMemberExpression(expression)) context.report({ node: expression, messageId: "unexpectedNullObject" }); for (const reference of node.value.references) { const id = reference.id; if (id.parent.type !== "VExpressionContainer") continue; if (reference.variable) context.report({ node: expression, messageId: "unexpectedUpdateIterationVariable", data: { varName: id.name } }); } } }); } }; exports.default = valid_v_bind_sync_default;