UNPKG

eslint-plugin-vue

Version:

Official ESLint plugin for Vue.js

77 lines (76 loc) • 3.17 kB
const require_index = require("../utils/index.js"); // lib/rules/no-deprecated-model-definition.ts /** * @author Flo Edelmann * See LICENSE file in root directory for full license. */ const allowedPropNames = /* @__PURE__ */ new Set(["modelValue", "model-value"]); const allowedEventNames = /* @__PURE__ */ new Set(["update:modelValue", "update:model-value"]); function findPropertyValue(node, key) { const property = node.properties.find((property) => property.type === "Property" && property.key.type === "Identifier" && property.key.name === key); return property?.type === "Property" && require_index.default.isStringLiteral(property.value) ? property.value : void 0; } function replaceLiteral(fixer, node, text) { return fixer.replaceTextRange([node.range[0] + 1, node.range[1] - 1], text); } var no_deprecated_model_definition_default = { meta: { type: "problem", docs: { description: "disallow deprecated `model` definition (in Vue.js 3.0.0+)", categories: ["vue3-essential"], url: "https://eslint.vuejs.org/rules/no-deprecated-model-definition.html" }, fixable: null, hasSuggestions: true, schema: [{ type: "object", additionalProperties: false, properties: { allowVue3Compat: { type: "boolean" } } }], messages: { deprecatedModel: "`model` definition is deprecated.", vue3Compat: "`model` definition is deprecated. You may use the Vue 3-compatible `modelValue`/`update:modelValue` though.", changeToModelValue: "Change to `modelValue`/`update:modelValue`.", changeToKebabModelValue: "Change to `model-value`/`update:model-value`." } }, create(context) { const shouldAllowVue3Compat = Boolean(context.options[0]?.allowVue3Compat); return require_index.default.executeOnVue(context, (obj) => { const modelProperty = require_index.default.findProperty(obj, "model"); if (!modelProperty || modelProperty.value.type !== "ObjectExpression") return; if (!shouldAllowVue3Compat) { context.report({ node: modelProperty, messageId: "deprecatedModel" }); return; } const propName = findPropertyValue(modelProperty.value, "prop"); const eventName = findPropertyValue(modelProperty.value, "event"); if (!propName || !eventName || !allowedPropNames.has(require_index.default.getStringLiteralValue(propName, true) ?? "") || !allowedEventNames.has(require_index.default.getStringLiteralValue(eventName, true) ?? "")) context.report({ node: modelProperty, messageId: "vue3Compat", suggest: propName && eventName ? [{ messageId: "changeToModelValue", *fix(fixer) { const newPropName = "modelValue"; const newEventName = "update:modelValue"; yield replaceLiteral(fixer, propName, newPropName); yield replaceLiteral(fixer, eventName, newEventName); } }, { messageId: "changeToKebabModelValue", *fix(fixer) { const newPropName = "model-value"; const newEventName = "update:model-value"; yield replaceLiteral(fixer, propName, newPropName); yield replaceLiteral(fixer, eventName, newEventName); } }] : [] }); }); } }; exports.default = no_deprecated_model_definition_default;