Skip to content

Commit bf0a8e5

Browse files
committed
add types to vue/no-potential-component-option-typo
1 parent 14d9938 commit bf0a8e5

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

lib/rules/no-potential-component-option-typo.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* @fileoverview detect if there is a potential typo in your component property
33
* @author IWANABETHATGUY
44
*/
5-
// @ts-nocheck
65
'use strict'
76

87
const utils = require('../utils')
@@ -53,46 +52,60 @@ module.exports = {
5352
create(context) {
5453
const option = context.options[0] || {}
5554
const custom = option.custom || []
55+
/** @type {('all' | 'vue' | 'vue-router' | 'nuxt')[]} */
5656
const presets = option.presets || ['vue']
5757
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+
}
6772
}
68-
const candidateOptionSet = new Set([...candidateOptions, ...custom])
6973
const candidateOptionList = [...candidateOptionSet]
7074
if (!candidateOptionList.length) {
7175
return {}
7276
}
7377
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+
7793
if (!componentInstanceOptions.length) {
78-
return {}
94+
return
7995
}
8096
componentInstanceOptions.forEach((option) => {
8197
const id = option.key
82-
const name = id.name
98+
const name = option.name
8399
if (candidateOptionSet.has(name)) {
84100
return
85101
}
86102
const potentialTypoList = candidateOptionList
87103
.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)
91105
.sort((a, b) => a.distance - b.distance)
92106
if (potentialTypoList.length) {
93107
context.report({
94108
node: id,
95-
loc: id.loc,
96109
message: `'{{name}}' may be a typo, which is similar to option [{{option}}].`,
97110
data: {
98111
name,

0 commit comments

Comments
 (0)