UNPKG

eslint-plugin-vue

Version:

Official ESLint plugin for Vue.js

102 lines (101 loc) • 3.18 kB
const require_index = require("../utils/index.js"); const require_vue_component_options = require("../utils/vue-component-options.json.js"); // lib/rules/no-potential-component-option-typo.ts /** * @fileoverview detect if there is a potential typo in your component property * @author IWANABETHATGUY */ var no_potential_component_option_typo_default = { meta: { type: "suggestion", docs: { description: "disallow a potential typo in your component property", categories: void 0, url: "https://eslint.vuejs.org/rules/no-potential-component-option-typo.html" }, fixable: null, hasSuggestions: true, schema: [{ type: "object", properties: { presets: { type: "array", items: { type: "string", enum: [ "all", "vue", "vue-router", "nuxt" ] }, uniqueItems: true, minItems: 0 }, custom: { type: "array", minItems: 0, items: { type: "string" }, uniqueItems: true }, threshold: { type: "number", minimum: 1 } }, additionalProperties: false }], messages: { potentialTypo: `'{{name}}' may be a typo, which is similar to option [{{option}}].` } }, create(context) { const option = context.options[0] || {}; const custom = option.custom || []; const presets = option.presets || ["vue"]; const threshold = option.threshold || 1; const candidateOptionSet = new Set(custom); for (const preset of presets) if (preset === "all") for (const opts of Object.values(require_vue_component_options.default)) for (const opt of opts) candidateOptionSet.add(opt); else { const presetOptions = require_vue_component_options.default[preset]; for (const opt of presetOptions) candidateOptionSet.add(opt); } const candidateOptionList = [...candidateOptionSet]; if (candidateOptionList.length === 0) return {}; return require_index.default.executeOnVue(context, (obj) => { const componentInstanceOptions = obj.properties.map((p) => { if (p.type === "Property") { const name = require_index.default.getStaticPropertyName(p); if (name != null) return { name, key: p.key }; } return null; }).filter(require_index.default.isDef); if (componentInstanceOptions.length === 0) return; for (const option of componentInstanceOptions) { const id = option.key; const name = option.name; if (candidateOptionSet.has(name)) continue; const potentialTypoList = candidateOptionList.map((o) => ({ option: o, distance: require_index.default.editDistance(o, name) })).filter(({ distance }) => distance <= threshold && distance > 0).sort((a, b) => a.distance - b.distance); if (potentialTypoList.length > 0) context.report({ node: id, messageId: "potentialTypo", data: { name, option: potentialTypoList.map(({ option }) => option).join(",") }, suggest: potentialTypoList.map(({ option }) => ({ desc: `Replace property '${name}' to '${option}'`, fix(fixer) { return fixer.replaceText(id, option); } })) }); } }); } }; exports.default = no_potential_component_option_typo_default;