From c17799f1ca2f20eae365ffb6801a515551316095 Mon Sep 17 00:00:00 2001 From: Toru Nagashima Date: Sun, 7 Jan 2018 23:45:50 +0900 Subject: [PATCH] Fix: html-indent for solo comment (fixes #330) --- lib/utils/indent-common.js | 40 +++++++++++++++++-- .../html-indent/solo-html-comment-01.vue | 6 +++ tests/lib/rules/html-indent.js | 22 ++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/html-indent/solo-html-comment-01.vue diff --git a/lib/utils/indent-common.js b/lib/utils/indent-common.js index de81799b4..1d02ba11b 100644 --- a/lib/utils/indent-common.js +++ b/lib/utils/indent-common.js @@ -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., + //
+ // + //
+ return { + primary: nextExpectedIndent + options.indentSize, + secondary: undefined + } + } + + // For last comment. E.g., + //
+ //
+ // + //
+ return { primary: lastExpectedIndent, secondary: nextExpectedIndent } + } + + // Adjust to next normally. E.g., + //
+ // + //
+ //
+ 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. @@ -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) { diff --git a/tests/fixtures/html-indent/solo-html-comment-01.vue b/tests/fixtures/html-indent/solo-html-comment-01.vue new file mode 100644 index 000000000..4582b2754 --- /dev/null +++ b/tests/fixtures/html-indent/solo-html-comment-01.vue @@ -0,0 +1,6 @@ + + diff --git a/tests/lib/rules/html-indent.js b/tests/lib/rules/html-indent.js index 18ce90748..e97aae5f2 100644 --- a/tests/lib/rules/html-indent.js +++ b/tests/lib/rules/html-indent.js @@ -191,6 +191,28 @@ tester.run('html-indent', rule, loadPatterns( }} `, + unIndent` +