Skip to content

Commit 7c5e1ee

Browse files
authored
fix(v-bind-style): only change kebabCase to camelCase (#2410)
1 parent 0247424 commit 7c5e1ee

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

lib/rules/v-bind-style.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ const casing = require('../utils/casing')
1313
* @typedef { VDirective & { key: VBindDirectiveKey } } VBindDirective
1414
*/
1515

16+
/**
17+
* @param {string} name
18+
* @returns {string}
19+
*/
20+
function kebabCaseToCamelCase(name) {
21+
return casing.isKebabCase(name) ? casing.camelCase(name) : name
22+
}
23+
1624
/**
1725
* @param {VBindDirective} node
1826
* @returns {boolean}
@@ -24,11 +32,10 @@ function isSameName(node) {
2432
node.value?.expression?.type === 'Identifier'
2533
? node.value.expression.name
2634
: null
27-
return Boolean(
28-
attrName &&
29-
valueName &&
30-
casing.camelCase(attrName) === casing.camelCase(valueName)
31-
)
35+
36+
if (!attrName || !valueName) return false
37+
38+
return kebabCaseToCamelCase(attrName) === kebabCaseToCamelCase(valueName)
3239
}
3340

3441
/**
@@ -144,7 +151,7 @@ module.exports = {
144151
} else if (node.key.argument.type === 'VIdentifier') {
145152
yield fixer.insertTextAfter(
146153
node,
147-
`="${casing.camelCase(node.key.argument.rawName)}"`
154+
`="${kebabCaseToCamelCase(node.key.argument.rawName)}"`
148155
)
149156
}
150157
}

tests/lib/rules/v-bind-style.js

+21
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ tester.run('v-bind-style', rule, {
124124
filename: 'test.vue',
125125
code: '<template><div :foo-bar/></template>',
126126
options: ['shorthand', { sameNameShorthand: 'always' }]
127+
},
128+
{
129+
// https://github.com/vuejs/eslint-plugin-vue/issues/2409
130+
filename: 'test.vue',
131+
code: '<template><div :foo-bar="foo_bar" /></template>',
132+
options: ['shorthand', { sameNameShorthand: 'always' }]
127133
}
128134
],
129135
invalid: [
@@ -235,6 +241,14 @@ tester.run('v-bind-style', rule, {
235241
options: ['shorthand', { sameNameShorthand: 'never' }],
236242
errors: [unexpectedShorthand]
237243
},
244+
{
245+
// https://github.com/vuejs/eslint-plugin-vue/issues/2409
246+
filename: 'test.vue',
247+
code: '<template><div :foo_bar /></template>',
248+
output: '<template><div :foo_bar="foo_bar" /></template>',
249+
options: ['shorthand', { sameNameShorthand: 'never' }],
250+
errors: [unexpectedShorthand]
251+
},
238252
// same-name shorthand: always
239253
{
240254
filename: 'test.vue',
@@ -243,6 +257,13 @@ tester.run('v-bind-style', rule, {
243257
options: ['shorthand', { sameNameShorthand: 'always' }],
244258
errors: [expectedShorthand]
245259
},
260+
{
261+
filename: 'test.vue',
262+
code: '<template><div :foo_bar="foo_bar" /></template>',
263+
output: '<template><div :foo_bar /></template>',
264+
options: ['shorthand', { sameNameShorthand: 'always' }],
265+
errors: [expectedShorthand]
266+
},
246267
{
247268
filename: 'test.vue',
248269
code: '<template><div v-bind:foo="foo" /></template>',

0 commit comments

Comments
 (0)