Skip to content

Commit 537bd4c

Browse files
committed
Fix: html-indent for solo comment (fixes #330)
1 parent 26e697d commit 537bd4c

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

Diff for: lib/utils/indent-common.js

+37-3
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,42 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
672672
}
673673
}
674674

675+
/**
676+
* Get the expected indent of comments.
677+
* @param {Token|null} nextToken The next token of comments.
678+
* @param {number|undefined} nextExpectedIndent The expected indent of the next token.
679+
* @param {number|undefined} lastExpectedIndent The expected indent of the last token.
680+
* @returns {{primary:number|undefined,secondary:number|undefined}}
681+
*/
682+
function getCommentExpectedIndents (nextToken, nextExpectedIndent, lastExpectedIndent) {
683+
if (typeof lastExpectedIndent === 'number' && isClosingToken(nextToken)) {
684+
if (nextExpectedIndent === lastExpectedIndent) {
685+
// For solo comment. E.g.,
686+
// <div>
687+
// <!-- comment -->
688+
// </div>
689+
return {
690+
primary: nextExpectedIndent + options.indentSize,
691+
secondary: undefined
692+
}
693+
}
694+
695+
// For last comment. E.g.,
696+
// <div>
697+
// <div></div>
698+
// <!-- comment -->
699+
// </div>
700+
return { primary: lastExpectedIndent, secondary: nextExpectedIndent }
701+
}
702+
703+
// Adjust to next normally. E.g.,
704+
// <div>
705+
// <!-- comment -->
706+
// <div></div>
707+
// </div>
708+
return { primary: nextExpectedIndent, secondary: undefined }
709+
}
710+
675711
/**
676712
* Validate indentation of the line that the given tokens are on.
677713
* @param {Token[]} tokens The tokens on the same line to validate.
@@ -732,9 +768,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
732768
// It allows the same indent level with the previous line.
733769
const lastOffsetInfo = offsets.get(lastToken)
734770
const lastExpectedIndent = lastOffsetInfo && lastOffsetInfo.expectedIndent
735-
const commentExpectedIndents = (typeof lastExpectedIndent === 'number' && isClosingToken(firstToken))
736-
? { primary: lastExpectedIndent, secondary: expectedIndent }
737-
: { primary: expectedIndent, secondary: undefined }
771+
const commentExpectedIndents = getCommentExpectedIndents(firstToken, expectedIndent, lastExpectedIndent)
738772

739773
// Validate.
740774
for (const comment of comments) {

Diff for: tests/fixtures/html-indent/solo-html-comment-01.vue

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--{}-->
2+
<template>
3+
<div>
4+
<!-- comment -->
5+
</div>
6+
</template>

Diff for: tests/lib/rules/html-indent.js

+22
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,28 @@ tester.run('html-indent', rule, loadPatterns(
182182
}}
183183
</template>
184184
`,
185+
unIndent`
186+
<template>
187+
<div>
188+
<!-- this comment is ignored because the next token doesn't exist. -->
189+
`,
190+
unIndent`
191+
<template>
192+
<div>
193+
<div></div>
194+
<!-- this comment is ignored because the next token doesn't exist. -->
195+
`,
196+
unIndent`
197+
<template>
198+
<div>
199+
<!-- this comment is ignored because the next token doesn't exist. -->
200+
`,
201+
unIndent`
202+
<template>
203+
<div>
204+
<div></div>
205+
<!-- this comment is ignored because the next token doesn't exist. -->
206+
`,
185207

186208
// Ignores
187209
{

0 commit comments

Comments
 (0)