|
2 | 2 | * @fileoverview detect if there is a potential typo in your component property
|
3 | 3 | * @author IWANABETHATGUY
|
4 | 4 | */
|
5 |
| -// @ts-nocheck |
6 | 5 | 'use strict'
|
7 | 6 |
|
8 | 7 | const utils = require('../utils')
|
@@ -53,46 +52,60 @@ module.exports = {
|
53 | 52 | create(context) {
|
54 | 53 | const option = context.options[0] || {}
|
55 | 54 | const custom = option.custom || []
|
| 55 | + /** @type {('all' | 'vue' | 'vue-router' | 'nuxt')[]} */ |
56 | 56 | const presets = option.presets || ['vue']
|
57 | 57 | const threshold = option.threshold || 1
|
58 |
| - let candidateOptions |
59 |
| - if (presets.includes('all')) { |
60 |
| - candidateOptions = Object.keys(vueComponentOptions).reduce((pre, cur) => { |
61 |
| - return [...pre, ...vueComponentOptions[cur]] |
62 |
| - }, []) |
63 |
| - } else { |
64 |
| - candidateOptions = presets.reduce((pre, cur) => { |
65 |
| - return [...pre, ...vueComponentOptions[cur]] |
66 |
| - }, []) |
| 58 | + /** @type {Set<string>} */ |
| 59 | + const candidateOptionSet = new Set(custom) |
| 60 | + for (const preset of presets) { |
| 61 | + if (preset === 'all') { |
| 62 | + for (const opts of Object.values(vueComponentOptions)) { |
| 63 | + for (const opt of opts) { |
| 64 | + candidateOptionSet.add(opt) |
| 65 | + } |
| 66 | + } |
| 67 | + } else { |
| 68 | + for (const opt of vueComponentOptions[preset]) { |
| 69 | + candidateOptionSet.add(opt) |
| 70 | + } |
| 71 | + } |
67 | 72 | }
|
68 |
| - const candidateOptionSet = new Set([...candidateOptions, ...custom]) |
69 | 73 | const candidateOptionList = [...candidateOptionSet]
|
70 | 74 | if (!candidateOptionList.length) {
|
71 | 75 | return {}
|
72 | 76 | }
|
73 | 77 | return utils.executeOnVue(context, (obj) => {
|
74 |
| - const componentInstanceOptions = obj.properties.filter( |
75 |
| - (p) => p.type === 'Property' && p.key.type === 'Identifier' |
76 |
| - ) |
| 78 | + const componentInstanceOptions = obj.properties |
| 79 | + .map((p) => { |
| 80 | + if (p.type === 'Property') { |
| 81 | + const name = utils.getStaticPropertyName(p) |
| 82 | + if (name != null) { |
| 83 | + return { |
| 84 | + name, |
| 85 | + key: p.key |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return null |
| 90 | + }) |
| 91 | + .filter(utils.isDef) |
| 92 | + |
77 | 93 | if (!componentInstanceOptions.length) {
|
78 |
| - return {} |
| 94 | + return |
79 | 95 | }
|
80 | 96 | componentInstanceOptions.forEach((option) => {
|
81 | 97 | const id = option.key
|
82 |
| - const name = id.name |
| 98 | + const name = option.name |
83 | 99 | if (candidateOptionSet.has(name)) {
|
84 | 100 | return
|
85 | 101 | }
|
86 | 102 | const potentialTypoList = candidateOptionList
|
87 | 103 | .map((o) => ({ option: o, distance: utils.editDistance(o, name) }))
|
88 |
| - .filter( |
89 |
| - ({ distance, option }) => distance <= threshold && distance > 0 |
90 |
| - ) |
| 104 | + .filter(({ distance }) => distance <= threshold && distance > 0) |
91 | 105 | .sort((a, b) => a.distance - b.distance)
|
92 | 106 | if (potentialTypoList.length) {
|
93 | 107 | context.report({
|
94 | 108 | node: id,
|
95 |
| - loc: id.loc, |
96 | 109 | message: `'{{name}}' may be a typo, which is similar to option [{{option}}].`,
|
97 | 110 | data: {
|
98 | 111 | name,
|
|
0 commit comments