Skip to content

⬅️ Back to Table of Contents

πŸ“„ getForStatementHeadLoc

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 1
πŸ“¦ Imports 3

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/eslint-plugin/src/util/getForStatementHeadLoc.ts

πŸ“¦ Imports

Name Source
TSESLint @typescript-eslint/utils
TSESTree @typescript-eslint/utils
nullThrows @typescript-eslint/utils/eslint-utils

Functions

getForStatementHeadLoc(sourceCode: TSESLint.SourceCode, node: TSESTree.ForInStatement | TSESTree.ForO…): TSESTree.SourceLocation

Gets the location of the head of the given for statement variant for reporting.

  • for (const foo in bar) expressionOrBlock ^^^^^^^^^^^^^^^^^^^^^^

  • for (const foo of bar) expressionOrBlock ^^^^^^^^^^^^^^^^^^^^^^

  • for await (const foo of bar) expressionOrBlock ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  • for (let i = 0; i < 10; i++) expressionOrBlock ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Raw JSDoc
/**
 * Gets the location of the head of the given for statement variant for reporting.
 *
 * - `for (const foo in bar) expressionOrBlock`
 *    ^^^^^^^^^^^^^^^^^^^^^^
 *
 * - `for (const foo of bar) expressionOrBlock`
 *    ^^^^^^^^^^^^^^^^^^^^^^
 *
 * - `for await (const foo of bar) expressionOrBlock`
 *    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *
 * - `for (let i = 0; i < 10; i++) expressionOrBlock`
 *    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 */

Calls:

  • nullThrows (from @typescript-eslint/utils/eslint-utils)
  • sourceCode.getTokenBefore
  • structuredClone
Code
export function getForStatementHeadLoc(
  sourceCode: TSESLint.SourceCode,
  node:
    TSESTree.ForInStatement | TSESTree.ForOfStatement | TSESTree.ForStatement,
): TSESTree.SourceLocation {
  const closingParens = nullThrows(
    sourceCode.getTokenBefore(node.body, token => token.value === ')'),
    'for statement must have a closing parenthesis.',
  );
  return {
    end: structuredClone(closingParens.loc.end),
    start: structuredClone(node.loc.start),
  };
}

Generated by Syntax Scribe