@@ -18,7 +18,6 @@ const KNOWN_NODES = new Set(['ArrayExpression', 'ArrayPattern', 'ArrowFunctionEx
1818const LT_CHAR = / [ \r \n \u2028 \u2029 ] /
1919const LINES = / [ ^ \r \n \u2028 \u2029 ] + (?: $ | \r \n | [ \r \n \u2028 \u2029 ] ) / g
2020const BLOCK_COMMENT_PREFIX = / ^ \s * \* /
21- const TRIVIAL_PUNCTUATOR = / ^ [ ( ) { } [ \] , ; ] $ /
2221
2322/**
2423 * Normalize options.
@@ -245,21 +244,6 @@ function isClosingToken (token) {
245244 )
246245}
247246
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-
263247/**
264248 * Creates AST event handlers for html-indent.
265249 *
@@ -564,33 +548,37 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
564548 /**
565549 * Calculate correct indentation of the line of the given tokens.
566550 * @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 `.
568552 */
569- function getExpectedIndent ( tokens ) {
570- const trivial = isTrivialToken ( tokens [ 0 ] )
571- let expectedIndent = Number . MAX_SAFE_INTEGER
553+ function getExpectedIndents ( tokens ) {
554+ const expectedIndents = [ ]
572555
573556 for ( let i = 0 ; i < tokens . length ; ++ i ) {
574557 const token = tokens [ i ]
575558 const offsetInfo = offsets . get ( token )
576559
577- // If the first token is not trivial then ignore trivial following tokens.
578- if ( offsetInfo != null && ( trivial || ! isTrivialToken ( token ) ) ) {
560+ if ( offsetInfo != null ) {
579561 if ( offsetInfo . expectedIndent != null ) {
580- expectedIndent = Math . min ( expectedIndent , offsetInfo . expectedIndent )
562+ expectedIndents . push ( offsetInfo . expectedIndent )
581563 } else {
582564 const baseOffsetInfo = offsets . get ( offsetInfo . baseToken )
583565 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 )
585567 if ( baseOffsetInfo . baseline ) {
586568 break
587569 }
588570 }
589571 }
590572 }
591573 }
574+ if ( ! expectedIndents . length ) {
575+ return null
576+ }
592577
593- return expectedIndent
578+ return {
579+ expectedIndent : expectedIndents [ 0 ] ,
580+ expectedBaseIndent : expectedIndents . reduce ( ( a , b ) => Math . min ( a , b ) )
581+ }
594582 }
595583
596584 /**
@@ -746,11 +734,14 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
746734 // Calculate and save expected indentation.
747735 const firstToken = tokens [ 0 ]
748736 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 ) {
751739 return
752740 }
753741
742+ const expectedBaseIndent = expectedIndents . expectedBaseIndent
743+ const expectedIndent = expectedIndents . expectedIndent
744+
754745 // Debug log
755746 // console.log('line', firstToken.loc.start.line, '=', { actualIndent, expectedIndent }, 'from:')
756747 // for (const token of tokens) {
@@ -773,11 +764,11 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
773764 if ( offsetInfo . baseline ) {
774765 // This is a baseline token, so the expected indent is the column of this token.
775766 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 )
777768 } else {
778769 // In hard-tabs mode, it cannot align tokens strictly, so use one additional offset.
779770 // 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 )
781772 }
782773 baseline . add ( token )
783774 } else if ( baseline . has ( offsetInfo . baseToken ) ) {
@@ -786,7 +777,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
786777 baseline . add ( token )
787778 } else {
788779 // Otherwise, set the expected indent of this line.
789- offsetInfo . expectedIndent = expectedIndent
780+ offsetInfo . expectedIndent = expectedBaseIndent
790781 }
791782 }
792783 }
0 commit comments