Skip to content

Commit a46fc7b

Browse files
author
David Lomas
committed
Add option to treat comments as empty space when enforcing rules
1 parent 7502cf8 commit a46fc7b

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ function parseOptions(options) {
2323
{
2424
ignores: ['pre', 'textarea', ...INLINE_ELEMENTS],
2525
ignoreWhenEmpty: true,
26-
allowEmptyLines: false
26+
allowEmptyLines: false,
27+
ignoreComments: false
2728
},
2829
options
2930
)
@@ -80,7 +81,10 @@ module.exports = {
8081
},
8182
allowEmptyLines: {
8283
type: 'boolean'
83-
}
84+
},
85+
ignoreComments: {
86+
type: 'boolean'
87+
},
8488
},
8589
additionalProperties: false
8690
}
@@ -98,6 +102,7 @@ module.exports = {
98102
const ignores = options.ignores
99103
const ignoreWhenEmpty = options.ignoreWhenEmpty
100104
const allowEmptyLines = options.allowEmptyLines
105+
const ignoreComments = options.ignoreComments
101106
const sourceCode = context.getSourceCode()
102107
const template =
103108
sourceCode.parserServices.getTemplateBodyTokenStore &&
@@ -149,7 +154,7 @@ module.exports = {
149154
* @type {SourceCode.CursorWithCountOptions}
150155
*/
151156
const getTokenOption = {
152-
includeComments: true,
157+
includeComments: !ignoreComments,
153158
filter: (token) => token.type !== 'HTMLWhitespace'
154159
}
155160
if (

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ tester.run('multiline-html-element-content-newline', rule, {
170170
options: [{ allowEmptyLines: true }]
171171
},
172172

173+
{
174+
code: `
175+
<template>
176+
<div> <!-- comment -->
177+
contents
178+
</div>
179+
</template>`,
180+
options: [{ allowEmptyLines: true, ignoreComments: true }]
181+
},
182+
173183
// self closing
174184
`
175185
<template>
@@ -611,6 +621,25 @@ content
611621
'Expected 1 line break before closing tag (`</div>`), but no line breaks found.'
612622
]
613623
},
624+
{
625+
code: `
626+
<template>
627+
<div> <!-- comment -->
628+
contents
629+
</div>
630+
</template>`,
631+
output: `
632+
<template>
633+
<div>
634+
<!-- comment -->
635+
contents
636+
</div>
637+
</template>`,
638+
options: [{ allowEmptyLines: true, ignoreComments: false }],
639+
errors: [
640+
'Expected 1 line break after opening tag (`<div>`), but no line breaks found.'
641+
]
642+
},
614643
// mustache
615644
{
616645
code: `

0 commit comments

Comments
 (0)