Skip to content

Commit 7b0e665

Browse files
TypeScript Bota-tarasyuk
TypeScript Bot
andauthored
Cherry-pick PR #45426 into release-4.4 (#45445)
Component commits: 01077c1 fix(45417): show inlay hints for null and literal-like identifiers Co-authored-by: Oleksandr T <[email protected]>
1 parent 55dd850 commit 7b0e665

File tree

5 files changed

+112
-10
lines changed

5 files changed

+112
-10
lines changed

src/compiler/checker.ts

-4
Original file line numberDiff line numberDiff line change
@@ -26284,10 +26284,6 @@ namespace ts {
2628426284
return isTypeAssignableToKind(checkComputedPropertyName(name), TypeFlags.NumberLike);
2628526285
}
2628626286

26287-
function isInfinityOrNaNString(name: string | __String): boolean {
26288-
return name === "Infinity" || name === "-Infinity" || name === "NaN";
26289-
}
26290-
2629126287
function isNumericLiteralName(name: string | __String) {
2629226288
// The intent of numeric names is that
2629326289
// - they are names with text in a numeric form, and that

src/compiler/utilities.ts

+5
Original file line numberDiff line numberDiff line change
@@ -7367,4 +7367,9 @@ namespace ts {
73677367
}
73687368
return false;
73697369
}
7370+
7371+
/* @internal */
7372+
export function isInfinityOrNaNString(name: string | __String): boolean {
7373+
return name === "Infinity" || name === "-Infinity" || name === "NaN";
7374+
}
73707375
}

src/services/classifier2020.ts

-4
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,6 @@ namespace ts.classifier.v2020 {
225225
return (isQualifiedName(node.parent) && node.parent.right === node) || (isPropertyAccessExpression(node.parent) && node.parent.name === node);
226226
}
227227

228-
function isInfinityOrNaNString(name: __String): boolean {
229-
return name === "Infinity" || name === "NaN";
230-
}
231-
232228
const tokenFromDeclarationMapping = new Map<SyntaxKind, TokenType>([
233229
[SyntaxKind.VariableDeclaration, TokenType.variable],
234230
[SyntaxKind.Parameter, TokenType.parameter],

src/services/inlayHints.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,22 @@ namespace ts.InlayHints {
204204

205205
function isHintableExpression(node: Node) {
206206
switch (node.kind) {
207-
case SyntaxKind.PrefixUnaryExpression:
208-
return isLiteralExpression((node as PrefixUnaryExpression).operand);
207+
case SyntaxKind.PrefixUnaryExpression: {
208+
const operand = (node as PrefixUnaryExpression).operand;
209+
return isLiteralExpression(operand) || isIdentifier(operand) && isInfinityOrNaNString(operand.escapedText);
210+
}
209211
case SyntaxKind.TrueKeyword:
210212
case SyntaxKind.FalseKeyword:
211213
case SyntaxKind.ArrowFunction:
212214
case SyntaxKind.FunctionExpression:
213215
case SyntaxKind.ObjectLiteralExpression:
214216
case SyntaxKind.ArrayLiteralExpression:
217+
case SyntaxKind.NullKeyword:
215218
return true;
219+
case SyntaxKind.Identifier: {
220+
const name = (node as Identifier).escapedText;
221+
return isUndefined(name) || isInfinityOrNaNString(name);
222+
}
216223
}
217224
return isLiteralExpression(node);
218225
}
@@ -310,5 +317,9 @@ namespace ts.InlayHints {
310317
printer.writeNode(EmitHint.Unspecified, typeNode, /*sourceFile*/ file, writer);
311318
});
312319
}
320+
321+
function isUndefined(name: __String) {
322+
return name === "undefined";
323+
}
313324
}
314325
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////function foo(
4+
//// a: string,
5+
//// b: undefined,
6+
//// c: null,
7+
//// d: boolean,
8+
//// e: boolean,
9+
//// f: number,
10+
//// g: number,
11+
//// h: number,
12+
//// i: RegExp,
13+
//// j: bigint,
14+
////) {
15+
////}
16+
////
17+
////foo(
18+
//// /*a*/"hello",
19+
//// /*b*/undefined,
20+
//// /*c*/null,
21+
//// /*d*/true,
22+
//// /*e*/false,
23+
//// /*f*/Infinity,
24+
//// /*g*/-Infinity,
25+
//// /*h*/NaN,
26+
//// /*i*//hello/g,
27+
//// /*j*/123n,
28+
////);
29+
30+
const [a, b, c, d, e, f, g, h, i, j] = test.markers();
31+
verify.getInlayHints([
32+
{
33+
text: "a:",
34+
position: a.position,
35+
kind: ts.InlayHintKind.Parameter,
36+
whitespaceAfter: true
37+
},
38+
{
39+
text: "b:",
40+
position: b.position,
41+
kind: ts.InlayHintKind.Parameter,
42+
whitespaceAfter: true
43+
},
44+
{
45+
text: "c:",
46+
position: c.position,
47+
kind: ts.InlayHintKind.Parameter,
48+
whitespaceAfter: true
49+
},
50+
{
51+
text: "d:",
52+
position: d.position,
53+
kind: ts.InlayHintKind.Parameter,
54+
whitespaceAfter: true
55+
},
56+
{
57+
text: "e:",
58+
position: e.position,
59+
kind: ts.InlayHintKind.Parameter,
60+
whitespaceAfter: true
61+
},
62+
{
63+
text: "f:",
64+
position: f.position,
65+
kind: ts.InlayHintKind.Parameter,
66+
whitespaceAfter: true
67+
},
68+
{
69+
text: "g:",
70+
position: g.position,
71+
kind: ts.InlayHintKind.Parameter,
72+
whitespaceAfter: true
73+
},
74+
{
75+
text: "h:",
76+
position: h.position,
77+
kind: ts.InlayHintKind.Parameter,
78+
whitespaceAfter: true
79+
},
80+
{
81+
text: "i:",
82+
position: i.position,
83+
kind: ts.InlayHintKind.Parameter,
84+
whitespaceAfter: true
85+
},
86+
{
87+
text: "j:",
88+
position: j.position,
89+
kind: ts.InlayHintKind.Parameter,
90+
whitespaceAfter: true
91+
}
92+
], undefined, {
93+
includeInlayParameterNameHints: "literals"
94+
});

0 commit comments

Comments
 (0)