Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/integrations/misc/__tests__/read-lines.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,21 @@ describe("nthline", () => {

it("should throw error for negative to_line", async () => {
await expect(readLines(testFile, -3)).rejects.toThrow(
"Invalid endLine: -3. Line numbers must be non-negative integers.",
"startLine (0) must be less than or equal to endLine (-3)",
)
})

it("should throw error for negative from_line", async () => {
await expect(readLines(testFile, 3, -1)).rejects.toThrow(
"Invalid startLine: -1. Line numbers must be non-negative integers.",
)
it("should handle negative from_line by clamping to 0", async () => {
const lines = await readLines(testFile, 3, -1)
expect(lines).toEqual(["Line 1", "Line 2", "Line 3", "Line 4"].join("\n"))
})

it("should throw error for non-integer line numbers", async () => {
await expect(readLines(testFile, 3, 1.5)).rejects.toThrow(
"Invalid startLine: 1.5. Line numbers must be non-negative integers.",
)
await expect(readLines(testFile, 3.5)).rejects.toThrow(
"Invalid endLine: 3.5. Line numbers must be non-negative integers.",
)
it("should floor non-integer line numbers", async () => {
const linesWithNonIntegerStart = await readLines(testFile, 3, 1.5)
expect(linesWithNonIntegerStart).toEqual(["Line 2", "Line 3", "Line 4"].join("\n"))

const linesWithNonIntegerEnd = await readLines(testFile, 3.5)
expect(linesWithNonIntegerEnd).toEqual(["Line 1", "Line 2", "Line 3", "Line 4"].join("\n"))
})

it("should throw error when from_line > to_line", async () => {
Expand Down
25 changes: 16 additions & 9 deletions src/integrations/misc/read-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@ const outOfRangeError = (filepath: string, n: number) => {
*/
export function readLines(filepath: string, endLine?: number, startLine?: number): Promise<string> {
return new Promise((resolve, reject) => {
// Validate input parameters
// Check startLine validity if provided
if (startLine !== undefined && (startLine < 0 || startLine % 1 !== 0)) {
return reject(
new RangeError(`Invalid startLine: ${startLine}. Line numbers must be non-negative integers.`),
)
// Reject if startLine is defined but not a number
if (startLine !== undefined && typeof startLine !== "number") {
return reject(new RangeError(`Invalid startLine: ${startLine}. Line numbers must be numbers.`))
}

// Force startLine to be an integer and clamp to 0 if negative
if (startLine !== undefined) {
startLine = Math.max(0, Math.floor(startLine))
}

// Reject if endLine is defined but not a number
if (endLine !== undefined && typeof endLine !== "number") {
return reject(new RangeError(`Invalid endLine: ${endLine}. Line numbers must be numbers.`))
}

// Check endLine validity if provided
if (endLine !== undefined && (endLine < 0 || endLine % 1 !== 0)) {
return reject(new RangeError(`Invalid endLine: ${endLine}. Line numbers must be non-negative integers.`))
// Force endLine to be an integer
if (endLine !== undefined) {
endLine = Math.floor(endLine)
}

const effectiveStartLine = startLine === undefined ? 0 : startLine
Expand Down