|
| 1 | +/** |
| 2 | + * @fileoverview detect if there is a potential typo in your component property |
| 3 | + * @author IWANABETHATGUY |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | +const vueComponentOptions = require('../utils/vue-component-options.json') |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +// Rule Definition |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +module.exports = { |
| 14 | + meta: { |
| 15 | + type: 'suggestion', |
| 16 | + docs: { |
| 17 | + description: 'disallow a potential typo in your component property', |
| 18 | + categories: undefined, |
| 19 | + recommended: false, |
| 20 | + url: 'https://eslint.vuejs.org/rules/no-potential-component-option-typo.html' |
| 21 | + }, |
| 22 | + fixable: null, |
| 23 | + schema: [ |
| 24 | + { |
| 25 | + type: 'object', |
| 26 | + properties: { |
| 27 | + presets: { |
| 28 | + type: 'array', |
| 29 | + items: { |
| 30 | + type: 'string', |
| 31 | + enum: ['all', 'vue', 'vue-router', 'nuxt'] |
| 32 | + }, |
| 33 | + uniqueItems: true, |
| 34 | + minItems: 0 |
| 35 | + }, |
| 36 | + custom: { |
| 37 | + type: 'array', |
| 38 | + minItems: 0, |
| 39 | + items: { type: 'string' }, |
| 40 | + uniqueItems: true |
| 41 | + }, |
| 42 | + threshold: { |
| 43 | + type: 'number', |
| 44 | + 'minimum': 1 |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + ] |
| 49 | + }, |
| 50 | + |
| 51 | + create: function (context) { |
| 52 | + const option = context.options[0] || {} |
| 53 | + const custom = option['custom'] || [] |
| 54 | + const presets = option['presets'] || ['vue'] |
| 55 | + const threshold = option['threshold'] || 1 |
| 56 | + let candidateOptions |
| 57 | + if (presets.includes('all')) { |
| 58 | + candidateOptions = Object.keys(vueComponentOptions).reduce((pre, cur) => { |
| 59 | + return [...pre, ...vueComponentOptions[cur]] |
| 60 | + }, []) |
| 61 | + } else { |
| 62 | + candidateOptions = presets.reduce((pre, cur) => { |
| 63 | + return [...pre, ...vueComponentOptions[cur]] |
| 64 | + }, []) |
| 65 | + } |
| 66 | + const candidateOptionSet = new Set([...candidateOptions, ...custom]) |
| 67 | + const candidateOptionList = [...candidateOptionSet] |
| 68 | + if (!candidateOptionList.length) { |
| 69 | + return {} |
| 70 | + } |
| 71 | + return utils.executeOnVue(context, obj => { |
| 72 | + const componentInstanceOptions = obj.properties.filter( |
| 73 | + p => p.type === 'Property' && p.key.type === 'Identifier' |
| 74 | + ) |
| 75 | + if (!componentInstanceOptions.length) { |
| 76 | + return {} |
| 77 | + } |
| 78 | + componentInstanceOptions.forEach(option => { |
| 79 | + const id = option.key |
| 80 | + const name = id.name |
| 81 | + if (candidateOptionSet.has(name)) { |
| 82 | + return |
| 83 | + } |
| 84 | + const potentialTypoList = candidateOptionList |
| 85 | + .map(o => ({ option: o, distance: utils.editDistance(o, name) })) |
| 86 | + .filter(({ distance, option }) => distance <= threshold && distance > 0) |
| 87 | + .sort((a, b) => a.distance - b.distance) |
| 88 | + if (potentialTypoList.length) { |
| 89 | + context.report({ |
| 90 | + node: id, |
| 91 | + loc: id.loc, |
| 92 | + message: `'{{name}}' may be a typo, which is similar to option [{{option}}].`, |
| 93 | + data: { |
| 94 | + name, |
| 95 | + option: potentialTypoList.map(({ option }) => option).join(',') |
| 96 | + }, |
| 97 | + suggest: potentialTypoList.map(({ option }) => ({ |
| 98 | + desc: `Replace property '${name}' to '${option}'`, |
| 99 | + fix (fixer) { |
| 100 | + return fixer.replaceText(id, option) |
| 101 | + } |
| 102 | + })) |
| 103 | + }) |
| 104 | + } |
| 105 | + }) |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments