UNPKG

eslint-plugin-comment-length

Version:

An ESLint plugin that provides rules that limit the line length of your comments

1 lines 8.5 kB
{"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 ) ??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