{"version":3,"file":"util.capture-next-block.cjs","sources":["../../../../src/rules/limit-multi-line-comments/util.capture-next-block.ts"],"sourcesContent":["import type { Context } from \"../../typings.context.js\";\nimport { isLineOverflowing } from \"../../utils/is-line-overflowing.js\";\n\nimport type { MultilineBlock } from \"./typings.block.js\";\n\n/**\n * captures the next logical group/block in the provided multi-line comment\n * content, based on a set of rules.\n *\n * 1) Everything within a set of back-ticks (``) is ignored, as this is used\n * to explicitly declare that the content should not be auto-fixed.\n *\n * 2) Lines that are not on the same indentation-level will not be recognized\n * as part of the same block.\n *\n * 3) Lines separated by a new-line will not be considered as part of the same\n * block.\n *\n * 4) Lines will only be grouped in case the current line of the block to be\n * constructed actually is overflowing. This avoids issues where auto-fixing\n * 'sucks' a line up even though the previous line should have been considered\n * a logical end to a block.\n */\nexport function captureNextBlock(\n  ignoreFollowingLines: boolean,\n  initialStartIndex: number,\n  context: Context,\n): [Omit<MultilineBlock, \"value\">, boolean] {\n  let ignoreLines = ignoreFollowingLines;\n  let startIndex = initialStartIndex;\n\n  // the provided startIndex may not necessarily indicate the startIndex of the\n  // next logical block. (it may e.g. just point to a blank line)\n  // as such we need to determine the actual start of the next block.\n  for (let i = initialStartIndex; i < context.comment.lines.length; i++) {\n    const line = context.comment.lines[i];\n\n    // ensure that lines within backticks is skipped (and that the line itself\n    // is ignored as it acts as a marker).\n    if (\n      line?.trimStart().startsWith(\"` \") ||\n      line?.trimStart().startsWith(\"``\")\n    ) {\n      ignoreLines = !ignoreLines;\n      continue;\n    }\n\n    startIndex = i;\n\n    if (line && line.trim() !== \"\" && !ignoreLines) {\n      break;\n    }\n  }\n\n  const blockLines: string[] = context.comment.lines.slice(\n    startIndex,\n    startIndex + 1,\n  );\n\n  // In case we could not resolve the start of a new block, then we cannot\n  // continue...\n  if (blockLines.length === 0) {\n    return [\n      {\n        lines: blockLines,\n        startIndex,\n        endIndex: startIndex,\n        lineOffsets: [],\n      },\n      ignoreLines,\n    ];\n  }\n\n  // ... else we can begin analysing the following lines to determine if they\n  // are to be added to the current group\n  for (let i = startIndex; i < context.comment.lines.length; i++) {\n    const currLine = context.comment.lines[i];\n    const nextLine = context.comment.lines[i + 1];\n\n    if (!currLine) {\n      break;\n    }\n\n    const currLineOffset =\n      currLine\n        .match(/^( |\\t)*/)?.[0]\n        ?.split(\"\")\n        .reduce(\n          (acc, curr) => acc + (curr === \"\\t\" ? context.tabSize : 1),\n          0,\n        ) ?? 0;\n\n    const nextLineOffset =\n      nextLine\n        ?.match(/^( |\\t)*/)?.[0]\n        ?.split(\"\")\n        .reduce(\n          (acc, curr) => acc + (curr === \"\\t\" ? context.tabSize : 1),\n          0,\n        ) ?? 0;\n\n    if (\n      !nextLine ||\n      nextLine.trim() === \"\" ||\n      currLineOffset !== nextLineOffset ||\n      (context.mode === \"overflow-only\" &&\n        !isLineOverflowing(\n          `${currLine} ${nextLine.trimStart().split(\" \")[0] ?? \"\"}`.trimEnd(),\n          context,\n        ))\n    ) {\n      return [\n        {\n          lines: blockLines,\n          startIndex,\n          endIndex: i,\n          lineOffsets: blockLines.map((it, lineIndex) => {\n            const whitespaceString = context.comment.value\n              .split(\"\\n\")\n              [startIndex + lineIndex]?.includes(\"*\")\n              ? (it.match(/^( |\\t)*/)?.[0] ?? \"\")\n              : \" \";\n\n            return {\n              string: whitespaceString,\n              size:\n                whitespaceString\n                  .split(\"\")\n                  .reduce(\n                    (acc, curr) => acc + (curr === \"\\t\" ? context.tabSize : 1),\n                    0,\n                  ) ?? 0,\n            };\n          }),\n        },\n        ignoreLines,\n      ];\n    }\n\n    blockLines.push(nextLine);\n  }\n\n  return [\n    {\n      lines: blockLines,\n      startIndex,\n      endIndex: context.comment.lines.length,\n      lineOffsets: blockLines.map((it, lineIndex) => {\n        const whitespaceString = context.comment.value\n          .split(\"\\n\")\n          [startIndex + lineIndex]?.includes(\"*\")\n          ? (it.match(/^( |\\t)*/)?.[0] ?? \"\")\n          : \" \";\n\n        return {\n          string: whitespaceString,\n          size:\n            whitespaceString\n              .split(\"\")\n              .reduce(\n                (acc, curr) => acc + (curr === \"\\t\" ? context.tabSize : 1),\n                0,\n              ) ?? 0,\n        };\n      }),\n    },\n    ignoreLines,\n  ];\n}\n"],"names":["isLineOverflowing"],"mappings":";;;;AAuBO,SAAS,gBAAA,CACd,oBAAA,EACA,iBAAA,EACA,OAAA,EAC0C;AAC1C,EAAA,IAAI,WAAA,GAAc,oBAAA;AAClB,EAAA,IAAI,UAAA,GAAa,iBAAA;AAKjB,EAAA,KAAA,IAAS,IAAI,iBAAA,EAAmB,CAAA,GAAI,QAAQ,OAAA,CAAQ,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrE,IAAA,MAAM,IAAA,GAAO,OAAA,CAAQ,OAAA,CAAQ,KAAA,CAAM,CAAC,CAAA;AAIpC,IAAA,IACE,IAAA,EAAM,SAAA,EAAU,CAAE,UAAA,CAAW,IAAI,CAAA,IACjC,IAAA,EAAM,SAAA,EAAU,CAAE,UAAA,CAAW,IAAI,CAAA,EACjC;AACA,MAAA,WAAA,GAAc,CAAC,WAAA;AACf,MAAA;AAAA,IACF;AAEA,IAAA,UAAA,GAAa,CAAA;AAEb,IAAA,IAAI,QAAQ,IAAA,CAAK,IAAA,EAAK,KAAM,EAAA,IAAM,CAAC,WAAA,EAAa;AAC9C,MAAA;AAAA,IACF;AAAA,EACF;AAEA,EAAA,MAAM,UAAA,GAAuB,OAAA,CAAQ,OAAA,CAAQ,KAAA,CAAM,KAAA;AAAA,IACjD,UAAA;AAAA,IACA,UAAA,GAAa;AAAA,GACf;AAIA,EAAA,IAAI,UAAA,CAAW,WAAW,CAAA,EAAG;AAC3B,IAAA,OAAO;AAAA,MACL;AAAA,QACE,KAAA,EAAO,UAAA;AAAA,QACP,UAAA;AAAA,QACA,QAAA,EAAU,UAAA;AAAA,QACV,aAAa;AAAC,OAChB;AAAA,MACA;AAAA,KACF;AAAA,EACF;AAIA,EAAA,KAAA,IAAS,IAAI,UAAA,EAAY,CAAA,GAAI,QAAQ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