-
-
Notifications
You must be signed in to change notification settings - Fork 680
Add vue/padding-line-between-component-options
rule
#1818
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
Closed
barthy-koeln
wants to merge
9
commits into
vuejs:master
from
barthy-koeln:rule/empty-line-between-options
Closed
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0c0ef9a
added rule/empty-line-between-options
ea082d5
added rule
af7d4e6
added docs
13c3f9a
implemented requested changes
f7819e6
valid docs
1b75edb
link rule in similar docs
41a2b32
correct code blocks in docs
36569a4
correct code blocks in docs (bis)
669788f
fix typo
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,121 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/empty-line-between-options | ||
description: enforce empty lines between top-level options | ||
--- | ||
# vue/empty-line-between-options | ||
|
||
> enforce empty lines between top-level 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 enforces consistent format of empty lines between Vue component options by either adding or removing them. | ||
|
||
<eslint-code-block fix :rules="{'vue/empty-line-between-options': ['error', 'always']}"> | ||
|
||
```vue | ||
<script> | ||
<!-- ✓ GOOD --> | ||
export default { | ||
name: 'a-button', | ||
|
||
/** | ||
* Ceci n'est pas un commentaire | ||
* @return {{}} | ||
*/ | ||
data() { | ||
return { | ||
// ... | ||
} | ||
}, | ||
|
||
computed: { | ||
// ... | ||
} | ||
} | ||
|
||
<!-- ✗ BAD --> | ||
export default { | ||
name: 'a-button', | ||
/** | ||
* Ceci n'est pas un commentaire | ||
* @return {{}} | ||
*/ | ||
data() { | ||
return { | ||
// ... | ||
} | ||
}, | ||
computed: { | ||
// ... | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block fix :rules="{'vue/empty-line-between-options': ['error', 'never']}"> | ||
|
||
```vue | ||
<script> | ||
<!-- ✓ GOOD --> | ||
export default { | ||
name: 'a-button', | ||
/** | ||
* Ceci n'est pas un commentaire | ||
* @return {{}} | ||
*/ | ||
data() { | ||
return { | ||
// ... | ||
} | ||
}, | ||
computed: { | ||
// ... | ||
} | ||
} | ||
|
||
<!-- ✗ BAD --> | ||
export default { | ||
name: 'a-button', | ||
|
||
/** | ||
* Ceci n'est pas un commentaire | ||
* @return {{}} | ||
*/ | ||
data() { | ||
return { | ||
// ... | ||
} | ||
}, | ||
|
||
computed: { | ||
// ... | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
|
||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/empty-line-between-options": ["error", "always" | "never"] | ||
} | ||
``` | ||
|
||
- `"always"` (default) ... add an empty line between options. | ||
- `"never"` ... remove empty lines between options. | ||
|
||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/empty-line-between-options.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/empty-line-between-options.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
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,141 @@ | ||
/** | ||
* @author Barthy Bonhomme <[email protected]> (https://github.com/barthy-koeln) | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
// ... | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: 'enforce empty lines between top-level options', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/empty-line-between-options.html' | ||
}, | ||
fixable: 'whitespace', | ||
schema: [{ enum: ['always', 'never'] }], | ||
messages: { | ||
never: 'Unexpected blank line between Vue component options.', | ||
always: 'Expected blank line between Vue component options.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const isVueFile = utils.isVueFile(context.getFilename()) | ||
if (!isVueFile) { | ||
return {} | ||
} | ||
|
||
const sourceCode = context.getSourceCode() | ||
const shouldPad = (context.options[0] || 'always') === 'always' | ||
|
||
const fixFunctions = { | ||
/** | ||
* Removes newlines between component options | ||
* | ||
* @param {RuleFixer} fixer | ||
* @param {number} endOfCurrent | ||
* @param {number} startOfNext | ||
* @return {Fix} | ||
*/ | ||
never(fixer, endOfCurrent, startOfNext) { | ||
return fixer.replaceTextRange([endOfCurrent, startOfNext], '\n') | ||
}, | ||
/** | ||
* Add newlines between component options. | ||
* | ||
* @param {RuleFixer} fixer | ||
* @param {number} endOfCurrent | ||
* @return {Fix} | ||
*/ | ||
always(fixer, endOfCurrent /*, startOfNext*/) { | ||
return fixer.insertTextAfterRange([0, endOfCurrent], '\n') | ||
} | ||
} | ||
|
||
/** | ||
* Report error based on configuration. | ||
* | ||
* @param {ASTNode} node Where to report errors | ||
* @param {boolean} isPadded True if the option is followed by an empty line | ||
* @param {number} endOfCurrent End of checked token | ||
* @param {number} startOfNext Start of next token | ||
*/ | ||
function reportError(node, isPadded, endOfCurrent, startOfNext) { | ||
const key = isPadded ? 'never' : 'always' | ||
const fixFunction = fixFunctions[key] | ||
|
||
context.report({ | ||
node, | ||
messageId: key, | ||
fix: (fixer) => fixFunction(fixer, endOfCurrent, startOfNext) | ||
}) | ||
} | ||
|
||
/** | ||
* Compares options and decides what to do. | ||
* This takes into account comments before options, but not empty lines between multiple comments. | ||
* | ||
* @param {ASTNode} current current option to check | ||
* @param {ASTNode} next next node to check against | ||
*/ | ||
function checkOption(current, next) { | ||
const endOfCurrent = | ||
sourceCode.getIndexFromLoc({ | ||
line: current.loc.end.line + 1, | ||
column: 0 | ||
}) - 1 /* start of next line, -1 for previous line */ | ||
|
||
const comments = sourceCode.getCommentsBefore(next) | ||
const nextNode = comments.length ? comments[0] : next | ||
|
||
const startOfNext = sourceCode.getIndexFromLoc({ | ||
line: nextNode.loc.start.line, | ||
column: 0 | ||
}) | ||
|
||
const isPadded = startOfNext !== endOfCurrent + 1 | ||
if (shouldPad === isPadded) { | ||
return | ||
} | ||
|
||
reportError(next, isPadded, endOfCurrent, startOfNext) | ||
} | ||
|
||
return { | ||
/** | ||
* @param {import('vue-eslint-parser/ast').Node} node | ||
*/ | ||
ObjectExpression(node) { | ||
if (node.parent && node.parent.type !== 'ExportDefaultDeclaration') { | ||
return | ||
} | ||
|
||
const { properties } = node | ||
|
||
for (let i = 0; i < properties.length - 1; i++) { | ||
const property = properties[i] | ||
const nextProperty = properties[i + 1] | ||
|
||
checkOption(property, nextProperty) | ||
} | ||
} | ||
} | ||
} | ||
} |
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.