@@ -198,6 +198,26 @@ function isBeginningOfLine (node, index, nodes) {
198
198
return false
199
199
}
200
200
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
+
201
221
/**
202
222
* Creates AST event handlers for html-indent.
203
223
*
@@ -504,9 +524,10 @@ function create (context) {
504
524
* Validate the given token with the pre-calculated expected indentation.
505
525
* @param {Token } token The token to validate.
506
526
* @param {number } expectedIndent The expected indentation.
527
+ * @param {number|undefined } optionalExpectedIndent The optional expected indentation.
507
528
* @returns {void }
508
529
*/
509
- function validateCore ( token , expectedIndent ) {
530
+ function validateCore ( token , expectedIndent , optionalExpectedIndent ) {
510
531
const line = token . loc . start . line
511
532
const actualIndent = token . loc . start . column
512
533
const indentText = getIndentText ( token )
@@ -530,7 +551,7 @@ function create (context) {
530
551
}
531
552
}
532
553
533
- if ( actualIndent !== expectedIndent ) {
554
+ if ( actualIndent !== expectedIndent && ( optionalExpectedIndent === undefined || actualIndent !== optionalExpectedIndent ) ) {
534
555
context . report ( {
535
556
loc : {
536
557
start : { line, column : 0 } ,
@@ -553,9 +574,10 @@ function create (context) {
553
574
* Validate indentation of the line that the given tokens are on.
554
575
* @param {Token[] } tokens The tokens on the same line to validate.
555
576
* @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.
556
578
* @returns {void }
557
579
*/
558
- function validate ( tokens , comments ) {
580
+ function validate ( tokens , comments , lastToken ) {
559
581
// Calculate and save expected indentation.
560
582
const firstToken = tokens [ 0 ]
561
583
const actualIndent = firstToken . loc . start . column
@@ -604,9 +626,17 @@ function create (context) {
604
626
}
605
627
}
606
628
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
+
607
637
// Validate.
608
638
for ( const comment of comments ) {
609
- validateCore ( comment , expectedIndent )
639
+ validateCore ( comment , commentExpectedIndents . primary , commentExpectedIndents . secondary )
610
640
}
611
641
validateCore ( firstToken , expectedIndent )
612
642
}
@@ -1142,6 +1172,7 @@ function create (context) {
1142
1172
let tokensOnSameLine = [ ]
1143
1173
let isBesideMultilineToken = false
1144
1174
let first = true
1175
+ let lastValidatedToken = null
1145
1176
1146
1177
// Validate indentation of tokens.
1147
1178
for ( const token of template . getTokens ( node , { includeComments : true , filter : isNotWhitespace } ) ) {
@@ -1168,15 +1199,16 @@ function create (context) {
1168
1199
}
1169
1200
}
1170
1201
1171
- validate ( tokensOnSameLine , comments )
1202
+ validate ( tokensOnSameLine , comments , lastValidatedToken )
1203
+ lastValidatedToken = tokensOnSameLine [ 0 ]
1172
1204
}
1173
1205
isBesideMultilineToken = last ( tokensOnSameLine ) . loc . end . line === token . loc . start . line
1174
1206
tokensOnSameLine = [ token ]
1175
1207
comments = [ ]
1176
1208
}
1177
1209
}
1178
1210
if ( tokensOnSameLine . length >= 1 && tokensOnSameLine . some ( isNotComment ) ) {
1179
- validate ( tokensOnSameLine , comments )
1211
+ validate ( tokensOnSameLine , comments , lastValidatedToken )
1180
1212
}
1181
1213
}
1182
1214
} ) )
0 commit comments