@@ -18,7 +18,6 @@ const KNOWN_NODES = new Set(['ArrayExpression', 'ArrayPattern', 'ArrowFunctionEx
18
18
const LT_CHAR = / [ \r \n \u2028 \u2029 ] /
19
19
const LINES = / [ ^ \r \n \u2028 \u2029 ] + (?: $ | \r \n | [ \r \n \u2028 \u2029 ] ) / g
20
20
const BLOCK_COMMENT_PREFIX = / ^ \s * \* /
21
- const TRIVIAL_PUNCTUATOR = / ^ [ ( ) { } [ \] , ; ] $ /
22
21
23
22
/**
24
23
* Normalize options.
@@ -245,21 +244,6 @@ function isClosingToken (token) {
245
244
)
246
245
}
247
246
248
- /**
249
- * Check whether a given token is trivial or not.
250
- * @param {Token } token The token to check.
251
- * @returns {boolean } `true` if the token is trivial.
252
- */
253
- function isTrivialToken ( token ) {
254
- return token != null && (
255
- ( token . type === 'Punctuator' && TRIVIAL_PUNCTUATOR . test ( token . value ) ) ||
256
- token . type === 'HTMLTagOpen' ||
257
- token . type === 'HTMLEndTagOpen' ||
258
- token . type === 'HTMLTagClose' ||
259
- token . type === 'HTMLSelfClosingTagClose'
260
- )
261
- }
262
-
263
247
/**
264
248
* Creates AST event handlers for html-indent.
265
249
*
@@ -564,33 +548,37 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
564
548
/**
565
549
* Calculate correct indentation of the line of the given tokens.
566
550
* @param {Token[] } tokens Tokens which are on the same line.
567
- * @returns {number } Correct indentation. If it failed to calculate then `Number.MAX_SAFE_INTEGER `.
551
+ * @returns {object|null } Correct indentation. If it failed to calculate then `null `.
568
552
*/
569
- function getExpectedIndent ( tokens ) {
570
- const trivial = isTrivialToken ( tokens [ 0 ] )
571
- let expectedIndent = Number . MAX_SAFE_INTEGER
553
+ function getExpectedIndents ( tokens ) {
554
+ const expectedIndents = [ ]
572
555
573
556
for ( let i = 0 ; i < tokens . length ; ++ i ) {
574
557
const token = tokens [ i ]
575
558
const offsetInfo = offsets . get ( token )
576
559
577
- // If the first token is not trivial then ignore trivial following tokens.
578
- if ( offsetInfo != null && ( trivial || ! isTrivialToken ( token ) ) ) {
560
+ if ( offsetInfo != null ) {
579
561
if ( offsetInfo . expectedIndent != null ) {
580
- expectedIndent = Math . min ( expectedIndent , offsetInfo . expectedIndent )
562
+ expectedIndents . push ( offsetInfo . expectedIndent )
581
563
} else {
582
564
const baseOffsetInfo = offsets . get ( offsetInfo . baseToken )
583
565
if ( baseOffsetInfo != null && baseOffsetInfo . expectedIndent != null && ( i === 0 || ! baseOffsetInfo . baseline ) ) {
584
- expectedIndent = Math . min ( expectedIndent , baseOffsetInfo . expectedIndent + offsetInfo . offset * options . indentSize )
566
+ expectedIndents . push ( baseOffsetInfo . expectedIndent + offsetInfo . offset * options . indentSize )
585
567
if ( baseOffsetInfo . baseline ) {
586
568
break
587
569
}
588
570
}
589
571
}
590
572
}
591
573
}
574
+ if ( ! expectedIndents . length ) {
575
+ return null
576
+ }
592
577
593
- return expectedIndent
578
+ return {
579
+ expectedIndent : expectedIndents [ 0 ] ,
580
+ expectedBaseIndent : expectedIndents . reduce ( ( a , b ) => Math . min ( a , b ) )
581
+ }
594
582
}
595
583
596
584
/**
@@ -746,11 +734,14 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
746
734
// Calculate and save expected indentation.
747
735
const firstToken = tokens [ 0 ]
748
736
const actualIndent = firstToken . loc . start . column
749
- const expectedIndent = getExpectedIndent ( tokens )
750
- if ( expectedIndent === Number . MAX_SAFE_INTEGER ) {
737
+ const expectedIndents = getExpectedIndents ( tokens )
738
+ if ( ! expectedIndents ) {
751
739
return
752
740
}
753
741
742
+ const expectedBaseIndent = expectedIndents . expectedBaseIndent
743
+ const expectedIndent = expectedIndents . expectedIndent
744
+
754
745
// Debug log
755
746
// console.log('line', firstToken.loc.start.line, '=', { actualIndent, expectedIndent }, 'from:')
756
747
// for (const token of tokens) {
@@ -773,11 +764,11 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
773
764
if ( offsetInfo . baseline ) {
774
765
// This is a baseline token, so the expected indent is the column of this token.
775
766
if ( options . indentChar === ' ' ) {
776
- offsetInfo . expectedIndent = Math . max ( 0 , token . loc . start . column + expectedIndent - actualIndent )
767
+ offsetInfo . expectedIndent = Math . max ( 0 , token . loc . start . column + expectedBaseIndent - actualIndent )
777
768
} else {
778
769
// In hard-tabs mode, it cannot align tokens strictly, so use one additional offset.
779
770
// But the additional offset isn't needed if it's at the beginning of the line.
780
- offsetInfo . expectedIndent = expectedIndent + ( token === tokens [ 0 ] ? 0 : 1 )
771
+ offsetInfo . expectedIndent = expectedBaseIndent + ( token === tokens [ 0 ] ? 0 : 1 )
781
772
}
782
773
baseline . add ( token )
783
774
} else if ( baseline . has ( offsetInfo . baseToken ) ) {
@@ -786,7 +777,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
786
777
baseline . add ( token )
787
778
} else {
788
779
// Otherwise, set the expected indent of this line.
789
- offsetInfo . expectedIndent = expectedIndent
780
+ offsetInfo . expectedIndent = expectedBaseIndent
790
781
}
791
782
}
792
783
}
0 commit comments