Skip to content

Commit 180bc4c

Browse files
authored
feat(49385): forbid inlay hints for some kind of initialized declarations (microsoft#49412)
.
1 parent 18ac372 commit 180bc4c

9 files changed

+73
-78
lines changed

src/services/inlayHints.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ namespace ts.InlayHints {
121121
}
122122

123123
function visitVariableLikeDeclaration(decl: VariableDeclaration | PropertyDeclaration) {
124-
if (!decl.initializer || isBindingPattern(decl.name)) {
124+
if (!decl.initializer || isBindingPattern(decl.name) || isVariableDeclaration(decl) && !isHintableDeclaration(decl)) {
125125
return;
126126
}
127127

@@ -272,8 +272,11 @@ namespace ts.InlayHints {
272272

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

279+
const effectiveTypeAnnotation = getEffectiveTypeAnnotationNode(param);
277280
if (effectiveTypeAnnotation) {
278281
continue;
279282
}
@@ -323,5 +326,13 @@ namespace ts.InlayHints {
323326
function isUndefined(name: __String) {
324327
return name === "undefined";
325328
}
329+
330+
function isHintableDeclaration(node: VariableDeclaration | ParameterDeclaration) {
331+
if ((isParameterDeclaration(node) || isVariableDeclaration(node) && isVarConst(node)) && node.initializer) {
332+
const initializer = skipParentheses(node.initializer);
333+
return !(isHintableLiteral(initializer) || isNewExpression(initializer) || isObjectLiteralExpression(initializer) || isAssertionExpression(initializer));
334+
}
335+
return true;
336+
}
326337
}
327338
}
Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
/// <reference path="fourslash.ts" />
22

3-
//// const a/*a*/ = 123;
3+
////class C {}
4+
////namespace N { export class Foo {} }
5+
////interface Foo {}
46

5-
const markers = test.markers();
6-
verify.getInlayHints([
7-
{
8-
text: ': 123',
9-
position: markers[0].position,
10-
kind: ts.InlayHintKind.Type,
11-
whitespaceBefore: true
12-
},
13-
], undefined, {
7+
////const a = "a";
8+
////const b = 1;
9+
////const c = true;
10+
////
11+
////const d = {} as Foo;
12+
////const e = <Foo>{};
13+
////const f = {} as const;
14+
////const g = (({} as const));
15+
////
16+
////const h = new C();
17+
////const i = new N.C();
18+
////const j = ((((new C()))));
19+
////
20+
////const k = { a: 1, b: 1 };
21+
////const l = ((({ a: 1, b: 1 })));
22+
23+
verify.getInlayHints([], undefined, {
1424
includeInlayVariableTypeHints: true
1525
});

tests/cases/fourslash/inlayHintsShouldWork16.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
/// <reference path="fourslash.ts" />
22

3-
//// const a/*a*/ = { a: 123 };
3+
//// const a = { a: 123 };
44

5-
const markers = test.markers();
6-
verify.getInlayHints([
7-
{
8-
text: ': { a: number; }',
9-
position: markers[0].position,
10-
kind: ts.InlayHintKind.Type,
11-
whitespaceBefore: true
12-
},
13-
], undefined, {
5+
verify.getInlayHints([], undefined, {
146
includeInlayVariableTypeHints: true
157
});
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
/// <reference path="fourslash.ts" />
22

33
//// class Class {}
4-
//// const a/*a*/ = new Class();
4+
//// const a = new Class();
55

6-
const markers = test.markers();
7-
verify.getInlayHints([
8-
{
9-
text: ': Class',
10-
position: markers[0].position,
11-
kind: ts.InlayHintKind.Type,
12-
whitespaceBefore: true
13-
},
14-
], undefined, {
6+
verify.getInlayHints([], undefined, {
157
includeInlayVariableTypeHints: true
168
});
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
/// <reference path="fourslash.ts" />
22

3-
//// const a/*a*/ = "I'm very very very very very very very very very long";
3+
////const a = "I'm very very very very very very very very very long";
44

5-
const markers = test.markers();
6-
verify.getInlayHints([
7-
{
8-
text: `: "I'm very very very very ve...`,
9-
position: markers[0].position,
10-
kind: ts.InlayHintKind.Type,
11-
whitespaceBefore: true
12-
},
13-
], undefined, {
5+
verify.getInlayHints([], undefined, {
146
includeInlayVariableTypeHints: true
157
});

tests/cases/fourslash/inlayHintsShouldWork56.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
/// <reference path="fourslash.ts" />
22

3-
//// const object/*a*/ = { foo: 1, bar: 2 }
4-
//// const array/*b*/ = [1, 2]
5-
//// const a/*c*/ = object;
3+
//// const object = { foo: 1, bar: 2 }
4+
//// const array/*a*/ = [1, 2]
5+
//// const a/*b*/ = object;
66
//// const { foo, bar } = object;
77
//// const {} = object;
8-
//// const b/*d*/ = array;
8+
//// const b/*c*/ = array;
99
//// const [ first, second ] = array;
1010
//// const [] = array;
1111

1212
const markers = test.markers();
1313
verify.getInlayHints([
14-
{
15-
text: ': { foo: number; bar: number; }',
16-
position: markers[0].position,
17-
kind: ts.InlayHintKind.Type,
18-
whitespaceBefore: true
19-
},
2014
{
2115
text: ': number[]',
22-
position: markers[1].position,
16+
position: markers[0].position,
2317
kind: ts.InlayHintKind.Type,
2418
whitespaceBefore: true
2519
},
2620
{
2721
text: ': { foo: number; bar: number; }',
28-
position: markers[2].position,
22+
position: markers[1].position,
2923
kind: ts.InlayHintKind.Type,
3024
whitespaceBefore: true
3125
},
3226
{
3327
text: ': number[]',
34-
position: markers[3].position,
28+
position: markers[2].position,
3529
kind: ts.InlayHintKind.Type,
3630
whitespaceBefore: true
3731
}

tests/cases/fourslash/inlayHintsShouldWork65.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
/// <reference path="fourslash.ts" />
22

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

6-
const [a, b] = test.markers();
6+
const [a] = test.markers();
77
verify.getInlayHints([
88
{
99
text: ': string',
1010
position: a.position,
1111
kind: ts.InlayHintKind.Type,
1212
whitespaceBefore: true
13-
},
14-
{
15-
text: ': number',
16-
position: b.position,
17-
kind: ts.InlayHintKind.Type,
18-
whitespaceBefore: true
1913
}
2014
], undefined, {
2115
includeInlayFunctionParameterTypeHints: true
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////class C {}
4+
////namespace N { export class Foo {} }
5+
////interface Foo {}
6+
7+
////function f1(a = 1) {}
8+
////function f2(a = "a") {}
9+
////function f3(a = true) {}
10+
11+
////function f4(a = { } as Foo) {}
12+
////function f5(a = <Foo>{}) {}
13+
////function f6(a = {} as const) {}
14+
////function f7(a = (({} as const))) {}
15+
16+
////function f8(a = new C()) {}
17+
////function f9(a = new N.C()) {}
18+
////function f10(a = ((((new C()))))) {}
19+
20+
////function f11(a = { a: 1, b: 1 }) {}
21+
////function f12(a = ((({ a: 1, b: 1 })))) {}
22+
23+
verify.getInlayHints([], undefined, {
24+
includeInlayFunctionParameterTypeHints: true
25+
});

0 commit comments

Comments
 (0)