Skip to content

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
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ For example:
| [vue/component-name-in-template-casing](./component-name-in-template-casing.md) | enforce specific casing for the component naming style in template | :wrench: |
| [vue/component-options-name-casing](./component-options-name-casing.md) | enforce the casing of component name in `components` options | :wrench::bulb: |
| [vue/custom-event-name-casing](./custom-event-name-casing.md) | enforce specific casing for custom event name | |
| [vue/empty-line-between-options](./empty-line-between-options.md) | enforce empty lines between top-level options | :wrench: |
| [vue/html-button-has-type](./html-button-has-type.md) | disallow usage of button without an explicit type attribute | |
| [vue/html-comment-content-newline](./html-comment-content-newline.md) | enforce unified line brake in HTML comments | :wrench: |
| [vue/html-comment-content-spacing](./html-comment-content-spacing.md) | enforce unified spacing in HTML comments | :wrench: |
Expand Down
121 changes: 121 additions & 0 deletions docs/rules/empty-line-between-options.md
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>


## :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.

## :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)
1 change: 1 addition & 0 deletions lib/configs/no-layout-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
'vue/comma-spacing': 'off',
'vue/comma-style': 'off',
'vue/dot-location': 'off',
'vue/empty-line-between-options': 'off',
'vue/first-attribute-linebreak': 'off',
'vue/func-call-spacing': 'off',
'vue/html-closing-bracket-newline': 'off',
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
'custom-event-name-casing': require('./rules/custom-event-name-casing'),
'dot-location': require('./rules/dot-location'),
'dot-notation': require('./rules/dot-notation'),
'empty-line-between-options': require('./rules/empty-line-between-options'),
eqeqeq: require('./rules/eqeqeq'),
'experimental-script-setup-vars': require('./rules/experimental-script-setup-vars'),
'first-attribute-linebreak': require('./rules/first-attribute-linebreak'),
Expand Down
141 changes: 141 additions & 0 deletions lib/rules/empty-line-between-options.js
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
// ------------------------------------------------------------------------------

// ...

// ------------------------------------------------------------------------------
// 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)
}
}
}
}
}
Loading