-
Notifications
You must be signed in to change notification settings - Fork 12.2k
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
Fixed a regression related to determining argument index when spread elements are involved #57637
Merged
DanielRosenwasser
merged 6 commits into
microsoft:main
from
Andarist:fix/regression-arg-index
Mar 28, 2024
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
53eac15
Fixed a regression related to determining argument index when spread …
Andarist aab5c0d
format
Andarist 5f8e455
use for-of
Andarist 6aadcd8
Merge remote-tracking branch 'origin/main' into fix/regression-arg-index
Andarist 36cfea0
unify both functions
Andarist e7bd4fd
Pull functions back out to reduce confusion about how to call
jakebailey File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ import { | |
canHaveSymbol, | ||
CheckFlags, | ||
contains, | ||
countWhere, | ||
createPrinterWithRemoveComments, | ||
createTextSpan, | ||
createTextSpanFromBounds, | ||
|
@@ -288,7 +287,7 @@ function getArgumentOrParameterListInfo(node: Node, position: number, sourceFile | |
if (!info) return undefined; | ||
const { list, argumentIndex } = info; | ||
|
||
const argumentCount = getArgumentCount(list, /*ignoreTrailingComma*/ isInString(sourceFile, position, node), checker); | ||
const argumentCount = getArgumentCount(list, checker); | ||
if (argumentIndex !== 0) { | ||
Debug.assertLessThan(argumentIndex, argumentCount); | ||
} | ||
|
@@ -485,8 +484,7 @@ function getArgumentIndex(argumentsList: Node, node: Node, checker: TypeChecker) | |
// The list we got back can include commas. In the presence of errors it may | ||
// also just have nodes without commas. For example "Foo(a b c)" will have 3 | ||
// args without commas. We want to find what index we're at. So we count | ||
// forward until we hit ourselves, only incrementing the index if it isn't a | ||
// comma. | ||
// forward until we hit ourselves. | ||
// | ||
// Note: the subtlety around trailing commas (in getArgumentCount) does not apply | ||
// here. That's because we're only walking forward until we hit the node we're | ||
|
@@ -495,19 +493,30 @@ function getArgumentIndex(argumentsList: Node, node: Node, checker: TypeChecker) | |
// arg index. | ||
const args = argumentsList.getChildren(); | ||
let argumentIndex = 0; | ||
let skipComma = false; | ||
for (let pos = 0; pos < length(args); pos++) { | ||
const child = args[pos]; | ||
if (child === node) { | ||
if (!skipComma && child.kind === SyntaxKind.CommaToken) { | ||
argumentIndex++; | ||
} | ||
break; | ||
} | ||
if (isSpreadElement(child)) { | ||
argumentIndex = argumentIndex + getSpreadElementCount(child, checker) + (pos > 0 ? pos : 0); | ||
argumentIndex += getSpreadElementCount(child, checker); | ||
skipComma = true; | ||
continue; | ||
} | ||
else { | ||
if (child.kind !== SyntaxKind.CommaToken) { | ||
argumentIndex++; | ||
} | ||
if (child.kind !== SyntaxKind.CommaToken) { | ||
argumentIndex++; | ||
skipComma = true; | ||
continue; | ||
} | ||
if (skipComma) { | ||
skipComma = false; | ||
continue; | ||
} | ||
argumentIndex++; | ||
} | ||
return argumentIndex; | ||
} | ||
|
@@ -525,32 +534,35 @@ function getSpreadElementCount(node: SpreadElement, checker: TypeChecker) { | |
return 0; | ||
} | ||
|
||
function getArgumentCount(argumentsList: Node, ignoreTrailingComma: boolean, checker: TypeChecker) { | ||
function getArgumentCount(argumentsList: Node, checker: TypeChecker) { | ||
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// The argument count for a list is normally the number of non-comma children it has. | ||
// For example, if you have "Foo(a,b)" then there will be three children of the arg | ||
// list 'a' '<comma>' 'b'. So, in this case the arg count will be 2. However, there | ||
// is a small subtlety. If you have "Foo(a,)", then the child list will just have | ||
// 'a' '<comma>'. So, in the case where the last child is a comma, we increase the | ||
// arg count by one to compensate. | ||
// | ||
// Note: this subtlety only applies to the last comma. If you had "Foo(a,," then | ||
// we'll have: 'a' '<comma>' '<missing>' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The mention of |
||
// That will give us 2 non-commas. We then add one for the last comma, giving us an | ||
// arg count of 3. | ||
const listChildren = argumentsList.getChildren(); | ||
|
||
const args = argumentsList.getChildren(); | ||
let argumentCount = 0; | ||
for (const child of listChildren) { | ||
let skipComma = false; | ||
for (let pos = 0; pos < length(args); pos++) { | ||
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const child = args[pos]; | ||
if (isSpreadElement(child)) { | ||
argumentCount = argumentCount + getSpreadElementCount(child, checker); | ||
argumentCount += getSpreadElementCount(child, checker); | ||
skipComma = true; | ||
continue; | ||
} | ||
if (child.kind !== SyntaxKind.CommaToken) { | ||
argumentCount++; | ||
skipComma = true; | ||
continue; | ||
} | ||
if (skipComma) { | ||
skipComma = false; | ||
continue; | ||
} | ||
} | ||
|
||
argumentCount = argumentCount + countWhere(listChildren, arg => arg.kind !== SyntaxKind.CommaToken); | ||
if (!ignoreTrailingComma && listChildren.length > 0 && last(listChildren).kind === SyntaxKind.CommaToken) { | ||
argumentCount++; | ||
} | ||
return argumentCount; | ||
return args.length && last(args).kind === SyntaxKind.CommaToken ? argumentCount + 1 : argumentCount; | ||
} | ||
|
||
// spanIndex is either the index for a given template span. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
tests/baselines/reference/signatureHelpRestArgs2.baseline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
// === SignatureHelp === | ||
=== /tests/cases/fourslash/index.js === | ||
// const promisify = function (thisArg, fnName) { | ||
// const fn = thisArg[fnName]; | ||
// return function () { | ||
// return new Promise((resolve) => { | ||
// fn.call(thisArg, ...arguments, ); | ||
// ^ | ||
// | ---------------------------------------------------------------------- | ||
// | Function.call(thisArg: any, **...argArray: any[]**): any | ||
// | Calls a method of an object, substituting another object for the current object. | ||
// | @param thisArg The object to be used as the current object. | ||
// | @param argArray A list of arguments to be passed to the method. | ||
// | ---------------------------------------------------------------------- | ||
// }); | ||
// }; | ||
// }; | ||
|
||
[ | ||
{ | ||
"marker": { | ||
"fileName": "/tests/cases/fourslash/index.js", | ||
"position": 189, | ||
"name": "1" | ||
}, | ||
"item": { | ||
"items": [ | ||
{ | ||
"isVariadic": true, | ||
"prefixDisplayParts": [ | ||
{ | ||
"text": "Function", | ||
"kind": "localName" | ||
}, | ||
{ | ||
"text": ".", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": "call", | ||
"kind": "methodName" | ||
}, | ||
{ | ||
"text": "(", | ||
"kind": "punctuation" | ||
} | ||
], | ||
"suffixDisplayParts": [ | ||
{ | ||
"text": ")", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": ":", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "any", | ||
"kind": "keyword" | ||
} | ||
], | ||
"separatorDisplayParts": [ | ||
{ | ||
"text": ",", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
} | ||
], | ||
"parameters": [ | ||
{ | ||
"name": "thisArg", | ||
"documentation": [ | ||
{ | ||
"text": "The object to be used as the current object.", | ||
"kind": "text" | ||
} | ||
], | ||
"displayParts": [ | ||
{ | ||
"text": "thisArg", | ||
"kind": "parameterName" | ||
}, | ||
{ | ||
"text": ":", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "any", | ||
"kind": "keyword" | ||
} | ||
], | ||
"isOptional": false, | ||
"isRest": false | ||
}, | ||
{ | ||
"name": "argArray", | ||
"documentation": [ | ||
{ | ||
"text": "A list of arguments to be passed to the method.", | ||
"kind": "text" | ||
} | ||
], | ||
"displayParts": [ | ||
{ | ||
"text": "...", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": "argArray", | ||
"kind": "parameterName" | ||
}, | ||
{ | ||
"text": ":", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "any", | ||
"kind": "keyword" | ||
}, | ||
{ | ||
"text": "[", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": "]", | ||
"kind": "punctuation" | ||
} | ||
], | ||
"isOptional": false, | ||
"isRest": false | ||
} | ||
], | ||
"documentation": [ | ||
{ | ||
"text": "Calls a method of an object, substituting another object for the current object.", | ||
"kind": "text" | ||
} | ||
], | ||
"tags": [ | ||
{ | ||
"name": "param", | ||
"text": [ | ||
{ | ||
"text": "thisArg", | ||
"kind": "parameterName" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "The object to be used as the current object.", | ||
"kind": "text" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "param", | ||
"text": [ | ||
{ | ||
"text": "argArray", | ||
"kind": "parameterName" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "A list of arguments to be passed to the method.", | ||
"kind": "text" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
], | ||
"applicableSpan": { | ||
"start": 166, | ||
"length": 23 | ||
}, | ||
"selectedItemIndex": 0, | ||
"argumentIndex": 1, | ||
"argumentCount": 2 | ||
} | ||
} | ||
] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this part of the comment because I adjusted the behavior (see
tests/cases/fourslash/signatureHelpSkippedArgs1.ts
). I find the new behavior better and it was easier for me to rewrite those loops while accommodating for that test case.