From 370b68ca1a12b777df43c7d5bf02207018f7e9d3 Mon Sep 17 00:00:00 2001 From: chezsmithy Date: Mon, 30 Jun 2025 17:58:29 -0700 Subject: [PATCH] linting fixes --- .../filtering/streamTransforms/lineStream.ts | 185 +++++++++------ .../streamTransforms/lineStream.vitest.ts | 213 ++++++++++++++++++ 2 files changed, 335 insertions(+), 63 deletions(-) diff --git a/core/autocomplete/filtering/streamTransforms/lineStream.ts b/core/autocomplete/filtering/streamTransforms/lineStream.ts index eabfca5a7..b603bc9ea 100644 --- a/core/autocomplete/filtering/streamTransforms/lineStream.ts +++ b/core/autocomplete/filtering/streamTransforms/lineStream.ts @@ -88,6 +88,118 @@ function isUselessLine(line: string): boolean { return hasUselessLine || trimmed.startsWith("// end"); } +/** + * Determines if a file is a markdown file based on its filepath. + */ +export function isMarkdownFile(filepath?: string): boolean { + if (!filepath) { + return false; + } + + return ["md", "markdown", "gfm"].includes( + filepath.split(".").pop()?.toLowerCase() || "", + ); +} + +/** + * Determines if the code block has nested markdown blocks. + */ +export function hasNestedMarkdownBlocks( + firstLine: string, + filepath?: string, +): boolean { + return ( + (firstLine.startsWith("```") && + headerIsMarkdown(firstLine.replace(/`/g, ""))) || + Boolean(filepath && headerIsMarkdown(filepath)) + ); +} + +/** + * Collects all lines from a LineStream into an array for analysis. + */ +export async function collectAllLines(rawLines: LineStream): Promise { + const allLines: string[] = []; + for await (const line of rawLines) { + allLines.push(line); + } + return allLines; +} + +/** + * Determines if we should stop at a markdown block based on nested markdown logic. + * This handles the complex case where markdown blocks contain other markdown blocks. + */ +export function shouldStopAtMarkdownBlock( + allLines: string[], + currentIndex: number, + hasNestedMarkdown: boolean, +): boolean { + if (!hasNestedMarkdown || allLines[currentIndex].trim() !== "```") { + return false; + } + + // Apply the same nested markdown logic as patchNestedMarkdown + const trimmedLines = allLines.map((l) => l.trim()); + let markdownNestCount = 0; + let shouldStop = false; + + for (let j = 0; j <= currentIndex; j++) { + const currentLine = trimmedLines[j]; + + if (markdownNestCount > 0) { + // Inside a markdown block + if (currentLine.match(/^`+$/)) { + // Found bare backticks - count remaining ones + let remainingBareBackticks = 0; + for (let k = j + 1; k < trimmedLines.length; k++) { + if (trimmedLines[k].match(/^`+$/)) { + remainingBareBackticks++; + } + } + + // If this is the last bare backticks and we're at position currentIndex, stop + if (remainingBareBackticks === 0 && j === currentIndex) { + markdownNestCount = 0; + shouldStop = true; + break; + } + } else if (currentLine.startsWith("```")) { + // Going into a nested codeblock + markdownNestCount++; + } + } else { + // Not inside a markdown block yet + if (currentLine.startsWith("```")) { + const header = currentLine.replaceAll("`", ""); + if (headerIsMarkdown(header)) { + markdownNestCount = 1; + } + } + } + } + + return shouldStop; +} + +/** + * Processes block nesting logic and returns updated state. + */ +export function processBlockNesting( + line: string, + seenFirstFence: boolean, +): { newSeenFirstFence: boolean; shouldSkip: boolean } { + if (!seenFirstFence && shouldRemoveLineBeforeStart(line)) { + return { newSeenFirstFence: false, shouldSkip: true }; + } + + if (!seenFirstFence) { + return { newSeenFirstFence: true, shouldSkip: false }; + } + + return { newSeenFirstFence: seenFirstFence, shouldSkip: false }; +} + export const USELESS_LINES = [""]; export const CODE_KEYWORDS_ENDING_IN_SEMICOLON = ["def"]; export const CODE_STOP_BLOCK = "[/CODE]"; @@ -372,34 +484,22 @@ export async function* filterCodeBlockLines( // when all blocks are matched. When no outer fence is discovered the function will always continue to the end. let nestCount = 0; - // Check if this is a markdown file and collect lines for analysis - const isMarkdownFile = - filepath && - ["md", "markdown", "gfm"].includes( - filepath.split(".").pop()?.toLowerCase() || "", - ); - - const allLines: string[] = []; - for await (const line of rawLines) { - allLines.push(line); - } + // Collect all lines for analysis + const allLines = await collectAllLines(rawLines); // Check if it has nested markdown blocks (like ```markdown or ```md) const firstLine = allLines[0] || ""; - const hasNestedMarkdown = - (firstLine.startsWith("```") && - headerIsMarkdown(firstLine.replace(/`/g, ""))) || - (filepath && headerIsMarkdown(filepath)); + const hasNestedMarkdown = hasNestedMarkdownBlocks(firstLine, filepath); for (let i = 0; i < allLines.length; i++) { const line = allLines[i]; - if (!seenFirstFence) { - if (shouldRemoveLineBeforeStart(line)) { - // Filter out starting ``` or START block - continue; - } - // Regardless of a fence or START block start tracking the nesting level + // Process block nesting logic for the first fence + const nesting = processBlockNesting(line, seenFirstFence); + if (nesting.shouldSkip) { + continue; // Filter out starting ``` or START block + } + if (!seenFirstFence && nesting.newSeenFirstFence) { seenFirstFence = true; nestCount = 1; } @@ -412,48 +512,7 @@ export async function* filterCodeBlockLines( // For markdown files with nested markdown blocks, apply special logic if (hasNestedMarkdown && line.trim() === "```") { - // Apply the same nested markdown logic as patchNestedMarkdown - const trimmedLines = allLines.map((l) => l.trim()); - - let markdownNestCount = 0; - let shouldStop = false; - - for (let j = 0; j <= i; j++) { - const currentLine = trimmedLines[j]; - - if (markdownNestCount > 0) { - // Inside a markdown block - if (currentLine.match(/^`+$/)) { - // Found bare backticks - count remaining ones - let remainingBareBackticks = 0; - for (let k = j + 1; k < trimmedLines.length; k++) { - if (trimmedLines[k].match(/^`+$/)) { - remainingBareBackticks++; - } - } - - // If this is the last bare backticks and we're at position i, stop - if (remainingBareBackticks === 0 && j === i) { - markdownNestCount = 0; - shouldStop = true; - break; - } - } else if (currentLine.startsWith("```")) { - // Going into a nested codeblock - markdownNestCount++; - } - } else { - // Not inside a markdown block yet - if (currentLine.startsWith("```")) { - const header = currentLine.replaceAll("`", ""); - if (headerIsMarkdown(header)) { - markdownNestCount = 1; - } - } - } - } - - if (shouldStop) { + if (shouldStopAtMarkdownBlock(allLines, i, hasNestedMarkdown)) { return; // Stop without yielding the final closing ``` } else { // This is an inner block delimiter, yield it as content diff --git a/core/autocomplete/filtering/streamTransforms/lineStream.vitest.ts b/core/autocomplete/filtering/streamTransforms/lineStream.vitest.ts index f68447c39..c37e853b5 100644 --- a/core/autocomplete/filtering/streamTransforms/lineStream.vitest.ts +++ b/core/autocomplete/filtering/streamTransforms/lineStream.vitest.ts @@ -757,4 +757,217 @@ describe("lineStream", () => { expect(mockFullStop).toHaveBeenCalledTimes(1); }); }); + + // Tests for new helper functions + describe("isMarkdownFile", () => { + it("should return true for markdown file extensions", () => { + expect(lineStream.isMarkdownFile("README.md")).toBe(true); + expect(lineStream.isMarkdownFile("docs.markdown")).toBe(true); + expect(lineStream.isMarkdownFile("notes.gfm")).toBe(true); + expect(lineStream.isMarkdownFile("path/to/file.md")).toBe(true); + }); + + it("should return false for non-markdown file extensions", () => { + expect(lineStream.isMarkdownFile("script.js")).toBe(false); + expect(lineStream.isMarkdownFile("styles.css")).toBe(false); + expect(lineStream.isMarkdownFile("document.txt")).toBe(false); + expect(lineStream.isMarkdownFile("config.json")).toBe(false); + }); + + it("should return false for undefined or empty filepath", () => { + expect(lineStream.isMarkdownFile(undefined)).toBe(false); + expect(lineStream.isMarkdownFile("")).toBe(false); + }); + + it("should handle case insensitive extensions", () => { + expect(lineStream.isMarkdownFile("README.MD")).toBe(true); + expect(lineStream.isMarkdownFile("docs.MARKDOWN")).toBe(true); + expect(lineStream.isMarkdownFile("notes.GFM")).toBe(true); + }); + }); + + describe("hasNestedMarkdownBlocks", () => { + it("should detect nested markdown blocks from first line", () => { + expect(lineStream.hasNestedMarkdownBlocks("```markdown", undefined)).toBe( + true, + ); + expect(lineStream.hasNestedMarkdownBlocks("```md", undefined)).toBe(true); + expect(lineStream.hasNestedMarkdownBlocks("```gfm", undefined)).toBe( + true, + ); + }); + + it("should detect nested markdown blocks from filepath", () => { + expect( + lineStream.hasNestedMarkdownBlocks("```typescript", "README.md"), + ).toBe(true); + expect( + lineStream.hasNestedMarkdownBlocks("```javascript", "docs.markdown"), + ).toBe(true); + }); + + it("should return false for non-markdown scenarios", () => { + expect( + lineStream.hasNestedMarkdownBlocks("```typescript", "script.js"), + ).toBe(false); + expect(lineStream.hasNestedMarkdownBlocks("```python", undefined)).toBe( + false, + ); + expect( + lineStream.hasNestedMarkdownBlocks("const x = 5;", undefined), + ).toBe(false); + }); + + it("should handle complex first line scenarios", () => { + expect( + lineStream.hasNestedMarkdownBlocks("```markdown README.md", undefined), + ).toBe(true); + expect( + lineStream.hasNestedMarkdownBlocks("```md with extra text", undefined), + ).toBe(true); + }); + }); + + describe("collectAllLines", () => { + it("should collect all lines from a stream", async () => { + const linesGenerator = await getLineGenerator([ + "line1", + "line2", + "line3", + ]); + + const result = await lineStream.collectAllLines(linesGenerator); + + expect(result).toEqual(["line1", "line2", "line3"]); + }); + + it("should handle empty stream", async () => { + const linesGenerator = await getLineGenerator([]); + + const result = await lineStream.collectAllLines(linesGenerator); + + expect(result).toEqual([]); + }); + + it("should handle stream with empty lines", async () => { + const linesGenerator = await getLineGenerator(["", "line2", "", "line4"]); + + const result = await lineStream.collectAllLines(linesGenerator); + + expect(result).toEqual(["", "line2", "", "line4"]); + }); + }); + + describe("shouldStopAtMarkdownBlock", () => { + it("should return false when hasNestedMarkdown is false", () => { + const allLines = ["```markdown", "content", "```"]; + expect(lineStream.shouldStopAtMarkdownBlock(allLines, 2, false)).toBe( + false, + ); + }); + + it("should return false when current line is not closing backticks", () => { + const allLines = ["```markdown", "content", "more content"]; + expect(lineStream.shouldStopAtMarkdownBlock(allLines, 2, true)).toBe( + false, + ); + }); + + it("should handle simple nested markdown case", () => { + const allLines = ["```markdown", "# Title", "```"]; + expect(lineStream.shouldStopAtMarkdownBlock(allLines, 2, true)).toBe( + true, + ); + }); + + it("should handle complex nested markdown with inner code blocks", () => { + const allLines = [ + "```markdown", + "# Title", + "```javascript", + "console.log('hello');", + "```", + "```", + ]; + // At index 4 (first inner closing), should not stop + expect(lineStream.shouldStopAtMarkdownBlock(allLines, 4, true)).toBe( + false, + ); + // At index 5 (outer closing), should stop + expect(lineStream.shouldStopAtMarkdownBlock(allLines, 5, true)).toBe( + true, + ); + }); + + it("should handle multiple bare backticks scenario", () => { + const allLines = [ + "```markdown", + "Content with ```inline code```", + "```", + "More content", + "```", + ]; + // At index 2, should not stop (not the last bare backticks) + expect(lineStream.shouldStopAtMarkdownBlock(allLines, 2, true)).toBe( + false, + ); + // At index 4, should stop (last bare backticks) + expect(lineStream.shouldStopAtMarkdownBlock(allLines, 4, true)).toBe( + true, + ); + }); + }); + + describe("processBlockNesting", () => { + it("should skip lines that should be removed before start", () => { + const result = lineStream.processBlockNesting("```typescript", false); + expect(result).toEqual({ + newSeenFirstFence: false, + shouldSkip: true, + }); + }); + + it("should skip [CODE] blocks", () => { + const result = lineStream.processBlockNesting("[CODE]", false); + expect(result).toEqual({ + newSeenFirstFence: false, + shouldSkip: true, + }); + }); + + it("should set first fence when not seen yet", () => { + const result = lineStream.processBlockNesting("const x = 5;", false); + expect(result).toEqual({ + newSeenFirstFence: true, + shouldSkip: false, + }); + }); + + it("should not change state when first fence already seen", () => { + const result = lineStream.processBlockNesting("const x = 5;", true); + expect(result).toEqual({ + newSeenFirstFence: true, + shouldSkip: false, + }); + }); + + it("should handle prefix", () => { + const result = lineStream.processBlockNesting("", false); + expect(result).toEqual({ + newSeenFirstFence: false, + shouldSkip: true, + }); + }); + + it("should handle START EDITING HERE blocks", () => { + const result = lineStream.processBlockNesting( + "", + false, + ); + expect(result).toEqual({ + newSeenFirstFence: false, + shouldSkip: true, + }); + }); + }); });