feat(49385): forbid inlay hints for some kind of initialized declarations (#49412)

.
This commit is contained in:
Oleksandr T 2022-06-16 01:34:57 +03:00 committed by GitHub
parent 18ac37221b
commit 180bc4cbea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 73 additions and 78 deletions

View File

@ -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;
}
@ -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;
}
@ -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;
}
}
}

View File

@ -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
});

View File

@ -1,15 +0,0 @@
/// <reference path="fourslash.ts" />
//// const a/*a*/ = 123;
const markers = test.markers();
verify.getInlayHints([
{
text: ': 123',
position: markers[0].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
], undefined, {
includeInlayVariableTypeHints: true
});

View File

@ -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
});

View File

@ -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
});

View File

@ -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
});

View File

@ -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; }',
text: ': number[]',
position: markers[0].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
{
text: ': number[]',
text: ': { foo: number; bar: number; }',
position: markers[1].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
{
text: ': { foo: number; bar: number; }',
position: markers[2].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
},
{
text: ': number[]',
position: markers[3].position,
position: markers[2].position,
kind: ts.InlayHintKind.Type,
whitespaceBefore: true
}

View File

@ -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

View File

@ -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
});