Skip to content

Commit

Permalink
Cherry-pick PR #45426 into release-4.4 (#45445)
Browse files Browse the repository at this point in the history
Component commits:
01077c1 fix(45417): show inlay hints for null and literal-like identifiers

Co-authored-by: Oleksandr T <[email protected]>
  • Loading branch information
TypeScript Bot and a-tarasyuk authored Aug 16, 2021
1 parent 55dd850 commit 7b0e665
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 10 deletions.
4 changes: 0 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26284,10 +26284,6 @@ namespace ts {
return isTypeAssignableToKind(checkComputedPropertyName(name), TypeFlags.NumberLike);
}

function isInfinityOrNaNString(name: string | __String): boolean {
return name === "Infinity" || name === "-Infinity" || name === "NaN";
}

function isNumericLiteralName(name: string | __String) {
// The intent of numeric names is that
// - they are names with text in a numeric form, and that
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7367,4 +7367,9 @@ namespace ts {
}
return false;
}

/* @internal */
export function isInfinityOrNaNString(name: string | __String): boolean {
return name === "Infinity" || name === "-Infinity" || name === "NaN";
}
}
4 changes: 0 additions & 4 deletions src/services/classifier2020.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,6 @@ namespace ts.classifier.v2020 {
return (isQualifiedName(node.parent) && node.parent.right === node) || (isPropertyAccessExpression(node.parent) && node.parent.name === node);
}

function isInfinityOrNaNString(name: __String): boolean {
return name === "Infinity" || name === "NaN";
}

const tokenFromDeclarationMapping = new Map<SyntaxKind, TokenType>([
[SyntaxKind.VariableDeclaration, TokenType.variable],
[SyntaxKind.Parameter, TokenType.parameter],
Expand Down
15 changes: 13 additions & 2 deletions src/services/inlayHints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,22 @@ namespace ts.InlayHints {

function isHintableExpression(node: Node) {
switch (node.kind) {
case SyntaxKind.PrefixUnaryExpression:
return isLiteralExpression((node as PrefixUnaryExpression).operand);
case SyntaxKind.PrefixUnaryExpression: {
const operand = (node as PrefixUnaryExpression).operand;
return isLiteralExpression(operand) || isIdentifier(operand) && isInfinityOrNaNString(operand.escapedText);
}
case SyntaxKind.TrueKeyword:
case SyntaxKind.FalseKeyword:
case SyntaxKind.ArrowFunction:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ObjectLiteralExpression:
case SyntaxKind.ArrayLiteralExpression:
case SyntaxKind.NullKeyword:
return true;
case SyntaxKind.Identifier: {
const name = (node as Identifier).escapedText;
return isUndefined(name) || isInfinityOrNaNString(name);
}
}
return isLiteralExpression(node);
}
Expand Down Expand Up @@ -310,5 +317,9 @@ namespace ts.InlayHints {
printer.writeNode(EmitHint.Unspecified, typeNode, /*sourceFile*/ file, writer);
});
}

function isUndefined(name: __String) {
return name === "undefined";
}
}
}
94 changes: 94 additions & 0 deletions tests/cases/fourslash/inlayHintsShouldWork64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/// <reference path="fourslash.ts" />

////function foo(
//// a: string,
//// b: undefined,
//// c: null,
//// d: boolean,
//// e: boolean,
//// f: number,
//// g: number,
//// h: number,
//// i: RegExp,
//// j: bigint,
////) {
////}
////
////foo(
//// /*a*/"hello",
//// /*b*/undefined,
//// /*c*/null,
//// /*d*/true,
//// /*e*/false,
//// /*f*/Infinity,
//// /*g*/-Infinity,
//// /*h*/NaN,
//// /*i*//hello/g,
//// /*j*/123n,
////);

const [a, b, c, d, e, f, g, h, i, j] = test.markers();
verify.getInlayHints([
{
text: "a:",
position: a.position,
kind: ts.InlayHintKind.Parameter,
whitespaceAfter: true
},
{
text: "b:",
position: b.position,
kind: ts.InlayHintKind.Parameter,
whitespaceAfter: true
},
{
text: "c:",
position: c.position,
kind: ts.InlayHintKind.Parameter,
whitespaceAfter: true
},
{
text: "d:",
position: d.position,
kind: ts.InlayHintKind.Parameter,
whitespaceAfter: true
},
{
text: "e:",
position: e.position,
kind: ts.InlayHintKind.Parameter,
whitespaceAfter: true
},
{
text: "f:",
position: f.position,
kind: ts.InlayHintKind.Parameter,
whitespaceAfter: true
},
{
text: "g:",
position: g.position,
kind: ts.InlayHintKind.Parameter,
whitespaceAfter: true
},
{
text: "h:",
position: h.position,
kind: ts.InlayHintKind.Parameter,
whitespaceAfter: true
},
{
text: "i:",
position: i.position,
kind: ts.InlayHintKind.Parameter,
whitespaceAfter: true
},
{
text: "j:",
position: j.position,
kind: ts.InlayHintKind.Parameter,
whitespaceAfter: true
}
], undefined, {
includeInlayParameterNameHints: "literals"
});

0 comments on commit 7b0e665

Please sign in to comment.