Skip to content

Commit a1746d4

Browse files
author
Andy
authored
Use helper functions in a few more places (#21308)
* Use helper functions in a few more places * Fix typo * Update API (#24966)
1 parent ded4465 commit a1746d4

File tree

4 files changed

+10
-11
lines changed

4 files changed

+10
-11
lines changed

src/compiler/utilities.ts

+5
Original file line numberDiff line numberDiff line change
@@ -4374,6 +4374,11 @@ namespace ts {
43744374
return position >= span.start && position < textSpanEnd(span);
43754375
}
43764376

4377+
/* @internal */
4378+
export function textRangeContainsPositionInclusive(span: TextRange, position: number): boolean {
4379+
return position >= span.pos && position <= span.end;
4380+
}
4381+
43774382
// Returns true if 'span' contains 'other'.
43784383
export function textSpanContainsTextSpan(span: TextSpan, other: TextSpan) {
43794384
return other.start >= span.start && textSpanEnd(other) <= textSpanEnd(span);

src/services/goToDefinition.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ namespace ts.GoToDefinition {
271271
}
272272

273273
export function findReferenceInPosition(refs: ReadonlyArray<FileReference>, pos: number): FileReference | undefined {
274-
return find(refs, ref => ref.pos <= pos && pos <= ref.end);
274+
return find(refs, ref => textRangeContainsPositionInclusive(ref, pos));
275275
}
276276

277277
function getDefinitionInfoForFileReference(name: string, targetFileName: string): DefinitionInfo {

src/services/utilities.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -729,21 +729,14 @@ namespace ts {
729729
// this is token that starts at the end of previous token - return it
730730
return n;
731731
}
732-
733-
const children = n.getChildren();
734-
for (const child of children) {
732+
return firstDefined(n.getChildren(), child => {
735733
const shouldDiveInChildNode =
736734
// previous token is enclosed somewhere in the child
737735
(child.pos <= previousToken.pos && child.end > previousToken.end) ||
738736
// previous token ends exactly at the beginning of child
739737
(child.pos === previousToken.end);
740-
741-
if (shouldDiveInChildNode && nodeHasTokens(child, sourceFile)) {
742-
return find(child);
743-
}
744-
}
745-
746-
return undefined;
738+
return shouldDiveInChildNode && nodeHasTokens(child, sourceFile) ? find(child) : undefined;
739+
});
747740
}
748741
}
749742

tests/baselines/reference/api/tsserverlibrary.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6627,6 +6627,7 @@ declare namespace ts {
66276627
function textSpanEnd(span: TextSpan): number;
66286628
function textSpanIsEmpty(span: TextSpan): boolean;
66296629
function textSpanContainsPosition(span: TextSpan, position: number): boolean;
6630+
function textRangeContainsPositionInclusive(span: TextRange, position: number): boolean;
66306631
function textSpanContainsTextSpan(span: TextSpan, other: TextSpan): boolean;
66316632
function textSpanOverlapsWith(span: TextSpan, other: TextSpan): boolean;
66326633
function textSpanOverlap(span1: TextSpan, span2: TextSpan): TextSpan | undefined;

0 commit comments

Comments
 (0)