Skip to content

Commit 7f39dc7

Browse files
authored
Update vue/one-component-per-file rule doc and update preset rules (#1149)
* Update `vue/one-component-per-file` rule doc and update preset rules * update
1 parent 64cd823 commit 7f39dc7

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

docs/rules/one-component-per-file.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
# enforce that each component should be in its own file
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/one-component-per-file
5+
description: enforce that each component should be in its own file
6+
---
7+
# vue/one-component-per-file
8+
> enforce that each component should be in its own file
9+
10+
- :gear: This rule is included in all of `"plugin:vue/vue3-strongly-recommended"`, `"plugin:vue/strongly-recommended"`, `"plugin:vue/vue3-recommended"` and `"plugin:vue/recommended"`.
211

312
## :book: Rule Details
413

5-
This rule checks if there is oly one component per file.
14+
This rule checks if there is only one component per file.
615

7-
:-1: Examples of **incorrect** code for this rule:
16+
<eslint-code-block filename="a.js" language="javascript" :rules="{'vue/one-component-per-file': ['error']}">
817

918
```js
19+
/* ✗ BAD */
20+
1021
Vue.component('TodoList', {
1122
// ...
1223
})
@@ -16,18 +27,30 @@ Vue.component('TodoItem', {
1627
})
1728
```
1829

19-
:+1: Examples of **correct** code for this rule:
30+
</eslint-code-block>
2031

21-
```js
32+
<eslint-code-block :rules="{'vue/one-component-per-file': ['error']}">
33+
34+
```vue
35+
<script>
36+
/* ✓ GOOD */
2237
export default {
2338
name: 'my-component'
2439
}
40+
</script>
2541
```
2642

43+
</eslint-code-block>
44+
2745
## :wrench: Options
2846

2947
Nothing.
3048

3149
## :books: Further reading
3250

3351
- [Style guide - Component files](https://vuejs.org/v2/style-guide/#Component-files-strongly-recommended)
52+
53+
## :mag: Implementation
54+
55+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/one-component-per-file.js)
56+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/one-component-per-file.js)

lib/rules/one-component-per-file.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ module.exports = {
1414
type: 'suggestion',
1515
docs: {
1616
description: 'enforce that each component should be in its own file',
17-
category: undefined, // strongly-recommended
18-
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.5/docs/rules/one-component-per-file.md'
17+
categories: ['vue3-strongly-recommended', 'strongly-recommended'],
18+
url: 'https://eslint.vuejs.org/rules/one-component-per-file.html'
1919
},
2020
fixable: null,
2121
schema: [],
@@ -24,19 +24,21 @@ module.exports = {
2424
}
2525
},
2626
create (context) {
27-
let componentCount = 0
27+
const components = []
2828

2929
return Object.assign({},
30-
utils.executeOnVueComponent(context, () => {
31-
++componentCount
30+
utils.executeOnVueComponent(context, (node) => {
31+
components.push(node)
3232
}),
3333
{
34-
'Program:exit' (node) {
35-
if (componentCount > 1) {
36-
context.report({
37-
node: node,
38-
messageId: 'toManyComponents'
39-
})
34+
'Program:exit' () {
35+
if (components.length > 1) {
36+
for (const node of components) {
37+
context.report({
38+
node: node,
39+
messageId: 'toManyComponents'
40+
})
41+
}
4042
}
4143
}
4244
}

tests/lib/rules/one-component-per-file.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,38 @@ ruleTester.run('one-component-per-file', rule, {
6060
Vue.component('name', {})
6161
Vue.component('name', {})
6262
`,
63-
errors: [{
64-
message: 'There is more than one component in this file.'
65-
}]
63+
errors: [
64+
'There is more than one component in this file.',
65+
'There is more than one component in this file.'
66+
]
6667
},
6768
{
6869
filename: 'test.js',
6970
code: `
7071
Vue.component('TodoList', {
7172
// ...
7273
})
73-
74+
7475
Vue.component('TodoItem', {
7576
// ...
7677
})
7778
export default {}
7879
`,
79-
errors: [{
80-
message: 'There is more than one component in this file.'
81-
}]
80+
errors: [
81+
'There is more than one component in this file.',
82+
'There is more than one component in this file.'
83+
]
8284
},
8385
{
8486
filename: 'test.vue',
8587
code: `
8688
Vue.component('name', {})
8789
export default {}
8890
`,
89-
errors: [{
90-
message: 'There is more than one component in this file.'
91-
}]
91+
errors: [
92+
'There is more than one component in this file.',
93+
'There is more than one component in this file.'
94+
]
9295
}
9396
]
9497
})

0 commit comments

Comments
 (0)