Skip to content

Commit 1ca86d0

Browse files
Fix cross-file position handling in printer utilities to prevent panic when processing type predicates with declaration maps
Co-authored-by: RyanCavanaugh <[email protected]>
1 parent c08e01a commit 1ca86d0

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

internal/printer/utilities.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,9 @@ func getStartPositionOfRange(r core.TextRange, sourceFile *ast.SourceFile, inclu
360360
}
361361
text := sourceFile.Text()
362362
pos := r.Pos()
363-
// Bounds check: if position is beyond text length, it's likely from a different file
363+
// Safety check: if position exceeds text length, it might be from a different file
364364
if pos >= len(text) {
365-
return pos // Return the original position without trying to skip trivia
365+
return pos // Return the original position without attempting to skip trivia
366366
}
367367
return scanner.SkipTriviaEx(text, pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
368368
}
@@ -394,31 +394,19 @@ func getLinesBetweenRangeEndAndRangeStart(range1 core.TextRange, range2 core.Tex
394394
}
395395

396396
func getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(pos int, stopPos int, sourceFile *ast.SourceFile, includeComments bool) int {
397-
text := sourceFile.Text()
398-
if pos >= len(text) {
399-
return 0 // Can't determine line differences for out-of-bounds positions
400-
}
401-
startPos := scanner.SkipTriviaEx(text, pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
397+
startPos := scanner.SkipTriviaEx(sourceFile.Text(), pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
402398
prevPos := getPreviousNonWhitespacePosition(startPos, stopPos, sourceFile)
403399
return getLinesBetweenPositions(sourceFile, core.IfElse(prevPos >= 0, prevPos, stopPos), startPos)
404400
}
405401

406402
func getLinesBetweenPositionAndNextNonWhitespaceCharacter(pos int, stopPos int, sourceFile *ast.SourceFile, includeComments bool) int {
407-
text := sourceFile.Text()
408-
if pos >= len(text) {
409-
return 0 // Can't determine line differences for out-of-bounds positions
410-
}
411-
nextPos := scanner.SkipTriviaEx(text, pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
403+
nextPos := scanner.SkipTriviaEx(sourceFile.Text(), pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
412404
return getLinesBetweenPositions(sourceFile, pos, core.IfElse(stopPos < nextPos, stopPos, nextPos))
413405
}
414406

415407
func getPreviousNonWhitespacePosition(pos int, stopPos int, sourceFile *ast.SourceFile) int {
416-
text := sourceFile.Text()
417408
for ; pos >= stopPos; pos-- {
418-
if pos >= len(text) {
419-
continue // Skip out-of-bounds positions
420-
}
421-
if !stringutil.IsWhiteSpaceLike(rune(text[pos])) {
409+
if !stringutil.IsWhiteSpaceLike(rune(sourceFile.Text()[pos])) {
422410
return pos
423411
}
424412
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @filename: /src/predicateExport.ts
2+
export function createPredicate() {
3+
return (_item: unknown): _item is boolean => {
4+
return true;
5+
};
6+
}
7+
8+
// @filename: /src/predicateImport.ts
9+
import { createPredicate } from './predicateExport';
10+
export const predicate = createPredicate();

0 commit comments

Comments
 (0)