Skip to content

Commit 824d35a

Browse files
committed
Fix review comments for multi-word-component-names rule
1 parent a7b8030 commit 824d35a

File tree

6 files changed

+48
-21
lines changed

6 files changed

+48
-21
lines changed

docs/rules/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ Enforce all the rules in this category, as well as all higher priority rules, wi
3939

4040
| Rule ID | Description | |
4141
|:--------|:------------|:---|
42-
| [vue/multi-word-component-names](./multi-word-component-names.md) | require component names to be always multi-word | |
4342
| [vue/no-arrow-functions-in-watch](./no-arrow-functions-in-watch.md) | disallow using arrow functions to define watcher | |
4443
| [vue/no-async-in-computed-properties](./no-async-in-computed-properties.md) | disallow asynchronous actions in computed properties | |
4544
| [vue/no-deprecated-data-object-declaration](./no-deprecated-data-object-declaration.md) | disallow using deprecated object declaration on data (in Vue.js 3.0.0+) | :wrench: |
@@ -172,7 +171,6 @@ Enforce all the rules in this category, as well as all higher priority rules, wi
172171

173172
| Rule ID | Description | |
174173
|:--------|:------------|:---|
175-
| [vue/multi-word-component-names](./multi-word-component-names.md) | require component names to be always multi-word | |
176174
| [vue/no-arrow-functions-in-watch](./no-arrow-functions-in-watch.md) | disallow using arrow functions to define watcher | |
177175
| [vue/no-async-in-computed-properties](./no-async-in-computed-properties.md) | disallow asynchronous actions in computed properties | |
178176
| [vue/no-custom-modifiers-on-v-model](./no-custom-modifiers-on-v-model.md) | disallow custom modifiers on v-model used on the component | |
@@ -298,6 +296,7 @@ For example:
298296
| [vue/html-comment-content-spacing](./html-comment-content-spacing.md) | enforce unified spacing in HTML comments | :wrench: |
299297
| [vue/html-comment-indent](./html-comment-indent.md) | enforce consistent indentation in HTML comments | :wrench: |
300298
| [vue/match-component-file-name](./match-component-file-name.md) | require component name property to match its file name | |
299+
| [vue/multi-word-component-names](./multi-word-component-names.md) | require component names to be always multi-word | |
301300
| [vue/new-line-between-multi-line-property](./new-line-between-multi-line-property.md) | enforce new lines between multi-line properties in Vue components | :wrench: |
302301
| [vue/next-tick-style](./next-tick-style.md) | enforce Promise or callback style in `nextTick` | :wrench: |
303302
| [vue/no-bare-strings-in-template](./no-bare-strings-in-template.md) | disallow the use of bare strings in `<template>` | |

docs/rules/multi-word-component-names.md

+37-11
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,65 @@ description: require component names to be always multi-word
99
> require component names to be always multi-word
1010
1111
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
12-
- :gear: This rule is included in all of `"plugin:vue/vue3-essential"`, `"plugin:vue/essential"`, `"plugin:vue/vue3-strongly-recommended"`, `"plugin:vue/strongly-recommended"`, `"plugin:vue/vue3-recommended"` and `"plugin:vue/recommended"`.
1312

1413
## :book: Rule Details
1514

16-
This rule ....
15+
This rule require component names to be always multi-word, except for root `App`
16+
components, and built-in components provided by Vue, such as `<transition>` or
17+
`<component>`. This prevents conflicts with existing and future HTML elements,
18+
since all HTML elements are a single word.
1719

18-
<eslint-code-block :rules="{'vue/multi-word-component-names': ['error']}">
20+
<eslint-code-block filename="src/TodoItem.js" language="javascript" :rules="{'vue/multi-word-component-names': ['error']}">
1921

20-
```vue
21-
<template>
22+
```js
2223
/* ✓ GOOD */
2324
Vue.component('todo-item', {
2425
// ...
2526
})
2627

28+
/* ✗ BAD */
29+
Vue.component('Todo', {
30+
// ...
31+
})
32+
```
33+
</eslint-code-block>
34+
35+
<eslint-code-block filename="src/TodoItem.js" :rules="{'vue/multi-word-component-names': ['error']}">
36+
37+
```vue
38+
<script>
39+
/* ✓ GOOD */
2740
export default {
2841
name: 'TodoItem',
2942
// ...
3043
}
44+
</script>
45+
```
46+
</eslint-code-block>
3147

32-
/* ✗ BAD */
33-
34-
Vue.component('todo', {
35-
// ...
36-
})
48+
<eslint-code-block filename="src/Todo.vue" :rules="{'vue/multi-word-component-names': ['error']}">
3749

50+
```vue
51+
<script>
52+
/* ✗ BAD */
3853
export default {
3954
name: 'Todo',
4055
// ...
4156
}
42-
</template>
57+
</script>
4358
```
59+
</eslint-code-block>
60+
61+
<eslint-code-block filename="src/Todo.vue" :rules="{'vue/multi-word-component-names': ['error']}">
4462

63+
```vue
64+
<script>
65+
/* ✗ BAD */
66+
export default {
67+
// ...
68+
}
69+
</script>
70+
```
4571
</eslint-code-block>
4672

4773
## :wrench: Options

lib/configs/essential.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
module.exports = {
77
extends: require.resolve('./base'),
88
rules: {
9-
'vue/multi-word-component-names': 'error',
109
'vue/no-arrow-functions-in-watch': 'error',
1110
'vue/no-async-in-computed-properties': 'error',
1211
'vue/no-custom-modifiers-on-v-model': 'error',

lib/configs/vue3-essential.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
module.exports = {
77
extends: require.resolve('./base'),
88
rules: {
9-
'vue/multi-word-component-names': 'error',
109
'vue/no-arrow-functions-in-watch': 'error',
1110
'vue/no-async-in-computed-properties': 'error',
1211
'vue/no-deprecated-data-object-declaration': 'error',

lib/rules/multi-word-component-names.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// ------------------------------------------------------------------------------
88
// Requirements
99
// ------------------------------------------------------------------------------
10-
const path = require('path')
1110

1211
const casing = require('../utils/casing')
1312
const utils = require('../utils')
@@ -39,10 +38,10 @@ function isValidComponentName(name) {
3938

4039
module.exports = {
4140
meta: {
42-
type: 'problem',
41+
type: 'suggestion',
4342
docs: {
4443
description: 'require component names to be always multi-word',
45-
categories: ['vue3-essential', 'essential'],
44+
categories: undefined,
4645
url: 'https://eslint.vuejs.org/rules/multi-word-component-names.html'
4746
},
4847
schema: [],
@@ -53,7 +52,7 @@ module.exports = {
5352
/** @param {RuleContext} context */
5453
create(context) {
5554
const fileName = context.getFilename()
56-
let componentName = path.parse(fileName).name
55+
let componentName = fileName.replace(/\.[^/.]+$/, '')
5756

5857
return utils.compositingVisitors(
5958
{
@@ -78,12 +77,16 @@ module.exports = {
7877
utils.executeOnVue(context, (obj) => {
7978
const node = utils.findProperty(obj, 'name')
8079

80+
/** @type {SourceLocation | null} */
81+
let loc = null
82+
8183
// Check if the component has a name property.
8284
if (node) {
8385
const valueNode = node.value
8486
if (valueNode.type !== 'Literal') return
8587

8688
componentName = `${valueNode.value}`
89+
loc = node.loc
8790
} else if (
8891
obj.parent.type === 'CallExpression' &&
8992
obj.parent.arguments.length === 2
@@ -94,6 +97,7 @@ module.exports = {
9497
if (argument.type !== 'Literal') return
9598

9699
componentName = `${argument.value}`
100+
loc = argument.loc
97101
}
98102

99103
if (!isValidComponentName(componentName)) {
@@ -102,7 +106,7 @@ module.exports = {
102106
data: {
103107
value: componentName
104108
},
105-
node: node || obj
109+
loc: loc || { line: 1, column: 0 }
106110
})
107111
}
108112
})

tests/lib/rules/multi-word-component-names.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ tester.run('multi-word-component-names', rule, {
232232
errors: [
233233
{
234234
message: 'Component name "invalid" should always be multi-word.',
235-
line: 3
235+
line: 1
236236
}
237237
]
238238
},

0 commit comments

Comments
 (0)