Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 99e9607

Browse files
ota-meshimichalsnik
authored andcommitted
Feature: Add allowEmptyLines option to vue/multiline-html-element-content-newline rule (vuejs#759)
* Update: Add `arrowEmptyLine` option to `vue/multiline-html-element-content-newline` rule Close vuejs#683 * remove extra comma * fixed typo * fixed
1 parent 97a51b2 commit 99e9607

File tree

3 files changed

+146
-4
lines changed

3 files changed

+146
-4
lines changed

docs/rules/multiline-html-element-content-newline.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ This rule enforces a line break before and after the contents of a multiline ele
7878
{
7979
"vue/multiline-html-element-content-newline": ["error", {
8080
"ignoreWhenEmpty": true,
81-
"ignores": ["pre", "textarea"]
81+
"ignores": ["pre", "textarea"],
82+
"allowEmptyLines": false
8283
}]
8384
}
8485
```
@@ -87,6 +88,8 @@ This rule enforces a line break before and after the contents of a multiline ele
8788
default `true`
8889
- `ignores` ... the configuration for element names to ignore line breaks style.
8990
default `["pre", "textarea"]`
91+
- `allowEmptyLines` ... if `true`, it allows empty lines around content. If you want to disallow multiple empty lines, use [no-multiple-empty-lines] in combination.
92+
default `false`
9093

9194
### `"ignores": ["VueComponent", "pre", "textarea"]`
9295

@@ -109,6 +112,36 @@ This rule enforces a line break before and after the contents of a multiline ele
109112

110113
</eslint-code-block>
111114

115+
### `"allowEmptyLines": true`
116+
117+
<eslint-code-block fix :rules="{'vue/multiline-html-element-content-newline': ['error', { allowEmptyLines: true }]}">
118+
119+
```vue
120+
<template>
121+
<!-- ✓ GOOD -->
122+
<div>
123+
content
124+
</div>
125+
<div>
126+
127+
content
128+
129+
</div>
130+
131+
<!-- ✗ BAD -->
132+
<div>content
133+
content</div>
134+
</template>
135+
```
136+
137+
</eslint-code-block>
138+
139+
## :books: Further reading
140+
141+
- [no-multiple-empty-lines]
142+
143+
[no-multiple-empty-lines]: https://eslint.org/docs/rules/no-multiple-empty-lines
144+
112145
## :mag: Implementation
113146

114147
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/multiline-html-element-content-newline.js)

lib/rules/multiline-html-element-content-newline.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function isMultilineElement (element) {
2222
function parseOptions (options) {
2323
return Object.assign({
2424
ignores: ['pre', 'textarea'],
25-
ignoreWhenEmpty: true
25+
ignoreWhenEmpty: true,
26+
allowEmptyLines: false
2627
}, options)
2728
}
2829

@@ -69,6 +70,9 @@ module.exports = {
6970
items: { type: 'string' },
7071
uniqueItems: true,
7172
additionalItems: false
73+
},
74+
allowEmptyLines: {
75+
type: 'boolean'
7276
}
7377
},
7478
additionalProperties: false
@@ -83,6 +87,7 @@ module.exports = {
8387
const options = parseOptions(context.options[0])
8488
const ignores = options.ignores
8589
const ignoreWhenEmpty = options.ignoreWhenEmpty
90+
const allowEmptyLines = options.allowEmptyLines
8691
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
8792
const sourceCode = context.getSourceCode()
8893

@@ -94,6 +99,14 @@ module.exports = {
9499
ignores.includes(casing.kebabCase(node.rawName))
95100
}
96101

102+
function isInvalidLineBreaks (lineBreaks) {
103+
if (allowEmptyLines) {
104+
return lineBreaks === 0
105+
} else {
106+
return lineBreaks !== 1
107+
}
108+
}
109+
97110
return utils.defineTemplateBodyVisitor(context, {
98111
'VElement' (node) {
99112
if (inIgnoreElement) {
@@ -127,7 +140,7 @@ module.exports = {
127140

128141
const beforeLineBreaks = contentFirst.loc.start.line - node.startTag.loc.end.line
129142
const afterLineBreaks = node.endTag.loc.start.line - contentLast.loc.end.line
130-
if (beforeLineBreaks !== 1) {
143+
if (isInvalidLineBreaks(beforeLineBreaks)) {
131144
context.report({
132145
node: template.getLastToken(node.startTag),
133146
loc: {
@@ -150,7 +163,7 @@ module.exports = {
150163
return
151164
}
152165

153-
if (afterLineBreaks !== 1) {
166+
if (isInvalidLineBreaks(afterLineBreaks)) {
154167
context.report({
155168
node: template.getFirstToken(node.endTag),
156169
loc: {

tests/lib/rules/multiline-html-element-content-newline.js

+96
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,43 @@ tester.run('multiline-html-element-content-newline', rule, {
7272
class="panel">
7373
</div>
7474
</template>`,
75+
// allowEmptyLines
76+
{
77+
code: `
78+
<template>
79+
<div
80+
class="panel">
81+
82+
</div>
83+
</template>`,
84+
options: [{ allowEmptyLines: true, ignoreWhenEmpty: false }]
85+
},
86+
{
87+
code: `
88+
<template>
89+
<div
90+
class="panel">
91+
92+
contents
93+
94+
</div>
95+
</template>`,
96+
options: [{ allowEmptyLines: true }]
97+
},
98+
{
99+
code: `
100+
<template>
101+
<div
102+
class="panel">
103+
104+
105+
contents
106+
107+
108+
</div>
109+
</template>`,
110+
options: [{ allowEmptyLines: true }]
111+
},
75112
// self closing
76113
`
77114
<template>
@@ -439,6 +476,65 @@ content
439476
'Expected 1 line break before closing tag (`</div>`), but 2 line breaks found.'
440477
]
441478
},
479+
// allowEmptyLines
480+
{
481+
code: `
482+
<template>
483+
<div
484+
class="panel">
485+
486+
</div>
487+
<div
488+
class="panel"></div>
489+
</template>`,
490+
options: [{ allowEmptyLines: true, ignoreWhenEmpty: false }],
491+
output: `
492+
<template>
493+
<div
494+
class="panel">
495+
496+
</div>
497+
<div
498+
class="panel">
499+
</div>
500+
</template>`,
501+
errors: [
502+
'Expected 1 line break after opening tag (`<div>`), but no line breaks found.'
503+
]
504+
},
505+
{
506+
code: `
507+
<template>
508+
<div>
509+
510+
content
511+
content
512+
513+
</div>
514+
<div>content
515+
content</div>
516+
</template>
517+
`,
518+
options: [{ allowEmptyLines: true }],
519+
output: `
520+
<template>
521+
<div>
522+
523+
content
524+
content
525+
526+
</div>
527+
<div>
528+
content
529+
content
530+
</div>
531+
</template>
532+
`,
533+
errors: [
534+
'Expected 1 line break after opening tag (`<div>`), but no line breaks found.',
535+
'Expected 1 line break before closing tag (`</div>`), but no line breaks found.'
536+
]
537+
},
442538
// mustache
443539
{
444540
code: `

0 commit comments

Comments
 (0)