Skip to content

Commit e6593a5

Browse files
mysticateamichalsnik
authored andcommitted
Fix: html-indent about comments (fixes #241) (#280)
1 parent cc43821 commit e6593a5

File tree

2 files changed

+135
-6
lines changed

2 files changed

+135
-6
lines changed

lib/rules/html-indent.js

+38-6
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,26 @@ function isBeginningOfLine (node, index, nodes) {
198198
return false
199199
}
200200

201+
/**
202+
* Check whether a given token is a closing token which triggers unindent.
203+
* @param {Token} token The token to check.
204+
* @returns {boolean} `true` if the token is a closing token.
205+
*/
206+
function isClosingToken (token) {
207+
return token != null && (
208+
token.type === 'HTMLEndTagOpen' ||
209+
token.type === 'VExpressionEnd' ||
210+
(
211+
token.type === 'Punctuator' &&
212+
(
213+
token.value === ')' ||
214+
token.value === '}' ||
215+
token.value === ']'
216+
)
217+
)
218+
)
219+
}
220+
201221
/**
202222
* Creates AST event handlers for html-indent.
203223
*
@@ -504,9 +524,10 @@ function create (context) {
504524
* Validate the given token with the pre-calculated expected indentation.
505525
* @param {Token} token The token to validate.
506526
* @param {number} expectedIndent The expected indentation.
527+
* @param {number|undefined} optionalExpectedIndent The optional expected indentation.
507528
* @returns {void}
508529
*/
509-
function validateCore (token, expectedIndent) {
530+
function validateCore (token, expectedIndent, optionalExpectedIndent) {
510531
const line = token.loc.start.line
511532
const actualIndent = token.loc.start.column
512533
const indentText = getIndentText(token)
@@ -530,7 +551,7 @@ function create (context) {
530551
}
531552
}
532553

533-
if (actualIndent !== expectedIndent) {
554+
if (actualIndent !== expectedIndent && (optionalExpectedIndent === undefined || actualIndent !== optionalExpectedIndent)) {
534555
context.report({
535556
loc: {
536557
start: { line, column: 0 },
@@ -553,9 +574,10 @@ function create (context) {
553574
* Validate indentation of the line that the given tokens are on.
554575
* @param {Token[]} tokens The tokens on the same line to validate.
555576
* @param {Token[]} comments The comments which are on the immediately previous lines of the tokens.
577+
* @param {Token|null} lastToken The last validated token. Comments can adjust to the token.
556578
* @returns {void}
557579
*/
558-
function validate (tokens, comments) {
580+
function validate (tokens, comments, lastToken) {
559581
// Calculate and save expected indentation.
560582
const firstToken = tokens[0]
561583
const actualIndent = firstToken.loc.start.column
@@ -604,9 +626,17 @@ function create (context) {
604626
}
605627
}
606628

629+
// Calculate the expected indents for comments.
630+
// It allows the same indent level with the previous line.
631+
const lastOffsetInfo = offsets.get(lastToken)
632+
const lastExpectedIndent = lastOffsetInfo && lastOffsetInfo.expectedIndent
633+
const commentExpectedIndents = (typeof lastExpectedIndent === 'number' && isClosingToken(firstToken))
634+
? { primary: lastExpectedIndent, secondary: expectedIndent }
635+
: { primary: expectedIndent, secondary: undefined }
636+
607637
// Validate.
608638
for (const comment of comments) {
609-
validateCore(comment, expectedIndent)
639+
validateCore(comment, commentExpectedIndents.primary, commentExpectedIndents.secondary)
610640
}
611641
validateCore(firstToken, expectedIndent)
612642
}
@@ -1142,6 +1172,7 @@ function create (context) {
11421172
let tokensOnSameLine = []
11431173
let isBesideMultilineToken = false
11441174
let first = true
1175+
let lastValidatedToken = null
11451176

11461177
// Validate indentation of tokens.
11471178
for (const token of template.getTokens(node, { includeComments: true, filter: isNotWhitespace })) {
@@ -1168,15 +1199,16 @@ function create (context) {
11681199
}
11691200
}
11701201

1171-
validate(tokensOnSameLine, comments)
1202+
validate(tokensOnSameLine, comments, lastValidatedToken)
1203+
lastValidatedToken = tokensOnSameLine[0]
11721204
}
11731205
isBesideMultilineToken = last(tokensOnSameLine).loc.end.line === token.loc.start.line
11741206
tokensOnSameLine = [token]
11751207
comments = []
11761208
}
11771209
}
11781210
if (tokensOnSameLine.length >= 1 && tokensOnSameLine.some(isNotComment)) {
1179-
validate(tokensOnSameLine, comments)
1211+
validate(tokensOnSameLine, comments, lastValidatedToken)
11801212
}
11811213
}
11821214
}))

tests/lib/rules/html-indent.js

+97
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,46 @@ tester.run('html-indent', rule, {
12901290
}}
12911291
</template>
12921292
`,
1293+
unIndent`
1294+
<template>
1295+
{{
1296+
message
1297+
// comment
1298+
// comment
1299+
}}
1300+
<!-- comment -->
1301+
</template>
1302+
`,
1303+
unIndent`
1304+
<template>
1305+
{{
1306+
message
1307+
/*
1308+
* comment
1309+
*/
1310+
}}
1311+
</template>
1312+
`,
1313+
unIndent`
1314+
<template>
1315+
{{
1316+
message
1317+
// comment
1318+
// comment
1319+
}}
1320+
<!-- comment -->
1321+
</template>
1322+
`,
1323+
unIndent`
1324+
<template>
1325+
{{
1326+
message
1327+
/*
1328+
* comment
1329+
*/
1330+
}}
1331+
</template>
1332+
`,
12931333

12941334
// Ignores
12951335
{
@@ -4246,6 +4286,63 @@ tester.run('html-indent', rule, {
42464286
{ message: 'Expected indentation of 4 spaces but found 2 spaces.', line: 6 }
42474287
]
42484288
},
4289+
{
4290+
code: unIndent`
4291+
<template>
4292+
{{
4293+
message
4294+
// comment
4295+
// comment
4296+
}}
4297+
<!-- comment -->
4298+
</template>
4299+
`,
4300+
output: unIndent`
4301+
<template>
4302+
{{
4303+
message
4304+
// comment
4305+
// comment
4306+
}}
4307+
<!-- comment -->
4308+
</template>
4309+
`,
4310+
errors: [
4311+
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 2 },
4312+
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 3 },
4313+
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 4 },
4314+
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 5 },
4315+
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 6 }
4316+
]
4317+
},
4318+
{
4319+
code: unIndent`
4320+
<template>
4321+
{{
4322+
message
4323+
/*
4324+
* comment
4325+
*/
4326+
}}
4327+
</template>
4328+
`,
4329+
output: unIndent`
4330+
<template>
4331+
{{
4332+
message
4333+
/*
4334+
* comment
4335+
*/
4336+
}}
4337+
</template>
4338+
`,
4339+
errors: [
4340+
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 2 },
4341+
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 3 },
4342+
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 4 },
4343+
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 7 }
4344+
]
4345+
},
42494346

42504347
// Ignores
42514348
{

0 commit comments

Comments
 (0)