Skip to content
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

Create padding-line-between-tags rule #1966

Merged
merged 44 commits into from Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
2ba27ac
Create space-between-siblings rule
dev1437 Sep 10, 2022
b530e82
Fix lint
dev1437 Sep 10, 2022
36b4c2d
Change how options are initialised
dev1437 Sep 10, 2022
3567a1b
Fill in name
dev1437 Sep 10, 2022
2469a38
Remove block
dev1437 Sep 10, 2022
a597c4c
Update test
dev1437 Sep 10, 2022
aa94054
Tidy up test and examples
dev1437 Sep 10, 2022
dcc83e2
Change message
dev1437 Sep 10, 2022
07252a0
Add test for flat tag
dev1437 Sep 10, 2022
bb6f4f2
Add never functionality
dev1437 Sep 10, 2022
abbae10
Add tests for never and update previous tests for new schema
dev1437 Sep 10, 2022
4589a3d
Linting
dev1437 Sep 10, 2022
52fbe43
Update docs
dev1437 Sep 10, 2022
d417713
Rename to padding-line-between-tags
dev1437 Sep 10, 2022
74abc89
Change schema to array of objects
dev1437 Sep 11, 2022
84f793c
Change messages
dev1437 Sep 11, 2022
fb1899a
Allow for blank lines to be specified on each tag
dev1437 Sep 11, 2022
acbf506
Update tests
dev1437 Sep 11, 2022
4f84411
Linting
dev1437 Sep 11, 2022
adc23c0
Update docs
dev1437 Sep 11, 2022
3b885b0
Add another test
dev1437 Sep 11, 2022
a04b8a3
Lint
dev1437 Sep 11, 2022
6b0620d
Clean up doc
dev1437 Sep 11, 2022
e210bc9
Clean up tests
dev1437 Sep 11, 2022
95c563a
Change type
dev1437 Sep 11, 2022
9c57ee5
Fix doc
dev1437 Sep 11, 2022
bd9c128
Update docs/rules/padding-line-between-tags.md
dev1437 Sep 11, 2022
38fccaa
Ignore top level
dev1437 Sep 12, 2022
d17ec54
Remove testing stuff
dev1437 Sep 12, 2022
cfa916f
Simplify logic and make last configuration apply
dev1437 Sep 12, 2022
e152ceb
Add test for last configuration applying
dev1437 Sep 12, 2022
40be55c
Update docs
dev1437 Sep 12, 2022
b5fd572
Add newlines between siblings on same line
dev1437 Sep 12, 2022
e6094bd
Update docs
dev1437 Sep 12, 2022
8f9fb7f
Merge branch 'rule/space-between-siblings' of github.com:dev1437/esli…
dev1437 Sep 12, 2022
261ed4d
Lint
dev1437 Sep 12, 2022
5688564
Fix doc
dev1437 Sep 13, 2022
1c621c4
Fix spaces on line diff = 0
dev1437 Sep 13, 2022
a3f314f
Remove only space between tags
dev1437 Sep 13, 2022
b94e599
Append text backwards
dev1437 Sep 13, 2022
8bbbeed
Uncomment tests
dev1437 Sep 13, 2022
93ea378
Linting
dev1437 Sep 13, 2022
19d365b
Fix loop and add test
dev1437 Sep 15, 2022
b05e3ea
Add another test
dev1437 Sep 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/README.md
Expand Up @@ -257,6 +257,7 @@ For example:
| [vue/require-name-property](./require-name-property.md) | require a name property in Vue components | | :hammer: |
| [vue/script-indent](./script-indent.md) | enforce consistent indentation in `<script>` | :wrench: | :lipstick: |
| [vue/sort-keys](./sort-keys.md) | enforce sort-keys in a manner that is compatible with order-in-components | | :hammer: |
| [vue/space-between-siblings](./space-between-siblings.md) | Insert newlines between sibling tags in template | :wrench: | :warning: |
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved
| [vue/static-class-names-order](./static-class-names-order.md) | enforce static class names order | :wrench: | :hammer: |
| [vue/v-for-delimiter-style](./v-for-delimiter-style.md) | enforce `v-for` directive's delimiter style | :wrench: | :lipstick: |
| [vue/v-on-function-call](./v-on-function-call.md) | enforce or forbid parentheses after method calls without arguments in `v-on` directives | :wrench: | :hammer: |
Expand Down
123 changes: 123 additions & 0 deletions docs/rules/space-between-siblings.md
@@ -0,0 +1,123 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/space-between-siblings
description: Insert newlines between sibling tags in template
---
# vue/space-between-siblings

> Insert newlines between sibling tags in template

- :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 requires newlines between sibling HTML tags

<eslint-code-block fix :rules="{'vue/space-between-siblings': ['error']}">

```vue
<!-- ✓ GOOD -->
<template>
<div>
<ul>
<li>
</li>

<li>
</li>

<li>
</li>
</ul>
</div>
</template>
```

```vue
<!-- ✗ BAD -->
<template>
<div>
<ul>
<li>
</li>
<li>
</li>
<li>
</li>
</ul>
</div>
</template>
```

</eslint-code-block>

## :wrench: Options

```json
{
"vue/space-between-siblings": ["error", {
"ignoreNewlinesBefore": [],
"ignoreNewlinesAfter": []
}]
}
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved
```

- `ignoreNewlinesBefore` ignores newlines before specified elements.
default `[]`
- `ignoreNewlinesAfter` ignores newlines after specified elements.
default `[]`

### `"ignoreNewlinesBefore": ["br"]`

<eslint-code-block fix :rules="{'vue/space-between-siblings': ['error', { ignoreNewlinesBefore: ['br'] }]}">

```vue
<template>
<div>
<ul>
<li>
</li>

<li>
</li>
<br />

<li>
</li>
</ul>
</div>
</template>
```

</eslint-code-block>

### `"ignoreNewlinesAfter": ["br"]`

<eslint-code-block fix :rules="{'vue/space-between-siblings': ['error', { ignoreNewlinesAfter: ['br'] }]}">

```vue
<template>
<div>
<ul>
<li>
</li>

<li>
</li>

<br />
<li>
</li>
</ul>
</div>
</template>
```

</eslint-code-block>

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/space-between-siblings.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/space-between-siblings.js)
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -183,6 +183,7 @@ module.exports = {
'script-setup-uses-vars': require('./rules/script-setup-uses-vars'),
'singleline-html-element-content-newline': require('./rules/singleline-html-element-content-newline'),
'sort-keys': require('./rules/sort-keys'),
'space-between-siblings': require('./rules/space-between-siblings'),
'space-in-parens': require('./rules/space-in-parens'),
'space-infix-ops': require('./rules/space-infix-ops'),
'space-unary-ops': require('./rules/space-unary-ops'),
Expand Down
123 changes: 123 additions & 0 deletions lib/rules/space-between-siblings.js
@@ -0,0 +1,123 @@
/**
* @author *****your name*****
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved
* See LICENSE file in root directory for full license.
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const utils = require('../utils')

// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------

// ...

// ------------------------------------------------------------------------------
// Rule Definition
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved
// ------------------------------------------------------------------------------

/**
* @param {RuleContext} context
*/
function checkNewline(context) {
/** @type {string[]} */
let ignoreNewlinesAfter = []
/** @type {string[]} */
let ignoreNewlinesBefore = []

if (context.options.length > 0) {
const options = context.options[0]
ignoreNewlinesAfter = options.ignoreNewlinesAfter ?? []
ignoreNewlinesBefore = options.ignoreNewlinesBefore ?? []
}

/**
* @param {VElement} block
*/
return (block) => {
if (ignoreNewlinesAfter.includes(block.name)) {
return
}

const endTag = block.endTag || block.startTag
const lowerSiblings = block.parent.children
.filter(
(element) =>
element.type === 'VElement' && element.range !== block.range
)
.filter((sibling) => sibling.range[0] - endTag.range[1] > 0)

if (lowerSiblings.length === 0) {
return
}

let closestSibling = lowerSiblings[0]

for (const sibling of lowerSiblings) {
const diff = sibling.range[0] - endTag.range[1]
if (diff < closestSibling.range[0] - endTag.range[1]) {
closestSibling = sibling
}
}

if (closestSibling.loc.start.line === endTag.loc.end.line + 1) {
context.report({
messageId: 'always',
loc: closestSibling.loc,
// @ts-ignore
fix(fixer) {
const element = /** @type {VElement} */ (closestSibling)
if (!ignoreNewlinesBefore.includes(element.name)) {
return fixer.insertTextAfter(block, '\n')
}
}
})
}
}
}

module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require newlines between sibling tags in template',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/space-between-siblings.html'
},
fixable: 'whitespace',
schema: [
{
type: 'object',
properties: {
ignoreNewlinesBefore: {
type: 'array',
items: { type: 'string' },
uniqueItems: true,
additionalItems: false
},
ignoreNewlinesAfter: {
type: 'array',
items: { type: 'string' },
uniqueItems: true,
additionalItems: false
}
},
additionalProperties: false
}
],
messages: {
never: 'Unexpected blank line before this block.',
always: 'Expected blank line after this block.'
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved
}
},
/** @param {RuleContext} context */
create(context) {
return utils.defineTemplateBodyVisitor(context, {
VElement: checkNewline(context)
})
}
}