Skip to content

Commit 275e3c6

Browse files
committed
Fix CI type errors for annotation tests
1 parent ddd16d2 commit 275e3c6

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

packages/codehike/src/code/extract-annotations.test.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ async function t(comment: string) {
77
return annotations[0]
88
}
99

10+
function getBlockRange(annotation: { ranges: any[] }, index = 0) {
11+
const range = annotation.ranges[index]
12+
if (!("fromLineNumber" in range) || !("toLineNumber" in range)) {
13+
throw new Error("Expected block range")
14+
}
15+
return range
16+
}
17+
1018
test("extracts basic annotation name", async () => {
1119
const annotation = await t("!foo bar")
1220
expect(annotation.name).toEqual("foo")
@@ -80,7 +88,7 @@ test("start/end creates block annotation spanning the range", async () => {
8088
const a = annotations[0]
8189
expect(a.name).toEqual("focus")
8290
expect(a.ranges).toHaveLength(1)
83-
const range = a.ranges[0]
91+
const range = getBlockRange(a)
8492
// after removing 2 comment lines, the code is 4 lines
8593
// "let b = 2" is line 2, "let c = 3" is line 3
8694
expect(range.fromLineNumber).toEqual(2)
@@ -123,8 +131,9 @@ test("start/end works with other annotations", async () => {
123131
const focus = annotations.find((a) => a.name === "focus")
124132
expect(mark).toBeDefined()
125133
expect(focus).toBeDefined()
126-
expect(focus!.ranges[0].fromLineNumber).toBeDefined()
127-
expect(focus!.ranges[0].toLineNumber).toBeDefined()
134+
const range = getBlockRange(focus!)
135+
expect(range.fromLineNumber).toBeDefined()
136+
expect(range.toLineNumber).toBeDefined()
128137
})
129138

130139
test("multiple start/end pairs of same name", async () => {
@@ -148,8 +157,8 @@ test("multiple start/end pairs of same name", async () => {
148157
expect(annotations[0].name).toEqual("focus")
149158
expect(annotations[1].name).toEqual("focus")
150159
// The two ranges should not overlap
151-
const r0 = annotations[0].ranges[0]
152-
const r1 = annotations[1].ranges[0]
160+
const r0 = getBlockRange(annotations[0])
161+
const r1 = getBlockRange(annotations[1])
153162
expect(r0.toLineNumber).toBeLessThan(r1.fromLineNumber)
154163
})
155164

@@ -262,8 +271,9 @@ test("start/end works with Python comments", async () => {
262271
const { annotations } = await splitAnnotationsAndCode(code, "python", "!")
263272
expect(annotations).toHaveLength(1)
264273
expect(annotations[0].name).toEqual("focus")
265-
expect(annotations[0].ranges[0].fromLineNumber).toEqual(2)
266-
expect(annotations[0].ranges[0].toLineNumber).toEqual(3)
274+
const range = getBlockRange(annotations[0])
275+
expect(range.fromLineNumber).toEqual(2)
276+
expect(range.toLineNumber).toEqual(3)
267277
})
268278

269279
test("start/end works with block comments", async () => {

packages/codehike/src/code/extract-annotations.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ async function extractCommentAnnotations(
121121
// Handle start/end range markers: !name(start) and !name(end)
122122
if (rangeString === "(start)") {
123123
query = START_MARKER + query
124-
rangeString = undefined
124+
rangeString = "(1)"
125125
} else if (rangeString === "(end)") {
126126
query = END_MARKER + query
127-
rangeString = undefined
127+
rangeString = "(1)"
128128
}
129129

130130
return {

0 commit comments

Comments
 (0)