Skip to content

Commit 5ed9a17

Browse files
committed
⭐️New: Add component-definition-name-casing rule. #256
this rule replaces `name-property-casing` fix issue: #251
1 parent 501a409 commit 5ed9a17

File tree

5 files changed

+392
-0
lines changed

5 files changed

+392
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# enforce specific casing for component definition name (vue/component-definition-name-casing)
2+
3+
- :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.
4+
5+
Define a style for component definition name casing for consistency purposes.
6+
7+
## :book: Rule Details
8+
9+
:+1: Examples of **correct** code for `PascalCase`:
10+
11+
```js
12+
export default {
13+
name: 'MyComponent'
14+
}
15+
```
16+
```js
17+
Vue.component('MyComponent', {
18+
19+
})
20+
```
21+
22+
:+1: Examples of **correct** code for `kebab-case`:
23+
24+
```js
25+
export default {
26+
name: 'my-component'
27+
}
28+
```
29+
```js
30+
Vue.component('my-component', {
31+
32+
})
33+
```
34+
35+
## :wrench: Options
36+
37+
Default casing is set to `PascalCase`.
38+
39+
```json
40+
{
41+
"vue/component-definition-name-casing": ["error", "PascalCase|kebab-case"]
42+
}
43+
```
44+
45+
## Related links
46+
47+
- [Style guide - Component name casing in JS/JSX](https://vuejs.org/v2/style-guide/#Component-name-casing-in-JS-JSX-strongly-recommended)

docs/rules/name-property-casing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- :gear: This rule is included in `"plugin:vue/strongly-recommended"` and `"plugin:vue/recommended"`.
44
- :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.
5+
- :warning: This rule was **deprecated** and replaced by [vue/component-definition-name-casing](component-definition-name-casing.md) rule.
56

67
Define a style for the `name` property casing for consistency purposes.
78

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @fileoverview enforce specific casing for component definition name
3+
* @author Armano
4+
*/
5+
'use strict'
6+
7+
const utils = require('../utils')
8+
const casing = require('../utils/casing')
9+
const allowedCaseOptions = ['PascalCase', 'kebab-case']
10+
11+
// ------------------------------------------------------------------------------
12+
// Rule Definition
13+
// ------------------------------------------------------------------------------
14+
15+
module.exports = {
16+
meta: {
17+
docs: {
18+
description: 'enforce specific casing for component definition name',
19+
category: undefined,
20+
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/component-definition-name-casing.md'
21+
},
22+
fixable: 'code', // or "code" or "whitespace"
23+
schema: [
24+
{
25+
enum: allowedCaseOptions
26+
}
27+
]
28+
},
29+
30+
create (context) {
31+
const options = context.options[0]
32+
const caseType = allowedCaseOptions.indexOf(options) !== -1 ? options : 'PascalCase'
33+
34+
// ----------------------------------------------------------------------
35+
// Public
36+
// ----------------------------------------------------------------------
37+
38+
function convertName (node) {
39+
const value = casing.getConverter(caseType)(node.value)
40+
if (value !== node.value) {
41+
context.report({
42+
node: node,
43+
message: 'Property name "{{value}}" is not {{caseType}}.',
44+
data: {
45+
value: node.value,
46+
caseType: caseType
47+
},
48+
fix: fixer => fixer.replaceText(node, node.raw.replace(node.value, value))
49+
})
50+
}
51+
}
52+
53+
return utils.executeOnVue(context, (obj) => {
54+
if (obj.parent && obj.parent.type === 'CallExpression' && obj.parent.arguments && obj.parent.arguments.length === 2) {
55+
const argument = obj.parent.arguments[0]
56+
if (argument.type === 'Literal') {
57+
convertName(argument)
58+
}
59+
}
60+
61+
const node = obj.properties
62+
.find(item => (
63+
item.type === 'Property' &&
64+
item.key.name === 'name' &&
65+
item.value.type === 'Literal'
66+
))
67+
68+
if (!node) return
69+
convertName(node.value)
70+
})
71+
}
72+
}

lib/rules/name-property-casing.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module.exports = {
1919
category: 'strongly-recommended',
2020
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/name-property-casing.md'
2121
},
22+
deprecated: true,
23+
replacedBy: ['component-definition-name-casing'],
2224
fixable: 'code', // or "code" or "whitespace"
2325
schema: [
2426
{

0 commit comments

Comments
 (0)