-
-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Add
vue/one-component-per-file
rule
- Loading branch information
Showing
4 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,33 @@ | ||
# enforce that each component should be in its own file | ||
|
||
## :book: Rule Details | ||
|
||
This rule checks if there is oly one component per file. | ||
|
||
:-1: Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
Vue.component('TodoList', { | ||
// ... | ||
}) | ||
|
||
Vue.component('TodoItem', { | ||
// ... | ||
}) | ||
``` | ||
|
||
:+1: Examples of **correct** code for this rule: | ||
|
||
```js | ||
export default { | ||
name: 'my-component' | ||
} | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :books: Further reading | ||
|
||
- [Style guide - Component files](https://vuejs.org/v2/style-guide/#Component-files-strongly-recommended) |
This file contains 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 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,44 @@ | ||
/** | ||
* @fileoverview enforce that each component should be in its own file | ||
* @author Armano | ||
*/ | ||
'use strict' | ||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'enforce that each component should be in its own file', | ||
category: undefined, // strongly-recommended | ||
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.4/docs/rules/one-component-per-file.md' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
toManyComponents: 'There is to many components in this file.' | ||
} | ||
}, | ||
create (context) { | ||
let componentCount = 0 | ||
|
||
return Object.assign({}, | ||
utils.executeOnVueComponent(context, () => { | ||
++componentCount | ||
}), | ||
{ | ||
'Program:exit' (node) { | ||
if (componentCount > 1) { | ||
context.report({ | ||
node: node, | ||
messageId: 'toManyComponents' | ||
}) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} |
This file contains 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,94 @@ | ||
/** | ||
* @fileoverview enforce that each component should be in its own file | ||
* @author Armano | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/one-component-per-file') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
ruleTester.run('one-component-per-file', rule, { | ||
valid: [ | ||
{ | ||
filename: 'test.js', | ||
code: `Vue.component('name', {})` | ||
}, | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
Vue.component('name', {}) | ||
new Vue({}) | ||
` | ||
}, | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
const foo = {} | ||
new Vue({}) | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `export default {}` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `export default { | ||
components: { | ||
test: { | ||
name: 'foo' | ||
} | ||
} | ||
}` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
Vue.component('name', {}) | ||
Vue.component('name', {}) | ||
`, | ||
errors: [{ | ||
message: 'There is to many components in this file.' | ||
}] | ||
}, | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
Vue.component('TodoList', { | ||
// ... | ||
}) | ||
Vue.component('TodoItem', { | ||
// ... | ||
}) | ||
export default {} | ||
`, | ||
errors: [{ | ||
message: 'There is to many components in this file.' | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
Vue.component('name', {}) | ||
export default {} | ||
`, | ||
errors: [{ | ||
message: 'There is to many components in this file.' | ||
}] | ||
} | ||
] | ||
}) |