Skip to content

[Update] Make vue/prop-name-casing fixable #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/rules/prop-name-casing.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ const utils = require('../utils')
const casing = require('../utils/casing')
const allowedCaseOptions = ['camelCase', 'snake_case']

function canFixPropertyName (node, originalName) {
// Can not fix of computed property names & shorthand
if (node.computed || node.shorthand) {
return false
}
const key = node.key
// Can not fix of unknown types
if (key.type !== 'Literal' && key.type !== 'Identifier') {
return false
}
// Can fix of ASCII printable characters
return originalName.match(/[ -~]+/)
}

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -46,7 +60,12 @@ function create (context) {
data: {
name: propName,
caseType: caseType
}
},
fix: canFixPropertyName(item, propName) ? fixer => {
return item.key.type === 'Literal'
? fixer.replaceText(item.key, item.key.raw.replace(item.key.value, convertedName))
: fixer.replaceText(item.key, convertedName)
} : undefined
})
}
}
Expand All @@ -63,7 +82,7 @@ module.exports = {
description: 'enforce specific casing for the Prop name in Vue components',
category: undefined // 'strongly-recommended'
},
fixable: null, // or "code" or "whitespace"
fixable: 'code', // null or "code" or "whitespace"
schema: [
{
enum: allowedCaseOptions
Expand Down
172 changes: 172 additions & 0 deletions tests/lib/rules/prop-name-casing.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ ruleTester.run('prop-name-casing', rule, {
}
}
`,
output: `
export default {
props: {
greetingText: String
}
}
`,
parserOptions,
errors: [{
message: 'Prop "greeting_text" is not in camelCase.',
Expand All @@ -138,6 +145,13 @@ ruleTester.run('prop-name-casing', rule, {
}
`,
options: ['camelCase'],
output: `
export default {
props: {
greetingText: String
}
}
`,
parserOptions,
errors: [{
message: 'Prop "greeting_text" is not in camelCase.',
Expand All @@ -155,6 +169,13 @@ ruleTester.run('prop-name-casing', rule, {
}
`,
options: ['snake_case'],
output: `
export default {
props: {
greeting_text: String
}
}
`,
parserOptions,
errors: [{
message: 'Prop "greetingText" is not in snake_case.',
Expand All @@ -172,6 +193,13 @@ ruleTester.run('prop-name-casing', rule, {
}
`,
options: ['camelCase'],
output: `
export default {
props: {
'greetingText': String
}
}
`,
parserOptions,
errors: [{
message: 'Prop "greeting-text" is not in camelCase.',
Expand All @@ -189,12 +217,156 @@ ruleTester.run('prop-name-casing', rule, {
}
`,
options: ['snake_case'],
output: `
export default {
props: {
'greeting_text': String
}
}
`,
parserOptions,
errors: [{
message: 'Prop "greeting-text" is not in snake_case.',
type: 'Property',
line: 4
}]
},
{
filename: 'test.vue',
code: `
export default {
props: {
'greeting_text': String
}
}
`,
output: `
export default {
props: {
'greetingText': String
}
}
`,
parserOptions,
errors: [{
message: 'Prop "greeting_text" is not in camelCase.',
type: 'Property',
line: 4
}]
},
{
// computed property name
filename: 'test.vue',
code: `
export default {
props: {
['greeting-text']: String
}
}
`,
output: null,
parserOptions,
errors: [{
message: 'Prop "greeting-text" is not in camelCase.',
type: 'Property',
line: 4
}]
},
{
// shorthand
filename: 'test.vue',
code: `
export default {
props: {
greeting_text
}
}
`,
output: null,
parserOptions,
errors: [{
message: 'Prop "greeting_text" is not in camelCase.',
type: 'Property',
line: 4
}]
},
{
// valiable computed property name
filename: 'test.vue',
code: `
export default {
props: {
[greeting_text]: String
}
}
`,
output: null,
parserOptions,
errors: [{
// bug ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess so, it shouldn't throw error in this case I think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can deal with it in separate task.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your view
I wrote an issue #442

message: 'Prop "greeting_text" is not in camelCase.',
type: 'Property',
line: 4
}]
},
{
// emoji
filename: 'test.vue',
code: `
export default {
props: {
'\u{1F37B}': String
}
}
`,
output: null,
parserOptions,
errors: [{
message: 'Prop "\u{1F37B}" is not in camelCase.',
type: 'Property',
line: 4
}]
},
{
// Japanese characters
filename: 'test.vue',
code: `
export default {
props: {
'漢字': String
}
}
`,
output: null,
parserOptions,
errors: [{
message: 'Prop "漢字" is not in camelCase.',
type: 'Property',
line: 4
}]
},
{
filename: 'test.vue',
code: `
export default {
props: {
'abc-123-def': String
}
}
`,
output: `
export default {
props: {
'abc123Def': String
}
}
`,
parserOptions,
errors: [{
message: 'Prop "abc-123-def" is not in camelCase.',
type: 'Property',
line: 4
}]
}
]
})