-
-
Notifications
You must be signed in to change notification settings - Fork 681
[New] Add prop-name-casing
#289
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
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e863e77
[New] Add `prop-name-casing`
fc2f689
fix message and category
551e90e
fix category
a5ca853
fix test message
f33d47d
Set category to undefined
michalsnik c690c76
Merge remote-tracking branch 'upstream/master' into prop-name-casing
michalsnik bb7db73
Merge remote-tracking branch 'upstream/master' into prop-name-casing
michalsnik 605a152
Add more tests and fix edge case scenario
michalsnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# enforce specific casing for the Prop name in Vue components(prop-name-casing) | ||
|
||
This rule would enforce proper casing of props in vue components(camelCase). | ||
|
||
## :book: Rule Details | ||
|
||
(https://vuejs.org/v2/style-guide/#Prop-name-casing-strongly-recommended). | ||
|
||
:+1: Examples of **correct** code for `camelCase`: | ||
|
||
```js | ||
export default { | ||
props: { | ||
greetingText: String | ||
} | ||
} | ||
``` | ||
|
||
:-1: Examples of **incorrect** code for `camelCase`: | ||
|
||
```js | ||
export default { | ||
props: { | ||
'greeting-text': String | ||
} | ||
} | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
Default casing is set to `camelCase`. | ||
|
||
``` | ||
"vue/prop-name-casing": ["error", "camelCase|snake_case"] | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* @fileoverview Requires specific casing for the Prop name in Vue components | ||
* @author Yu Kimura | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
const casing = require('../utils/casing') | ||
const allowedCaseOptions = ['camelCase', 'snake_case'] | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
function create (context) { | ||
const options = context.options[0] | ||
const caseType = allowedCaseOptions.indexOf(options) !== -1 ? options : 'camelCase' | ||
const converter = casing.getConverter(caseType) | ||
|
||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
|
||
return utils.executeOnVue(context, (obj) => { | ||
const node = obj.properties.find(p => | ||
p.type === 'Property' && | ||
p.key.type === 'Identifier' && | ||
p.key.name === 'props' | ||
) | ||
if (!node) return | ||
|
||
const items = node.value.type === 'ObjectExpression' ? node.value.properties : node.value.elements | ||
for (const item of items) { | ||
if (item.type !== 'Property') { | ||
return | ||
} | ||
|
||
const propName = item.key.type === 'Literal' ? item.key.value : item.key.name | ||
const convertedName = converter(propName) | ||
if (convertedName !== propName) { | ||
context.report({ | ||
node: item, | ||
message: 'Prop "{{name}}" is not {{caseType}}.', | ||
data: { | ||
name: propName, | ||
caseType: caseType | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'enforce specific casing for the Prop name in Vue components', | ||
category: 'strongly-recommended' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please change the category to be
We're not going to enable this rule by default until the next major release, so we'll stash them in special category for everyone to see what's going to be enabled in next version :) |
||
}, | ||
fixable: null, // or "code" or "whitespace" | ||
schema: [ | ||
{ | ||
enum: allowedCaseOptions | ||
} | ||
] | ||
}, | ||
|
||
create | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/** | ||
* @fileoverview Define a style for the name property casing for consistency purposes | ||
* @author Yu Kimura | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/prop-name-casing') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const parserOptions = { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
ecmaFeatures: { experimentalObjectRestSpread: true } | ||
} | ||
|
||
const ruleTester = new RuleTester() | ||
ruleTester.run('prop-name-casing', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: ['greetingText'] | ||
} | ||
`, | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: ['greetingText'] | ||
} | ||
`, | ||
options: ['camelCase'], | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: ['greetingText'] | ||
} | ||
`, | ||
options: ['snake_case'], | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
greetingText: String | ||
} | ||
} | ||
`, | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
greetingText: String | ||
} | ||
} | ||
`, | ||
options: ['camelCase'], | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
greeting_text: String | ||
} | ||
} | ||
`, | ||
options: ['snake_case'], | ||
parserOptions | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
greeting_text: String | ||
} | ||
} | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'Prop "greeting_text" is not camelCase.', | ||
type: 'Property', | ||
line: 4 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
greeting_text: String | ||
} | ||
} | ||
`, | ||
options: ['camelCase'], | ||
parserOptions, | ||
errors: [{ | ||
message: 'Prop "greeting_text" is not camelCase.', | ||
type: 'Property', | ||
line: 4 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
greetingText: String | ||
} | ||
} | ||
`, | ||
options: ['snake_case'], | ||
parserOptions, | ||
errors: [{ | ||
message: 'Prop "greetingText" is not snake_case.', | ||
type: 'Property', | ||
line: 4 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
'greeting-text': String | ||
} | ||
} | ||
`, | ||
options: ['camelCase'], | ||
parserOptions, | ||
errors: [{ | ||
message: 'Prop "greeting-text" is not camelCase.', | ||
type: 'Property', | ||
line: 4 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
'greeting-text': String | ||
} | ||
} | ||
`, | ||
options: ['snake_case'], | ||
parserOptions, | ||
errors: [{ | ||
message: 'Prop "greeting-text" is not snake_case.', | ||
type: 'Property', | ||
line: 4 | ||
}] | ||
} | ||
] | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be:
is not in