Skip to content

Commit 1d808d2

Browse files
authored
Merge pull request #6 from supermemoryai/12-17-fix_remove_non-null_assertions_in_rebuild.ts
fix: remove non-null assertions in rebuild.ts
2 parents aed0e21 + b3e0cf2 commit 1d808d2

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/chunking/rebuild.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,15 @@ export const rebuildText = (window: ASTWindow, code: string): RebuiltText => {
8585

8686
// Normal case: slice from first node start to last node end
8787
// Use startPosition/endPosition from nodes for optimized line calculation
88-
const firstNode = window.nodes[0]!
89-
const lastNode = window.nodes[window.nodes.length - 1]!
88+
const firstNode = window.nodes[0]
89+
const lastNode = window.nodes[window.nodes.length - 1]
90+
if (!firstNode || !lastNode) {
91+
return {
92+
text: '',
93+
byteRange: { start: 0, end: 0 },
94+
lineRange: { start: 0, end: 0 },
95+
}
96+
}
9097

9198
const startByte = firstNode.startIndex
9299
const endByte = lastNode.endIndex
@@ -114,20 +121,36 @@ const rebuildFromLineRanges = (
114121
window: ASTWindow,
115122
code: string,
116123
): RebuiltText => {
117-
const lineRanges = window.lineRanges!
124+
const lineRanges = window.lineRanges
125+
if (!lineRanges || lineRanges.length === 0) {
126+
return {
127+
text: '',
128+
byteRange: { start: 0, end: 0 },
129+
lineRange: { start: 0, end: 0 },
130+
}
131+
}
118132
const lineStarts = buildLineStartsTable(code)
119133

120134
// Get the overall line range
121-
const firstRange = lineRanges[0]!
122-
const lastRange = lineRanges[lineRanges.length - 1]!
135+
const firstRange = lineRanges[0]
136+
const lastRange = lineRanges[lineRanges.length - 1]
137+
if (!firstRange || !lastRange) {
138+
return {
139+
text: '',
140+
byteRange: { start: 0, end: 0 },
141+
lineRange: { start: 0, end: 0 },
142+
}
143+
}
123144
const startLine = firstRange.start
124145
const endLine = lastRange.end
125146

126147
// Calculate byte offsets from line numbers
127148
const startByte = lineStarts[startLine] ?? 0
128149
// End byte is start of line after endLine, or end of file
129150
const endByte =
130-
endLine + 1 < lineStarts.length ? lineStarts[endLine + 1]! : code.length
151+
endLine + 1 < lineStarts.length
152+
? (lineStarts[endLine + 1] ?? code.length)
153+
: code.length
131154

132155
const text = code.slice(startByte, endByte)
133156

0 commit comments

Comments
 (0)