From c82ccf1391e441c58047131731bf49b57d0911ba Mon Sep 17 00:00:00 2001 From: TypeScript Bot Date: Tue, 10 Aug 2021 23:20:29 -0700 Subject: [PATCH] Cherry-pick PR #45399 into release-4.4 (#45404) Component commits: f7af8f4a4a fix(45393): show parameter name hints for unary literal expressions Co-authored-by: Oleksandr T --- src/services/inlayHints.ts | 13 ++++++- .../cases/fourslash/inlayHintsShouldWork63.ts | 34 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/inlayHintsShouldWork63.ts diff --git a/src/services/inlayHints.ts b/src/services/inlayHints.ts index 616209024899a..ba278c5f30199 100644 --- a/src/services/inlayHints.ts +++ b/src/services/inlayHints.ts @@ -203,7 +203,18 @@ namespace ts.InlayHints { } function isHintableExpression(node: Node) { - return isLiteralExpression(node) || isBooleanLiteral(node) || isArrowFunction(node) || isFunctionExpression(node) || isObjectLiteralExpression(node) || isArrayLiteralExpression(node); + switch (node.kind) { + case SyntaxKind.PrefixUnaryExpression: + return isLiteralExpression((node as PrefixUnaryExpression).operand); + case SyntaxKind.TrueKeyword: + case SyntaxKind.FalseKeyword: + case SyntaxKind.ArrowFunction: + case SyntaxKind.FunctionExpression: + case SyntaxKind.ObjectLiteralExpression: + case SyntaxKind.ArrayLiteralExpression: + return true; + } + return isLiteralExpression(node); } function visitFunctionDeclarationLikeForReturnType(decl: FunctionDeclaration | ArrowFunction | FunctionExpression | MethodDeclaration | GetAccessorDeclaration) { diff --git a/tests/cases/fourslash/inlayHintsShouldWork63.ts b/tests/cases/fourslash/inlayHintsShouldWork63.ts new file mode 100644 index 0000000000000..e42071876d70f --- /dev/null +++ b/tests/cases/fourslash/inlayHintsShouldWork63.ts @@ -0,0 +1,34 @@ +/// + +////function foo(a: number, b: number, c: number, d: number) {} +////foo(/*a*/1, /*b*/+1, /*c*/-1, /*d*/+"1"); + +const [a, b, c, d] = 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 + } +], undefined, { + includeInlayParameterNameHints: "literals" +});