mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-12 20:01:02 -05:00
Use helper functions in a few more places (#21308)
* Use helper functions in a few more places * Fix typo * Update API (#24966)
This commit is contained in:
@@ -4374,6 +4374,11 @@ namespace ts {
|
||||
return position >= span.start && position < textSpanEnd(span);
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
export function textRangeContainsPositionInclusive(span: TextRange, position: number): boolean {
|
||||
return position >= span.pos && position <= span.end;
|
||||
}
|
||||
|
||||
// Returns true if 'span' contains 'other'.
|
||||
export function textSpanContainsTextSpan(span: TextSpan, other: TextSpan) {
|
||||
return other.start >= span.start && textSpanEnd(other) <= textSpanEnd(span);
|
||||
|
||||
@@ -271,7 +271,7 @@ namespace ts.GoToDefinition {
|
||||
}
|
||||
|
||||
export function findReferenceInPosition(refs: ReadonlyArray<FileReference>, pos: number): FileReference | undefined {
|
||||
return find(refs, ref => ref.pos <= pos && pos <= ref.end);
|
||||
return find(refs, ref => textRangeContainsPositionInclusive(ref, pos));
|
||||
}
|
||||
|
||||
function getDefinitionInfoForFileReference(name: string, targetFileName: string): DefinitionInfo {
|
||||
|
||||
@@ -729,21 +729,14 @@ namespace ts {
|
||||
// this is token that starts at the end of previous token - return it
|
||||
return n;
|
||||
}
|
||||
|
||||
const children = n.getChildren();
|
||||
for (const child of children) {
|
||||
return firstDefined(n.getChildren(), child => {
|
||||
const shouldDiveInChildNode =
|
||||
// previous token is enclosed somewhere in the child
|
||||
(child.pos <= previousToken.pos && child.end > previousToken.end) ||
|
||||
// previous token ends exactly at the beginning of child
|
||||
(child.pos === previousToken.end);
|
||||
|
||||
if (shouldDiveInChildNode && nodeHasTokens(child, sourceFile)) {
|
||||
return find(child);
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
return shouldDiveInChildNode && nodeHasTokens(child, sourceFile) ? find(child) : undefined;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6627,6 +6627,7 @@ declare namespace ts {
|
||||
function textSpanEnd(span: TextSpan): number;
|
||||
function textSpanIsEmpty(span: TextSpan): boolean;
|
||||
function textSpanContainsPosition(span: TextSpan, position: number): boolean;
|
||||
function textRangeContainsPositionInclusive(span: TextRange, position: number): boolean;
|
||||
function textSpanContainsTextSpan(span: TextSpan, other: TextSpan): boolean;
|
||||
function textSpanOverlapsWith(span: TextSpan, other: TextSpan): boolean;
|
||||
function textSpanOverlap(span1: TextSpan, span2: TextSpan): TextSpan | undefined;
|
||||
|
||||
Reference in New Issue
Block a user