Skip to content

Fix: html-indent for solo comment (fixes #330) #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions lib/utils/indent-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,42 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
}
}

/**
* Get the expected indent of comments.
* @param {Token|null} nextToken The next token of comments.
* @param {number|undefined} nextExpectedIndent The expected indent of the next token.
* @param {number|undefined} lastExpectedIndent The expected indent of the last token.
* @returns {{primary:number|undefined,secondary:number|undefined}}
*/
function getCommentExpectedIndents (nextToken, nextExpectedIndent, lastExpectedIndent) {
if (typeof lastExpectedIndent === 'number' && isClosingToken(nextToken)) {
if (nextExpectedIndent === lastExpectedIndent) {
// For solo comment. E.g.,
// <div>
// <!-- comment -->
// </div>
return {
primary: nextExpectedIndent + options.indentSize,
secondary: undefined
}
}

// For last comment. E.g.,
// <div>
// <div></div>
// <!-- comment -->
// </div>
return { primary: lastExpectedIndent, secondary: nextExpectedIndent }
}

// Adjust to next normally. E.g.,
// <div>
// <!-- comment -->
// <div></div>
// </div>
return { primary: nextExpectedIndent, secondary: undefined }
}

/**
* Validate indentation of the line that the given tokens are on.
* @param {Token[]} tokens The tokens on the same line to validate.
Expand Down Expand Up @@ -732,9 +768,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
// It allows the same indent level with the previous line.
const lastOffsetInfo = offsets.get(lastToken)
const lastExpectedIndent = lastOffsetInfo && lastOffsetInfo.expectedIndent
const commentExpectedIndents = (typeof lastExpectedIndent === 'number' && isClosingToken(firstToken))
? { primary: lastExpectedIndent, secondary: expectedIndent }
: { primary: expectedIndent, secondary: undefined }
const commentExpectedIndents = getCommentExpectedIndents(firstToken, expectedIndent, lastExpectedIndent)

// Validate.
for (const comment of comments) {
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/html-indent/solo-html-comment-01.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!--{}-->
<template>
<div>
<!-- comment -->
</div>
</template>
22 changes: 22 additions & 0 deletions tests/lib/rules/html-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,28 @@ tester.run('html-indent', rule, loadPatterns(
}}
</template>
`,
unIndent`
<template>
<div>
<!-- this comment is ignored because the next token doesn't exist. -->
`,
unIndent`
<template>
<div>
<div></div>
<!-- this comment is ignored because the next token doesn't exist. -->
`,
unIndent`
<template>
<div>
<!-- this comment is ignored because the next token doesn't exist. -->
`,
unIndent`
<template>
<div>
<div></div>
<!-- this comment is ignored because the next token doesn't exist. -->
`,

// Ignores
{
Expand Down