lib/utils/indent-common.js
function validate (tokens, comments, lastToken) {
    // Calculate and save expected indentation.
    const firstToken = tokens[0]
    const actualIndent = firstToken.loc.start.column
    const expectedIndent = getExpectedIndent(tokens)
    if (expectedIndent === Number.MAX_SAFE_INTEGER) {
      return
    }

    // Debug log
    // console.log('line', firstToken.loc.start.line, '=', { actualIndent, expectedIndent }, 'from:')
    // for (const token of tokens) {
    //   const offsetInfo = offsets.get(token)
    //   if (offsetInfo == null) {
    //     console.log('    ', JSON.stringify(sourceCode.getText(token)), 'is unknown.')
    //   } else if (offsetInfo.expectedIndent != null) {
    //     console.log('    ', JSON.stringify(sourceCode.getText(token)), 'is fixed at', offsetInfo.expectedIndent, '.')
    //   } else {
    //     const baseOffsetInfo = offsets.get(offsetInfo.baseToken)
    //     console.log('    ', JSON.stringify(sourceCode.getText(token)), 'is', offsetInfo.offset, 'offset from ', JSON.stringify(sourceCode.getText(offsetInfo.baseToken)), '( line:', offsetInfo.baseToken && offsetInfo.baseToken.loc.start.line, ', indent:', baseOffsetInfo && baseOffsetInfo.expectedIndent, ', baseline:', baseOffsetInfo && baseOffsetInfo.baseline, ')')
    //   }
    // }

    // Save.
    const baseline = new Set()
    for (const token of tokens) {
      const offsetInfo = offsets.get(token)
      if (offsetInfo != null) {
        if (offsetInfo.baseline) {
          // This is a baseline token, so the expected indent is the column of this token.
          if (options.indentChar === ' ') {
            offsetInfo.expectedIndent = Math.max(0, token.loc.start.column + expectedIndent - actualIndent)
          } else {
            // In hard-tabs mode, it cannot align tokens strictly, so use one additional offset.
            // But the additional offset isn't needed if it's at the beginning of the line.
            offsetInfo.expectedIndent = expectedIndent + (token === tokens[0] ? 0 : 1)
          }
          baseline.add(token)
        } else if (baseline.has(offsetInfo.baseToken)) {
          // The base token is a baseline token on this line, so inherit it.
          offsetInfo.expectedIndent = offsets.get(offsetInfo.baseToken).expectedIndent
          baseline.add(token)
        } else {
          // Otherwise, set the expected indent of this line.
          offsetInfo.expectedIndent = expectedIndent
        }
      }
    }

    // Calculate the expected indents for comments.
    // It allows the same indent level with the previous line.
    const lastOffsetInfo = offsets.get(lastToken)
    const lastExpectedIndent = lastOffsetInfo && lastOffsetInfo.expectedIndent
    const commentExpectedIndents = getCommentExpectedIndents(firstToken, expectedIndent, lastExpectedIndent)

    // Validate.
    for (const comment of comments) {
      validateCore(comment, commentExpectedIndents.primary, commentExpectedIndents.secondary)
    }
    validateCore(firstToken, expectedIndent)
  }
function validate (tokens, comments, lastToken) {
    // Calculate and save expected indentation.
    const firstToken = tokens[0]
    const actualIndent = firstToken.loc.start.column
    const expectedIndent = getExpectedIndent(tokens)
    if (expectedIndent === Number.MAX_SAFE_INTEGER) {
      return
    }

    // Debug log
    // console.log('line', firstToken.loc.start.line, '=', { actualIndent, expectedIndent }, 'from:')
    // for (const token of tokens) {
    //   const offsetInfo = offsets.get(token)
    //   if (offsetInfo == null) {
    //     console.log('    ', JSON.stringify(sourceCode.getText(token)), 'is unknown.')
    //   } else if (offsetInfo.expectedIndent != null) {
    //     console.log('    ', JSON.stringify(sourceCode.getText(token)), 'is fixed at', offsetInfo.expectedIndent, '.')
    //   } else {
    //     const baseOffsetInfo = offsets.get(offsetInfo.baseToken)
    //     console.log('    ', JSON.stringify(sourceCode.getText(token)), 'is', offsetInfo.offset, 'offset from ', JSON.stringify(sourceCode.getText(offsetInfo.baseToken)), '( line:', offsetInfo.baseToken && offsetInfo.baseToken.loc.start.line, ', indent:', baseOffsetInfo && baseOffsetInfo.expectedIndent, ', baseline:', baseOffsetInfo && baseOffsetInfo.baseline, ')')
    //   }
    // }

    // Save.
    const baseline = new Set()
    for (const token of tokens) {
      const offsetInfo = offsets.get(token)
      if (offsetInfo != null) {
        if (offsetInfo.baseline) {
          // This is a baseline token, so the expected indent is the column of this token.
          if (options.indentChar === ' ') {
            offsetInfo.expectedIndent = Math.max(0, token.loc.start.column + expectedIndent - actualIndent)
          } else {
            // In hard-tabs mode, it cannot align tokens strictly, so use one additional offset.
            // But the additional offset isn't needed if it's at the beginning of the line.
            offsetInfo.expectedIndent = expectedIndent + (token === tokens[0] ? 0 : 1)
          }
          baseline.add(token)
        } else if (baseline.has(offsetInfo.baseToken)) {
          // The base token is a baseline token on this line, so inherit it.
          offsetInfo.expectedIndent = offsets.get(offsetInfo.baseToken).expectedIndent
          baseline.add(token)
        } else {
          // Otherwise, set the expected indent of this line.
          offsetInfo.expectedIndent = expectedIndent
        }
      }
    }

    // Calculate the expected indents for comments.
    // It allows the same indent level with the previous line.
    const lastOffsetInfo = offsets.get(lastToken)
    const lastExpectedIndent = lastOffsetInfo && lastOffsetInfo.expectedIndent
    const commentExpectedIndents = getCommentExpectedIndents(firstToken, expectedIndent, lastExpectedIndent)

    // Validate.
    for (const comment of comments) {
      validateCore(comment, commentExpectedIndents.primary, commentExpectedIndents.secondary)
    }
    validateCore(firstToken, expectedIndent)
  }
function validate ( tokens , comments , lastToken ) { // Calculate and save expected indentation. const firstToken = tokens [ 0 ] const actualIndent = firstToken . loc . start . column const expectedIndent = getExpectedIndent ( tokens ) if ( expectedIndent === Number . MAX_SAFE_INTEGER ) { return } // Debug log // console.log('line', firstToken.loc.start.line, '=', { actualIndent, expectedIndent }, 'from:') // for (const token of tokens) { //   const offsetInfo = offsets.get(token) //   if (offsetInfo == null) { //     console.log('    ', JSON.stringify(sourceCode.getText(token)), 'is unknown.') //   } else if (offsetInfo.expectedIndent != null) { //     console.log('    ', JSON.stringify(sourceCode.getText(token)), 'is fixed at', offsetInfo.expectedIndent, '.') //   } else { //     const baseOffsetInfo = offsets.get(offsetInfo.baseToken) //     console.log('    ', JSON.stringify(sourceCode.getText(token)), 'is', offsetInfo.offset, 'offset from ', JSON.stringify(sourceCode.getText(offsetInfo.baseToken)), '( line:', offsetInfo.baseToken && offsetInfo.baseToken.loc.start.line, ', indent:', baseOffsetInfo && baseOffsetInfo.expectedIndent, ', baseline:', baseOffsetInfo && baseOffsetInfo.baseline, ')') //   } // } // Save. const baseline = new Set ( ) for ( const token of tokens ) { const offsetInfo = offsets . get ( token ) if ( offsetInfo != null ) { if ( offsetInfo . baseline ) { // This is a baseline token, so the expected indent is the column of this token. if ( options . indentChar === ' ' ) { offsetInfo . expectedIndent = Math . max ( 0 , token . loc . start . column + expectedIndent - actualIndent ) } else { // In hard-tabs mode, it cannot align tokens strictly, so use one additional offset. // But the additional offset isn't needed if it's at the beginning of the line. offsetInfo . expectedIndent = expectedIndent + ( token === tokens [ 0 ] ? 0 : 1 ) } baseline . add ( token ) } else if ( baseline . has ( offsetInfo . baseToken ) ) { // The base token is a baseline token on this line, so inherit it. offsetInfo . expectedIndent = offsets . get ( offsetInfo . baseToken ) . expectedIndent baseline . add ( token ) } else { // Otherwise, set the expected indent of this line. offsetInfo . expectedIndent = expectedIndent } } } // Calculate the expected indents for comments. // It allows the same indent level with the previous line. const lastOffsetInfo = offsets . get ( lastToken ) const lastExpectedIndent = lastOffsetInfo && lastOffsetInfo . expectedIndent const commentExpectedIndents = getCommentExpectedIndents ( firstToken , expectedIndent , lastExpectedIndent ) // Validate. for ( const comment of comments ) { validateCore ( comment , commentExpectedIndents . primary , commentExpectedIndents . secondary ) } validateCore ( firstToken , expectedIndent ) }
Validate indentation of the line that the given tokens are on.
@param {Token[]} tokens The tokens on the same line to validate.
@param {Token[]} comments The comments which are on the immediately previous lines of the tokens.
@param {Token|null} lastToken The last validated token. Comments can adjust to the token.
@returns {void}
Validate indentation of the line that the given tokens are on .