Skip to content

Commit 1e8a5bf

Browse files
author
Andy
authored
signatureHelp: Factor out 'isSyntacticOwner' function (microsoft#25518)
1 parent 49468e7 commit 1e8a5bf

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

src/services/signatureHelp.ts

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ namespace ts.SignatureHelp {
3030
}
3131

3232
// Only need to be careful if the user typed a character and signature help wasn't showing.
33-
const shouldCarefullyCheckContext = !!triggerReason && triggerReason.kind === "characterTyped";
33+
const onlyUseSyntacticOwners = !!triggerReason && triggerReason.kind === "characterTyped";
3434

3535
// Bail out quickly in the middle of a string or comment, don't provide signature help unless the user explicitly requested it.
36-
if (shouldCarefullyCheckContext) {
37-
if (isInString(sourceFile, position, startingToken) || isInComment(sourceFile, position)) {
38-
return undefined;
39-
}
36+
if (onlyUseSyntacticOwners && (isInString(sourceFile, position, startingToken) || isInComment(sourceFile, position))) {
37+
return undefined;
4038
}
4139

4240
const argumentInfo = getContainingArgumentInfo(startingToken, position, sourceFile);
@@ -45,7 +43,7 @@ namespace ts.SignatureHelp {
4543
cancellationToken.throwIfCancellationRequested();
4644

4745
// Extra syntactic and semantic filtering of signature help
48-
const candidateInfo = getCandidateInfo(argumentInfo, typeChecker, sourceFile, startingToken, shouldCarefullyCheckContext);
46+
const candidateInfo = getCandidateInfo(argumentInfo, typeChecker, sourceFile, startingToken, onlyUseSyntacticOwners);
4947
cancellationToken.throwIfCancellationRequested();
5048

5149
if (!candidateInfo) {
@@ -66,35 +64,9 @@ namespace ts.SignatureHelp {
6664

6765
const { invocation } = argumentInfo;
6866
if (invocation.kind === InvocationKind.Call) {
69-
if (onlyUseSyntacticOwners) {
70-
if (isCallOrNewExpression(invocation.node)) {
71-
const invocationChildren = invocation.node.getChildren(sourceFile);
72-
switch (startingToken.kind) {
73-
case SyntaxKind.OpenParenToken:
74-
if (!contains(invocationChildren, startingToken)) {
75-
return undefined;
76-
}
77-
break;
78-
case SyntaxKind.CommaToken:
79-
const containingList = findContainingList(startingToken);
80-
if (!containingList || !contains(invocationChildren, findContainingList(startingToken))) {
81-
return undefined;
82-
}
83-
break;
84-
case SyntaxKind.LessThanToken:
85-
if (!lessThanFollowsCalledExpression(startingToken, sourceFile, invocation.node.expression)) {
86-
return undefined;
87-
}
88-
break;
89-
default:
90-
return undefined;
91-
}
92-
}
93-
else {
94-
return undefined;
95-
}
67+
if (onlyUseSyntacticOwners && !isSyntacticOwner(startingToken, invocation.node, sourceFile)) {
68+
return undefined;
9669
}
97-
9870
const candidates: Signature[] = [];
9971
const resolvedSignature = checker.getResolvedSignature(invocation.node, candidates, argumentInfo.argumentCount)!; // TODO: GH#18217
10072
return candidates.length === 0 ? undefined : { candidates, resolvedSignature };
@@ -111,6 +83,23 @@ namespace ts.SignatureHelp {
11183
}
11284
}
11385

86+
function isSyntacticOwner(startingToken: Node, node: CallLikeExpression, sourceFile: SourceFile): boolean {
87+
if (!isCallOrNewExpression(node)) return false;
88+
const invocationChildren = node.getChildren(sourceFile);
89+
switch (startingToken.kind) {
90+
case SyntaxKind.OpenParenToken:
91+
return contains(invocationChildren, startingToken);
92+
case SyntaxKind.CommaToken: {
93+
const containingList = findContainingList(startingToken);
94+
return !!containingList && contains(invocationChildren, containingList);
95+
}
96+
case SyntaxKind.LessThanToken:
97+
return lessThanFollowsCalledExpression(startingToken, sourceFile, node.expression);
98+
default:
99+
return false;
100+
}
101+
}
102+
114103
function createJavaScriptSignatureHelpItems(argumentInfo: ArgumentListInfo, program: Program, cancellationToken: CancellationToken): SignatureHelpItems | undefined {
115104
// See if we can find some symbol with the call expression name that has call signatures.
116105
const expression = getExpressionFromInvocation(argumentInfo.invocation);

0 commit comments

Comments
 (0)