8000 Fix: html-indent for solo comment (fixes #330) (#333) · vuejs/eslint-plugin-vue@9f83c5a · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

8000
Appearance settings

Commit 9f83c5a

Browse files
mysticateamichalsnik
authored andcommitted
Fix: html-indent for solo comment (fixes #330) (#333)
1 parent 784925b commit 9f83c5a

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

lib/utils/indent-common.js

Lines changed: 37 additions & 3 deletions
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) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--{}-->
2+
<template>
3+
<div>
4+
<!-- comment -->
5+
</div>
6+
</template>

tests/lib/rules/html-indent.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,28 @@ tester.run('html-indent', rule, loadPatterns(
191191
}}
192192
</template>
193193
`,
194+
unIndent`
195+
<template>
196+
<div>
197+
<!-- this comment is ignored because the next token doesn't exist. -->
198+
`,
199+
unIndent`
200+
<template>
201+
<div>
202+
<div></div>
203+
<!-- this comment is ignored because the next token doesn't exist. -->
204+
`,
205+
unIndent`
206+
<template>
207+
<div>
208+
<!-- this comment is ignored because the next token doesn't exist. -->
209+
`,
210+
unIndent`
211+
<template>
212+
<div>
213+
<div></div>
214+
<!-- this comment is ignored because the next token doesn't exist. -->
215+
`,
194216

195217
// Ignores
196218
{

0 commit comments

Comments
 (0)
0