Skip to content

Commit 5788883

Browse files
authored
Fix unable to autofix event name with update:. (vuejs#1648)
1 parent ae160d9 commit 5788883

File tree

2 files changed

+96
-15
lines changed

2 files changed

+96
-15
lines changed

lib/rules/v-on-event-hyphenation.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,16 @@ module.exports = {
5151
const ignoredAttributes = (optionsPayload && optionsPayload.ignore) || []
5252
const autofix = Boolean(optionsPayload && optionsPayload.autofix)
5353

54-
const caseConverter = casing.getExactConverter(
54+
const caseConverter = casing.getConverter(
5555
useHyphenated ? 'kebab-case' : 'camelCase'
5656
)
5757

5858
/**
5959
* @param {VDirective} node
60+
* @param {VIdentifier} argument
6061
* @param {string} name
6162
*/
62-
function reportIssue(node, name) {
63+
function reportIssue(node, argument, name) {
6364
const text = sourceCode.getText(node.key)
6465

6566
context.report({
@@ -71,13 +72,14 @@ module.exports = {
7172
data: {
7273
text
7374
},
74-
fix: autofix
75-
? (fixer) =>
76-
fixer.replaceText(
77-
node.key,
78-
text.replace(name, caseConverter(name))
79-
)
80-
: null
75+
fix:
76+
autofix &&
77+
// It cannot be converted in snake_case.
78+
!name.includes('_')
79+
? (fixer) => {
80+
return fixer.replaceText(argument, caseConverter(name))
81+
}
82+
: null
8183
})
8284
}
8385

@@ -99,14 +101,13 @@ module.exports = {
99101
return utils.defineTemplateBodyVisitor(context, {
100102
"VAttribute[directive=true][key.name.name='on']"(node) {
101103
if (!utils.isCustomComponent(node.parent.parent)) return
102-
103-
const name =
104-
node.key.argument &&
105-
node.key.argument.type === 'VIdentifier' &&
106-
node.key.argument.rawName
104+
if (!node.key.argument || node.key.argument.type !== 'VIdentifier') {
105+
return
106+
}
107+
const name = node.key.argument.rawName
107108
if (!name || isIgnoredAttribute(name)) return
108109

109-
reportIssue(node, name)
110+
reportIssue(node, node.key.argument, name)
110111
}
111112
})
112113
}

tests/lib/rules/v-on-event-hyphenation.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,86 @@ tester.run('v-on-event-hyphenation', rule, {
102102
</template>
103103
`,
104104
errors: ["v-on event 'v-on:custom-event' can't be hyphenated."]
105+
},
106+
{
107+
code: `
108+
<template>
109+
<VueComponent @update:modelValue="foo"/>
110+
<VueComponent @update:model-value="foo"/>
111+
</template>
112+
`,
113+
options: ['always', { autofix: true }],
114+
output: `
115+
<template>
116+
<VueComponent @update:model-value="foo"/>
117+
<VueComponent @update:model-value="foo"/>
118+
</template>
119+
`,
120+
errors: ["v-on event '@update:modelValue' must be hyphenated."]
121+
},
122+
{
123+
code: `
124+
<template>
125+
<VueComponent @update:modelValue="foo"/>
126+
<VueComponent @update:model-value="foo"/>
127+
</template>
128+
`,
129+
options: ['never', { autofix: true }],
130+
output: `
131+
<template>
132+
<VueComponent @update:modelValue="foo"/>
133+
<VueComponent @update:modelValue="foo"/>
134+
</template>
135+
`,
136+
errors: ["v-on event '@update:model-value' can't be hyphenated."]
137+
},
138+
{
139+
code: `
140+
<template>
141+
<VueComponent @upDate:modelValue="foo"/>
142+
<VueComponent @up-date:modelValue="foo"/>
143+
<VueComponent @upDate:model-value="foo"/>
144+
<VueComponent @up-date:model-value="foo"/>
145+
</template>
146+
`,
147+
options: ['always', { autofix: true }],
148+
output: `
149+
<template>
150+
<VueComponent @up-date:model-value="foo"/>
151+
<VueComponent @up-date:model-value="foo"/>
152+
<VueComponent @up-date:model-value="foo"/>
153+
<VueComponent @up-date:model-value="foo"/>
154+
</template>
155+
`,
156+
errors: [
157+
"v-on event '@upDate:modelValue' must be hyphenated.",
158+
"v-on event '@up-date:modelValue' must be hyphenated.",
159+
"v-on event '@upDate:model-value' must be hyphenated."
160+
]
161+
},
162+
{
163+
code: `
164+
<template>
165+
<VueComponent @upDate:modelValue="foo"/>
166+
<VueComponent @up-date:modelValue="foo"/>
167+
<VueComponent @upDate:model-value="foo"/>
168+
<VueComponent @up-date:model-value="foo"/>
169+
</template>
170+
`,
171+
options: ['never', { autofix: true }],
172+
output: `
173+
<template>
174+
<VueComponent @upDate:modelValue="foo"/>
175+
<VueComponent @upDate:modelValue="foo"/>
176+
<VueComponent @upDate:modelValue="foo"/>
177+
<VueComponent @upDate:modelValue="foo"/>
178+
</template>
179+
`,
180+
errors: [
181+
"v-on event '@up-date:modelValue' can't be hyphenated.",
182+
"v-on event '@upDate:model-value' can't be hyphenated.",
183+
"v-on event '@up-date:model-value' can't be hyphenated."
184+
]
105185
}
106186
]
107187
})

0 commit comments

Comments
 (0)