Skip to content

Commit bd995fd

Browse files
armano2michalsnik
andcommitted
Add ignoreWhenEmpty options to singleline/multiline-html-element-content-newline (#684)
* Add `ignoreWhenEmpty` options to multiline-html-element-content-newline and singleline-html-element-content-newline * add sugestions from code review Co-Authored-By: Michał Sajnóg <[email protected]>
1 parent ab3ed7a commit bd995fd

6 files changed

+62
-9
lines changed

Diff for: docs/rules/multiline-html-element-content-newline.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,15 @@ This rule enforces a line break before and after the contents of a multiline ele
7474

7575
```json
7676
{
77-
"vue/multiline-html-element-content-newline": ["error", {
78-
"ignores": ["pre", "textarea"]
79-
}]
77+
"vue/multiline-html-element-content-newline": ["error", {
78+
"ignoreWhenEmpty": true,
79+
"ignores": ["pre", "textarea"]
80+
}]
8081
}
8182
```
8283

84+
- `ignoreWhenEmpty` ... disables reporting when element has no content.
85+
default `true`
8386
- `ignores` ... the configuration for element names to ignore line breaks style.
8487
default `["pre", "textarea"]`
8588

Diff for: docs/rules/singleline-html-element-content-newline.md

+3
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ This rule enforces a line break before and after the contents of a singleline el
5252
{
5353
"vue/singleline-html-element-content-newline": ["error", {
5454
"ignoreWhenNoAttributes": true,
55+
"ignoreWhenEmpty": true,
5556
"ignores": ["pre", "textarea"]
5657
}]
5758
}
5859
```
5960

6061
- `ignoreWhenNoAttributes` ... allows having contents in one line, when given element has no attributes.
6162
default `true`
63+
- `ignoreWhenEmpty` ... disables reporting when element has no content.
64+
default `true`
6265
- `ignores` ... the configuration for element names to ignore line breaks style.
6366
default `["pre", "textarea"]`
6467

Diff for: lib/rules/multiline-html-element-content-newline.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ function isMultilineElement (element) {
2121

2222
function parseOptions (options) {
2323
return Object.assign({
24-
ignores: ['pre', 'textarea']
24+
ignores: ['pre', 'textarea'],
25+
ignoreWhenEmpty: true
2526
}, options)
2627
}
2728

@@ -60,7 +61,10 @@ module.exports = {
6061
schema: [{
6162
type: 'object',
6263
properties: {
63-
'ignores': {
64+
ignoreWhenEmpty: {
65+
type: 'boolean'
66+
},
67+
ignores: {
6468
type: 'array',
6569
items: { type: 'string' },
6670
uniqueItems: true,
@@ -76,7 +80,9 @@ module.exports = {
7680
},
7781

7882
create (context) {
79-
const ignores = parseOptions(context.options[0]).ignores
83+
const options = parseOptions(context.options[0])
84+
const ignores = options.ignores
85+
const ignoreWhenEmpty = options.ignoreWhenEmpty
8086
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
8187
const sourceCode = context.getSourceCode()
8288

@@ -108,6 +114,14 @@ module.exports = {
108114
}
109115

110116
const getTokenOption = { includeComments: true, filter: (token) => token.type !== 'HTMLWhitespace' }
117+
if (
118+
ignoreWhenEmpty &&
119+
node.children.length === 0 &&
120+
template.getFirstTokensBetween(node.startTag, node.endTag, getTokenOption).length === 0
121+
) {
122+
return
123+
}
124+
111125
const contentFirst = template.getTokenAfter(node.startTag, getTokenOption)
112126
const contentLast = template.getTokenBefore(node.endTag, getTokenOption)
113127

Diff for: lib/rules/singleline-html-element-content-newline.js

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

@@ -58,6 +59,9 @@ module.exports = {
5859
ignoreWhenNoAttributes: {
5960
type: 'boolean'
6061
},
62+
ignoreWhenEmpty: {
63+
type: 'boolean'
64+
},
6165
ignores: {
6266
type: 'array',
6367
items: { type: 'string' },
@@ -77,6 +81,7 @@ module.exports = {
7781
const options = parseOptions(context.options[0])
7882
const ignores = options.ignores
7983
const ignoreWhenNoAttributes = options.ignoreWhenNoAttributes
84+
const ignoreWhenEmpty = options.ignoreWhenEmpty
8085
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
8186
const sourceCode = context.getSourceCode()
8287

@@ -111,6 +116,14 @@ module.exports = {
111116
}
112117

113118
const getTokenOption = { includeComments: true, filter: (token) => token.type !== 'HTMLWhitespace' }
119+
if (
120+
ignoreWhenEmpty &&
121+
node.children.length === 0 &&
122+
template.getFirstTokensBetween(node.startTag, node.endTag, getTokenOption).length === 0
123+
) {
124+
return
125+
}
126+
114127
const contentFirst = template.getTokenAfter(node.startTag, getTokenOption)
115128
const contentLast = template.getTokenBefore(node.endTag, getTokenOption)
116129

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

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ tester.run('multiline-html-element-content-newline', rule, {
2626
`<template><div class="panel">content</div></template>`,
2727
`<template><div class="panel"><div></div></div></template>`,
2828
`<template><div class="panel"><!-- comment --></div></template>`,
29+
`
30+
<template>
31+
<slot
32+
name="panel"
33+
></slot>
34+
</template>
35+
`,
36+
`
37+
<template>
38+
<div
39+
></div>
40+
</template>
41+
`,
2942
`
3043
<template>
3144
<div class="panel">
@@ -525,6 +538,7 @@ content
525538
</div>
526539
</template>
527540
`,
541+
options: [{ ignoreWhenEmpty: false }],
528542
errors: ['Expected 1 line break after opening tag (`<div>`), but no line breaks found.']
529543
}
530544
]

Diff for: tests/lib/rules/singleline-html-element-content-newline.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const tester = new RuleTester({
2323

2424
tester.run('singleline-html-element-content-newline', rule, {
2525
valid: [
26+
`
27+
<template>
28+
<slot name="panel"></slot>
29+
</template>
30+
`,
2631
`
2732
<template>
2833
<div class="panel">
@@ -319,6 +324,7 @@ content
319324
</div>
320325
</template>
321326
`,
327+
options: [{ ignoreWhenEmpty: false }],
322328
errors: [
323329
'Expected 1 line break after opening tag (`<div>`), but no line breaks found.'
324330
]
@@ -429,7 +435,7 @@ singleline element
429435
<div></div>
430436
</template>
431437
`,
432-
options: [{ ignoreWhenNoAttributes: false }],
438+
options: [{ ignoreWhenEmpty: false, ignoreWhenNoAttributes: false }],
433439
output: `
434440
<template>
435441
<div>
@@ -446,7 +452,7 @@ singleline element
446452
<div> </div>
447453
</template>
448454
`,
449-
options: [{ ignoreWhenNoAttributes: false }],
455+
options: [{ ignoreWhenEmpty: false, ignoreWhenNoAttributes: false }],
450456
output: `
451457
<template>
452458
<div>

0 commit comments

Comments
 (0)