Skip to content

Commit 12054ea

Browse files
ota-meshimichalsnik
authored andcommitted
[Update] Make vue/prop-name-casing fixable (#402)
* [Update] Make `vue/prop-name-casing` fixable * [fix] Only ASCII printable characters can be auto fixable * [fix] That `vue/attributes-order` got duplicated when merging README
1 parent 4c25403 commit 12054ea

File tree

2 files changed

+193
-2
lines changed

2 files changed

+193
-2
lines changed

Diff for: lib/rules/prop-name-casing.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ const utils = require('../utils')
88
const casing = require('../utils/casing')
99
const allowedCaseOptions = ['camelCase', 'snake_case']
1010

11+
function canFixPropertyName (node, originalName) {
12+
// Can not fix of computed property names & shorthand
13+
if (node.computed || node.shorthand) {
14+
return false
15+
}
16+
const key = node.key
17+
// Can not fix of unknown types
18+
if (key.type !== 'Literal' && key.type !== 'Identifier') {
19+
return false
20+
}
21+
// Can fix of ASCII printable characters
22+
return originalName.match(/[ -~]+/)
23+
}
24+
1125
// ------------------------------------------------------------------------------
1226
// Rule Definition
1327
// ------------------------------------------------------------------------------
@@ -46,7 +60,12 @@ function create (context) {
4660
data: {
4761
name: propName,
4862
caseType: caseType
49-
}
63+
},
64+
fix: canFixPropertyName(item, propName) ? fixer => {
65+
return item.key.type === 'Literal'
66+
? fixer.replaceText(item.key, item.key.raw.replace(item.key.value, convertedName))
67+
: fixer.replaceText(item.key, convertedName)
68+
} : undefined
5069
})
5170
}
5271
}
@@ -63,7 +82,7 @@ module.exports = {
6382
description: 'enforce specific casing for the Prop name in Vue components',
6483
category: undefined // 'strongly-recommended'
6584
},
66-
fixable: null, // or "code" or "whitespace"
85+
fixable: 'code', // null or "code" or "whitespace"
6786
schema: [
6887
{
6988
enum: allowedCaseOptions

Diff for: tests/lib/rules/prop-name-casing.js

+172
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ ruleTester.run('prop-name-casing', rule, {
121121
}
122122
}
123123
`,
124+
output: `
125+
export default {
126+
props: {
127+
greetingText: String
128+
}
129+
}
130+
`,
124131
parserOptions,
125132
errors: [{
126133
message: 'Prop "greeting_text" is not in camelCase.',
@@ -138,6 +145,13 @@ ruleTester.run('prop-name-casing', rule, {
138145
}
139146
`,
140147
options: ['camelCase'],
148+
output: `
149+
export default {
150+
props: {
151+
greetingText: String
152+
}
153+
}
154+
`,
141155
parserOptions,
142156
errors: [{
143157
message: 'Prop "greeting_text" is not in camelCase.',
@@ -155,6 +169,13 @@ ruleTester.run('prop-name-casing', rule, {
155169
}
156170
`,
157171
options: ['snake_case'],
172+
output: `
173+
export default {
174+
props: {
175+
greeting_text: String
176+
}
177+
}
178+
`,
158179
parserOptions,
159180
errors: [{
160181
message: 'Prop "greetingText" is not in snake_case.',
@@ -172,6 +193,13 @@ ruleTester.run('prop-name-casing', rule, {
172193
}
173194
`,
174195
options: ['camelCase'],
196+
output: `
197+
export default {
198+
props: {
199+
'greetingText': String
200+
}
201+
}
202+
`,
175203
parserOptions,
176204
errors: [{
177205
message: 'Prop "greeting-text" is not in camelCase.',
@@ -189,12 +217,156 @@ ruleTester.run('prop-name-casing', rule, {
189217
}
190218
`,
191219
options: ['snake_case'],
220+
output: `
221+
export default {
222+
props: {
223+
'greeting_text': String
224+
}
225+
}
226+
`,
192227
parserOptions,
193228
errors: [{
194229
message: 'Prop "greeting-text" is not in snake_case.',
195230
type: 'Property',
196231
line: 4
197232
}]
233+
},
234+
{
235+
filename: 'test.vue',
236+
code: `
237+
export default {
238+
props: {
239+
'greeting_text': String
240+
}
241+
}
242+
`,
243+
output: `
244+
export default {
245+
props: {
246+
'greetingText': String
247+
}
248+
}
249+
`,
250+
parserOptions,
251+
errors: [{
252+
message: 'Prop "greeting_text" is not in camelCase.',
253+
type: 'Property',
254+
line: 4
255+
}]
256+
},
257+
{
258+
// computed property name
259+
filename: 'test.vue',
260+
code: `
261+
export default {
262+
props: {
263+
['greeting-text']: String
264+
}
265+
}
266+
`,
267+
output: null,
268+
parserOptions,
269+
errors: [{
270+
message: 'Prop "greeting-text" is not in camelCase.',
271+
type: 'Property',
272+
line: 4
273+
}]
274+
},
275+
{
276+
// shorthand
277+
filename: 'test.vue',
278+
code: `
279+
export default {
280+
props: {
281+
greeting_text
282+
}
283+
}
284+
`,
285+
output: null,
286+
parserOptions,
287+
errors: [{
288+
message: 'Prop "greeting_text" is not in camelCase.',
289+
type: 'Property',
290+
line: 4
291+
}]
292+
},
293+
{
294+
// valiable computed property name
295+
filename: 'test.vue',
296+
code: `
297+
export default {
298+
props: {
299+
[greeting_text]: String
300+
}
301+
}
302+
`,
303+
output: null,
304+
parserOptions,
305+
errors: [{
306+
// bug ?
307+
message: 'Prop "greeting_text" is not in camelCase.',
308+
type: 'Property',
309+
line: 4
310+
}]
311+
},
312+
{
313+
// emoji
314+
filename: 'test.vue',
315+
code: `
316+
export default {
317+
props: {
318+
'\u{1F37B}': String
319+
}
320+
}
321+
`,
322+
output: null,
323+
parserOptions,
324+
errors: [{
325+
message: 'Prop "\u{1F37B}" is not in camelCase.',
326+
type: 'Property',
327+
line: 4
328+
}]
329+
},
330+
{
331+
// Japanese characters
332+
filename: 'test.vue',
333+
code: `
334+
export default {
335+
props: {
336+
'漢字': String
337+
}
338+
}
339+
`,
340+
output: null,
341+
parserOptions,
342+
errors: [{
343+
message: 'Prop "漢字" is not in camelCase.',
344+
type: 'Property',
345+
line: 4
346+
}]
347+
},
348+
{
349+
filename: 'test.vue',
350+
code: `
351+
export default {
352+
props: {
353+
'abc-123-def': String
354+
}
355+
}
356+
`,
357+
output: `
358+
export default {
359+
props: {
360+
'abc123Def': String
361+
}
362+
}
363+
`,
364+
parserOptions,
365+
errors: [{
366+
message: 'Prop "abc-123-def" is not in camelCase.',
367+
type: 'Property',
368+
line: 4
369+
}]
198370
}
199371
]
200372
})

0 commit comments

Comments
 (0)