-
-
Notifications
You must be signed in to change notification settings - Fork 681
Add vue/component-options-name-casing
rule
#1725
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
ota-meshi
merged 8 commits into
vuejs:master
from
g-plane:component-options-name-casing
Nov 30, 2021
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f90ff31
Add `vue/component-options-name-casing` rule
g-plane afd39ab
fix docs
g-plane 095d4f4
fix demo
g-plane 51f3cf9
refactor
g-plane 42afe42
fix auto-fix
g-plane 617935c
fix checking kebab-case
g-plane 63b3838
provide suggestions
g-plane 7144320
accept suggestions
g-plane 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
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,137 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/component-options-name-casing | ||
description: enforce the casing of component name in `components` options | ||
--- | ||
# vue/component-options-name-casing | ||
|
||
> enforce the casing of component name in `components` options | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. | ||
|
||
## :book: Rule Details | ||
|
||
This rule aims to enforce casing of the component names in `components` options. | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/component-options-name-casing": ["error", "PascalCase" | "kebab-case" | "camelCase"] | ||
} | ||
``` | ||
|
||
This rule has an option which can be one of these values: | ||
|
||
- `"PascalCase"` (default) ... enforce component names to pascal case. | ||
- `"kebab-case"` ... enforce component names to kebab case. | ||
- `"camelCase"` ... enforce component names to camel case. | ||
|
||
### `"PascalCase"` (default) | ||
|
||
<eslint-code-block fix :rules="{'vue/component-definition-name-casing': ['error']}"> | ||
g-plane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```vue | ||
<script> | ||
export default { | ||
/* ✓ GOOD */ | ||
components: { | ||
AppHeader, | ||
AppSidebar | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block fix :rules="{'vue/component-definition-name-casing': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
/* ✗ BAD */ | ||
components: { | ||
appHeader, | ||
'app-sidebar': AppSidebar | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `"kebab-case"` | ||
|
||
<eslint-code-block fix :rules="{'vue/component-definition-name-casing': ['error', 'kebab-case']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
/* ✓ GOOD */ | ||
components: { | ||
'app-header': AppHeader, | ||
'app-sidebar': appSidebar | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block fix :rules="{'vue/component-definition-name-casing': ['error', 'kebab-case']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
/* ✗ BAD */ | ||
components: { | ||
AppHeader, | ||
appSidebar | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `"camelCase"` | ||
|
||
<eslint-code-block fix :rules="{'vue/component-definition-name-casing': ['error', 'camelCase']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
/* ✓ GOOD */ | ||
components: { | ||
appHeader, | ||
appSidebar | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block fix :rules="{'vue/component-definition-name-casing': ['error', 'camelCase']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
/* ✗ BAD */ | ||
components: { | ||
AppHeader, | ||
'app-sidebar': appSidebar | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/component-options-name-casing.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/component-options-name-casing.js) |
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,79 @@ | ||
/** | ||
* @author Pig Fang | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
const casing = require('../utils/casing') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'enforce the casing of component name in `components` options', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/component-options-name-casing.html' | ||
}, | ||
fixable: 'code', | ||
schema: [{ enum: casing.allowedCaseOptions }], | ||
messages: { | ||
caseNotMatched: 'Component name "{{component}}" is not {{caseType}}.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const caseType = context.options[0] || 'PascalCase' | ||
|
||
const checkCase = casing.getChecker(caseType) | ||
const convert = casing.getConverter(caseType) | ||
|
||
return utils.executeOnVue(context, (obj) => { | ||
const node = utils.findProperty(obj, 'components') | ||
if (!node || node.value.type !== 'ObjectExpression') { | ||
return | ||
} | ||
|
||
node.value.properties.forEach((property) => { | ||
if ( | ||
property.type === 'Property' && | ||
property.key.type === 'Identifier' | ||
) { | ||
const { name } = property.key | ||
if (checkCase(name)) { | ||
return | ||
} | ||
|
||
context.report({ | ||
node: property.key, | ||
messageId: 'caseNotMatched', | ||
data: { | ||
component: name, | ||
caseType | ||
}, | ||
fix: (fixer) => { | ||
const converted = convert(name) | ||
if (caseType === 'kebab-case') { | ||
return property.key.range[0] === property.value.range[0] && | ||
property.key.range[1] === property.value.range[1] | ||
g-plane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
? fixer.replaceText(property, `'${converted}': ${name}`) | ||
: fixer.replaceText(property.key, `'${converted}'`) | ||
} else { | ||
return fixer.replaceText(property.key, converted) | ||
} | ||
g-plane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.