improv: single responsibility in filter functions

This commit is contained in:
tomasz-io 2024-11-04 17:58:59 +07:00
parent fb1a75dcf3
commit fe1af0156f
3 changed files with 48 additions and 14 deletions

View file

@ -3,7 +3,8 @@ import { HelperVars } from "../../util/HelperVars";
import { BracketMatchingService } from "../BracketMatchingService";
import { stopAtStopTokens } from "./charStream";
import {
avoidPathLineAndEmptyComments,
avoidEmptyComments,
avoidPathLine,
showWhateverWeHaveAtXMs,
skipPrefixes,
stopAtLines,
@ -59,10 +60,11 @@ export class StreamTransformPipeline {
lineGenerator = stopAtLines(lineGenerator, fullStop);
lineGenerator = stopAtRepeatingLines(lineGenerator, fullStop);
lineGenerator = avoidPathLineAndEmptyComments(
lineGenerator = avoidEmptyComments(
lineGenerator,
helper.lang.singleLineComment,
);
lineGenerator = avoidPathLine(lineGenerator, helper.lang.singleLineComment);
lineGenerator = skipPrefixes(lineGenerator);
// lineGenerator = noTopLevelKeywordsMidline(

View file

@ -31,21 +31,34 @@ describe("lineStream", () => {
});
describe("avoidPathLine", () => {
it("should filter out path lines and empty comments", async () => {
it("should filter out path lines", async () => {
const linesGenerator = await getLineGenerator([
"// Path: src/index.ts",
"const x = 5;",
"//",
"console.log(x);",
]);
const result = lineStream.avoidPathLineAndEmptyComments(
linesGenerator,
"//",
);
const result = lineStream.avoidPathLine(linesGenerator, "//");
const filteredLines = await getFilteredLines(result);
expect(filteredLines).toEqual(["const x = 5;", "//", "console.log(x);"]);
});
});
expect(filteredLines).toEqual(["const x = 5;", "console.log(x);"]);
describe("avoidEmptyComments", () => {
it("should filter out empty comments", async () => {
const linesGenerator = await getLineGenerator([
"// Path: src/index.ts",
"const x = 5;",
"//",
"console.log(x);",
]);
const result = lineStream.avoidEmptyComments(linesGenerator, "//");
const filteredLines = await getFilteredLines(result);
expect(filteredLines).toEqual([
"// Path: src/index.ts",
"const x = 5;",
"console.log(x);",
]);
});
});

View file

@ -122,21 +122,40 @@ export async function* noTopLevelKeywordsMidline(
}
/**
* Filters out unwanted lines from a LineStream, specifically those starting with '// Path: <PATH>' or empty comments.
* Filters out lines starting with '// Path: <PATH>' from a LineStream.
*
* @param {LineStream} stream - The input stream of lines to filter.
* @param {string} comment - The comment syntax to filter (e.g., '//' for JavaScript-style comments).
* @yields {string} The filtered lines, excluding unwanted path lines and empty comments.
* @yields {string} The filtered lines, excluding unwanted path lines.
*/
export async function* avoidPathLineAndEmptyComments(
export async function* avoidPathLine(
stream: LineStream,
comment: string,
): LineStream {
// Snippets are inserted as comments with a line at the start '// Path: <PATH>'.
// Sometimes the model with copy this pattern, which is unwanted
for await (const line of stream) {
// Also filter lines that are empty comments
if (line.startsWith(`${comment} Path: `) || line.trim() === comment) {
if (line.startsWith(`${comment} Path: `)) {
continue;
}
yield line;
}
}
/**
* Filters out empty comment lines from a LineStream.
*
* @param {LineStream} stream - The input stream of lines to filter.
* @param {string} comment - The comment syntax to filter (e.g., '//' for JavaScript-style comments).
* @yields {string} The filtered lines, excluding empty comments.
*/
export async function* avoidEmptyComments(
stream: LineStream,
comment: string,
): LineStream {
// Filter lines that are empty comments
for await (const line of stream) {
if (line.trim() === comment) {
continue;
}
yield line;