Skip to content

feat(49385): Inlay hint for type of literal-initialized constant is unnecessary #49412

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

Merged
merged 1 commit into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/services/inlayHints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ namespace ts.InlayHints {
}

function visitVariableLikeDeclaration(decl: VariableDeclaration | PropertyDeclaration) {
if (!decl.initializer || isBindingPattern(decl.name)) {
if (!decl.initializer || isBindingPattern(decl.name) || isVariableDeclaration(decl) && !isHintableDeclaration(decl)) {
return;
}

Expand Down Expand Up @@ -272,8 +272,11 @@ namespace ts.InlayHints {

for (let i = 0; i < node.parameters.length && i < signature.parameters.length; ++i) {
const param = node.parameters[i];
const effectiveTypeAnnotation = getEffectiveTypeAnnotationNode(param);
if (!isHintableDeclaration(param)) {
continue;
}

const effectiveTypeAnnotation = getEffectiveTypeAnnotationNode(param);
if (effectiveTypeAnnotation) {
continue;
}
Expand Down Expand Up @@ -323,5 +326,13 @@ namespace ts.InlayHints {
function isUndefined(name: __String) {
return name === "undefined";
}

function isHintableDeclaration(node: VariableDeclaration | ParameterDeclaration) {
if ((isParameterDeclaration(node) || isVariableDeclaration(node) && isVarConst(node)) && node.initializer) {
const initializer = skipParentheses(node.initializer);
return !(isHintableLiteral(initializer) || isNewExpression(initializer) || isObjectLiteralExpression(initializer) || isAssertionExpression(initializer));
}
return true;
}
}
}
30 changes: 20 additions & 10 deletions tests/cases/fourslash/inlayHintsShouldWork15.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
/// <reference path="fourslash.ts" />

//// const a/*a*/ = 123;
////class C {}
////namespace N { export class Foo {} }
////interface Foo {}

const markers = test.markers();
verify.getInlayHints([
{
text: ': 123',
position: markers[0].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
], undefined, {
////const a = "a";
////const b = 1;
////const c = true;
////
////const d = {} as Foo;
////const e = <Foo>{};
////const f = {} as const;
////const g = (({} as const));
////
////const h = new C();
////const i = new N.C();
////const j = ((((new C()))));
////
////const k = { a: 1, b: 1 };
////const l = ((({ a: 1, b: 1 })));

verify.getInlayHints([], undefined, {
includeInlayVariableTypeHints: true
});
15 changes: 0 additions & 15 deletions tests/cases/fourslash/inlayHintsShouldWork16.ts

This file was deleted.

12 changes: 2 additions & 10 deletions tests/cases/fourslash/inlayHintsShouldWork17.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
/// <reference path="fourslash.ts" />

//// const a/*a*/ = { a: 123 };
//// const a = { a: 123 };

const markers = test.markers();
verify.getInlayHints([
{
text: ': { a: number; }',
position: markers[0].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
], undefined, {
verify.getInlayHints([], undefined, {
includeInlayVariableTypeHints: true
});
12 changes: 2 additions & 10 deletions tests/cases/fourslash/inlayHintsShouldWork18.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
/// <reference path="fourslash.ts" />

//// class Class {}
//// const a/*a*/ = new Class();
//// const a = new Class();

const markers = test.markers();
verify.getInlayHints([
{
text: ': Class',
position: markers[0].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
], undefined, {
verify.getInlayHints([], undefined, {
includeInlayVariableTypeHints: true
});
12 changes: 2 additions & 10 deletions tests/cases/fourslash/inlayHintsShouldWork22.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
/// <reference path="fourslash.ts" />

//// const a/*a*/ = "I'm very very very very very very very very very long";
////const a = "I'm very very very very very very very very very long";

const markers = test.markers();
verify.getInlayHints([
{
text: `: "I'm very very very very ve...`,
position: markers[0].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
], undefined, {
verify.getInlayHints([], undefined, {
includeInlayVariableTypeHints: true
});
20 changes: 7 additions & 13 deletions tests/cases/fourslash/inlayHintsShouldWork56.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
/// <reference path="fourslash.ts" />

//// const object/*a*/ = { foo: 1, bar: 2 }
//// const array/*b*/ = [1, 2]
//// const a/*c*/ = object;
//// const object = { foo: 1, bar: 2 }
//// const array/*a*/ = [1, 2]
//// const a/*b*/ = object;
//// const { foo, bar } = object;
//// const {} = object;
//// const b/*d*/ = array;
//// const b/*c*/ = array;
//// const [ first, second ] = array;
//// const [] = array;

const markers = test.markers();
verify.getInlayHints([
{
text: ': { foo: number; bar: number; }',
position: markers[0].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
{
text: ': number[]',
position: markers[1].position,
position: markers[0].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
{
text: ': { foo: number; bar: number; }',
position: markers[2].position,
position: markers[1].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
{
text: ': number[]',
position: markers[3].position,
position: markers[2].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
}
Expand Down
10 changes: 2 additions & 8 deletions tests/cases/fourslash/inlayHintsShouldWork65.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
/// <reference path="fourslash.ts" />

//// type F = (a: string, b: number) => void
//// const f: F = (a/*a*/, b/*b*/ = 1) => { }
//// const f: F = (a/*a*/, b = 1) => { }

const [a, b] = test.markers();
const [a] = test.markers();
verify.getInlayHints([
{
text: ': string',
position: a.position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
{
text: ': number',
position: b.position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
}
], undefined, {
includeInlayFunctionParameterTypeHints: true
Expand Down
25 changes: 25 additions & 0 deletions tests/cases/fourslash/inlayHintsShouldWork69.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path="fourslash.ts" />

////class C {}
////namespace N { export class Foo {} }
////interface Foo {}

////function f1(a = 1) {}
////function f2(a = "a") {}
////function f3(a = true) {}

////function f4(a = { } as Foo) {}
////function f5(a = <Foo>{}) {}
////function f6(a = {} as const) {}
////function f7(a = (({} as const))) {}

////function f8(a = new C()) {}
////function f9(a = new N.C()) {}
////function f10(a = ((((new C()))))) {}

////function f11(a = { a: 1, b: 1 }) {}
////function f12(a = ((({ a: 1, b: 1 })))) {}

verify.getInlayHints([], undefined, {
includeInlayFunctionParameterTypeHints: true
});