Skip to content

Commit 8f512fc

Browse files
committed
fix: fix tuple signatures label display for ts 5.0
1 parent bb609cd commit 8f512fc

File tree

4 files changed

+42
-22
lines changed

4 files changed

+42
-22
lines changed

typescript/src/tupleSignature.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export const getTupleSignature = (node: ts.Node, typeChecker: ts.TypeChecker) =>
4949
if (!/^\d+$/.test(property.name)) return
5050
const type = typeChecker.getTypeOfSymbolAtLocation(property, targetNode!)
5151
let displayString = typeChecker.typeToString(type)
52-
const tupleLabelDeclaration: ts.NamedTupleMember | undefined = property['target']?.['tupleLabelDeclaration']
52+
const tupleLabelDeclaration: ts.NamedTupleMember | undefined =
53+
property['target']?.['tupleLabelDeclaration'] ??
54+
// https://github.com/microsoft/TypeScript/blob/main/src/services/symbolDisplay.ts#L648 (labelDecl)
55+
// todo uncomment after ts-expose-internals update to 5.0
56+
(property as any) /*import('typescript-full').TransientSymbol*/?.links?.target?.links?.tupleLabelDeclaration
5357
const tupleLabel = tupleLabelDeclaration?.name.text
5458
if (tupleLabel) {
5559
displayString = `${tupleLabel}: ${displayString}`

typescript/test/completions.spec.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,17 @@ test('Additional types suggestions', () => {
534534
})
535535
})
536536

537+
test('Tuple signature', () => {
538+
const tester = fourslashLikeTester(/* ts */ `
539+
const [/*1*/] = [] as [a: number]
540+
`)
541+
tester.completion(1, {
542+
exact: {
543+
names: ['a'],
544+
},
545+
})
546+
})
547+
537548
test('Object Literal Completions', () => {
538549
const [_positivePositions, _negativePositions, numPositions] = fileContentsSpecialPositions(/* ts */ `
539550
interface Options {
@@ -684,7 +695,10 @@ test('In Keyword Completions', () => {
684695
const completion = pickObj(getCompletionsAtPosition(pos!, { shouldHave: true })!, 'entriesSorted', 'prevCompletionsMap')
685696
// this test is bad case of demonstrating how it can be used with string in union (IT SHOULDNT!)
686697
// but it is here to ensure this is no previous crash issue, indexes are correct when used only with objects
687-
expect(completion).toMatchInlineSnapshot(`
698+
expect({
699+
...completion,
700+
prevCompletionsMap: Object.entries(completion.prevCompletionsMap).map(([key, v]) => [key, (v.documentationOverride as string).replaceAll('\n', ' ')]),
701+
}).toMatchInlineSnapshot(`
688702
{
689703
"entriesSorted": [
690704
{
@@ -727,19 +741,20 @@ test('In Keyword Completions', () => {
727741
},
728742
},
729743
],
730-
"prevCompletionsMap": {
731-
"a": {
732-
"documentationOverride": "2: boolean
733-
734-
3: number",
735-
},
736-
"☆b": {
737-
"documentationOverride": "2: string",
738-
},
739-
"☆c": {
740-
"documentationOverride": "3: number",
741-
},
742-
},
744+
"prevCompletionsMap": [
745+
[
746+
"a",
747+
"2: boolean 3: number",
748+
],
749+
[
750+
"☆b",
751+
"2: string",
752+
],
753+
[
754+
"☆c",
755+
"3: number",
756+
],
757+
],
743758
}
744759
`)
745760
})

typescript/test/shared.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
/* eslint-disable import/first */
12
beforeAll(() => {
2-
//@ts-ignore plugin expect it to set globallly
3+
//@ts-expect-error plugin expect it to set globallly
34
globalThis.__WEB__ = false
45
})
56

@@ -15,5 +16,6 @@ export const settingsOverride: Partial<Configuration> = {
1516
'arrayMethodsSnippets.enable': true,
1617
'codeActions.extractTypeInferName': true,
1718
'methodSnippets.skip': 'no-skip',
19+
tupleHelpSignature: true,
1820
}
1921
export const defaultConfigFunc = await getDefaultConfigFunc(settingsOverride)

typescript/test/testing.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,16 @@ export const overrideSettings = (newOverrides: Partial<Configuration>) => {
6464
export const fourslashLikeTester = (contents: string, fileName = entrypoint) => {
6565
const [positive, _negative, numberedPositions] = fileContentsSpecialPositions(contents, fileName)
6666

67-
const ranges = positive.reduce(
67+
const ranges = positive.reduce<number[][]>(
6868
(prevRanges, pos) => {
6969
const lastPrev = prevRanges[prevRanges.length - 1]!
7070
if (lastPrev.length < 2) {
7171
lastPrev.push(pos)
7272
return prevRanges
73-
} else {
74-
return [...prevRanges, [pos]]
7573
}
74+
return [...prevRanges, [pos]]
7675
},
77-
[[]] as number[][],
76+
[[]],
7877
)
7978
return {
8079
completion: (marker: number | number[], matcher: CompletionMatcher, meta?) => {
@@ -127,7 +126,7 @@ export const fourslashLikeTester = (contents: string, fileName = entrypoint) =>
127126
)!
128127
const action = actionsGroup.actions.find(action => action.description === refactorName)!
129128
const { edits } = languageService.getEditsForRefactor(fileName, {}, { pos: start, end }, actionsGroup.name, action.name, {})!
130-
const a = tsFull.textChanges.applyChanges(getCurrentFile(), edits![0]!.textChanges)
129+
const a = tsFull.textChanges.applyChanges(getCurrentFile(), edits[0]!.textChanges)
131130
} else {
132131
expect(appliableNames, `at marker ${mark}`).not.toContain(refactorName)
133132
}
@@ -150,7 +149,7 @@ export const fileContentsSpecialPositions = (contents: string, fileName = entryp
150149
let mainMatch = currentMatch[1]!
151150
if (addOnly) mainMatch = mainMatch.slice(0, -1)
152151
const possiblyNum = +mainMatch
153-
if (!isNaN(possiblyNum)) {
152+
if (!Number.isNaN(possiblyNum)) {
154153
addArr[2][possiblyNum] = offset
155154
} else {
156155
addArr[mainMatch === 't' ? '0' : '1'].push(offset)

0 commit comments

Comments
 (0)