Skip to content

Use helper functions in a few more places #21308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
6 commits merged into from
Jun 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 3 additions & 10 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down