@@ -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 } ) )
0 commit comments