Skip to content

Commit bebb9b3

Browse files
authored
Add vue/block-tag-newline rule (#1328)
1 parent f4c21e5 commit bebb9b3

File tree

6 files changed

+787
-1
lines changed

6 files changed

+787
-1
lines changed

docs/rules/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,14 @@ For example:
281281
```json
282282
{
283283
"rules": {
284-
"vue/component-name-in-template-casing": "error"
284+
"vue/block-tag-newline": "error"
285285
}
286286
}
287287
```
288288

289289
| Rule ID | Description | |
290290
|:--------|:------------|:---|
291+
| [vue/block-tag-newline](./block-tag-newline.md) | enforce line breaks after opening and before closing block-level tags | :wrench: |
291292
| [vue/component-name-in-template-casing](./component-name-in-template-casing.md) | enforce specific casing for the component naming style in template | :wrench: |
292293
| [vue/html-comment-content-newline](./html-comment-content-newline.md) | enforce unified line brake in HTML comments | :wrench: |
293294
| [vue/html-comment-content-spacing](./html-comment-content-spacing.md) | enforce unified spacing in HTML comments | :wrench: |

docs/rules/block-tag-newline.md

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/block-tag-newline
5+
description: enforce line breaks after opening and before closing block-level tags
6+
---
7+
# vue/block-tag-newline
8+
> enforce line breaks after opening and before closing block-level tags
9+
10+
- :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.
11+
12+
## :book: Rule Details
13+
14+
This rule enforces a line break (or no line break) after opening and before closing block tags.
15+
16+
<eslint-code-block fix :rules="{'vue/block-tag-newline': ['error']}">
17+
18+
```vue
19+
<!-- ✓ GOOD -->
20+
<template><input></template>
21+
22+
<script>
23+
export default {}
24+
</script>
25+
```
26+
27+
</eslint-code-block>
28+
29+
<eslint-code-block fix :rules="{'vue/block-tag-newline': ['error']}">
30+
31+
```vue
32+
<!-- ✗ BAD -->
33+
<template>
34+
<input></template>
35+
36+
<script>
37+
export default {}</script>
38+
```
39+
40+
</eslint-code-block>
41+
42+
## :wrench: Options
43+
44+
```json
45+
{
46+
"vue/block-tag-newline": ["error", {
47+
"singleline": "always" | "never" | "consistent" | "ignore",
48+
"multiline": "always" | "never" | "consistent" | "ignore",
49+
"maxEmptyLines": 0,
50+
"blocks": {
51+
"template": {
52+
"singleline": "always" | "never" | "consistent" | "ignore",
53+
"multiline": "always" | "never" | "consistent" | "ignore",
54+
"maxEmptyLines": 0,
55+
},
56+
"script": {
57+
"singleline": "always" | "never" | "consistent" | "ignore",
58+
"multiline": "always" | "never" | "consistent" | "ignore",
59+
"maxEmptyLines": 0,
60+
},
61+
"my-block": {
62+
"singleline": "always" | "never" | "consistent" | "ignore",
63+
"multiline": "always" | "never" | "consistent" | "ignore",
64+
"maxEmptyLines": 0,
65+
}
66+
}
67+
}]
68+
}
69+
```
70+
71+
- `singleline` ... the configuration for single-line blocks.
72+
- `"consistent"` ... (default) requires consistent usage of line breaks for each pair of tags. It reports an error if one tag in the pair has a linebreak inside it and the other tag does not.
73+
- `"always"` ... require one line break after opening and before closing block tags.
74+
- `"never"` ... disallow line breaks after opening and before closing block tags.
75+
- `multiline` ... the configuration for multi-line blocks.
76+
- `"consistent"` ... requires consistent usage of line breaks for each pair of tags. It reports an error if one tag in the pair has a linebreak inside it and the other tag does not.
77+
- `"always"` ... (default) require one line break after opening and before closing block tags.
78+
- `"never"` ... disallow line breaks after opening and before closing block tags.
79+
- `maxEmptyLines` ... specifies the maximum number of empty lines allowed. default 0.
80+
- `blocks` ... specifies for each block name.
81+
82+
### `{ "singleline": "never", "multiline": "always" }`
83+
84+
<eslint-code-block fix :rules="{'vue/block-tag-newline': ['error', { 'singleline': 'never', 'multiline': 'always' }]}">
85+
86+
```vue
87+
<!-- ✓ GOOD -->
88+
<template><input></template>
89+
90+
<script>
91+
export default {
92+
}
93+
</script>
94+
```
95+
96+
</eslint-code-block>
97+
98+
<eslint-code-block fix :rules="{'vue/block-tag-newline': ['error', { 'singleline': 'never', 'multiline': 'always' }]}">
99+
100+
```vue
101+
<!-- ✗ BAD -->
102+
<template>
103+
<input>
104+
</template>
105+
106+
<script>export default {
107+
}</script>
108+
```
109+
110+
</eslint-code-block>
111+
112+
### `{ "singleline": "always", "multiline": "always", "maxEmptyLines": 1 }`
113+
114+
<eslint-code-block fix :rules="{'vue/block-tag-newline': ['error', { 'singleline': 'always', 'multiline': 'always', 'maxEmptyLines': 1 }]}">
115+
116+
```vue
117+
<!-- ✓ GOOD -->
118+
<template>
119+
120+
<input>
121+
122+
</template>
123+
124+
<script>
125+
126+
export default {
127+
}
128+
129+
</script>
130+
```
131+
132+
</eslint-code-block>
133+
134+
<eslint-code-block fix :rules="{'vue/block-tag-newline': ['error', { 'singleline': 'always', 'multiline': 'always', 'maxEmptyLines': 1 }]}">
135+
136+
```vue
137+
<!-- ✗ BAD -->
138+
<template>
139+
140+
141+
<input>
142+
143+
144+
</template>
145+
146+
<script>
147+
148+
149+
export default {
150+
}
151+
152+
153+
</script>
154+
```
155+
156+
</eslint-code-block>
157+
158+
## :mag: Implementation
159+
160+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/block-tag-newline.js)
161+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/block-tag-newline.js)

lib/configs/no-layout-rules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
'vue/array-bracket-spacing': 'off',
99
'vue/arrow-spacing': 'off',
1010
'vue/block-spacing': 'off',
11+
'vue/block-tag-newline': 'off',
1112
'vue/brace-style': 'off',
1213
'vue/comma-dangle': 'off',
1314
'vue/comma-spacing': 'off',

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
'attribute-hyphenation': require('./rules/attribute-hyphenation'),
1313
'attributes-order': require('./rules/attributes-order'),
1414
'block-spacing': require('./rules/block-spacing'),
15+
'block-tag-newline': require('./rules/block-tag-newline'),
1516
'brace-style': require('./rules/brace-style'),
1617
camelcase: require('./rules/camelcase'),
1718
'comma-dangle': require('./rules/comma-dangle'),

0 commit comments

Comments
 (0)