diff --git a/src/services/inlayHints.ts b/src/services/inlayHints.ts index e00ca3308d58b..7462a2c6f2c7c 100644 --- a/src/services/inlayHints.ts +++ b/src/services/inlayHints.ts @@ -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; + } } } diff --git a/tests/cases/fourslash/inlayHintsShouldWork15.ts b/tests/cases/fourslash/inlayHintsShouldWork15.ts index 544838b43c73b..183d100491898 100644 --- a/tests/cases/fourslash/inlayHintsShouldWork15.ts +++ b/tests/cases/fourslash/inlayHintsShouldWork15.ts @@ -1,15 +1,25 @@ /// -//// 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 = {}; +////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 }); diff --git a/tests/cases/fourslash/inlayHintsShouldWork16.ts b/tests/cases/fourslash/inlayHintsShouldWork16.ts deleted file mode 100644 index 544838b43c73b..0000000000000 --- a/tests/cases/fourslash/inlayHintsShouldWork16.ts +++ /dev/null @@ -1,15 +0,0 @@ -/// - -//// 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 -}); diff --git a/tests/cases/fourslash/inlayHintsShouldWork17.ts b/tests/cases/fourslash/inlayHintsShouldWork17.ts index 70ea5e0d5eb5d..63bfb682b36ae 100644 --- a/tests/cases/fourslash/inlayHintsShouldWork17.ts +++ b/tests/cases/fourslash/inlayHintsShouldWork17.ts @@ -1,15 +1,7 @@ /// -//// 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 }); diff --git a/tests/cases/fourslash/inlayHintsShouldWork18.ts b/tests/cases/fourslash/inlayHintsShouldWork18.ts index e33033549f0dc..b97cd22d00e1d 100644 --- a/tests/cases/fourslash/inlayHintsShouldWork18.ts +++ b/tests/cases/fourslash/inlayHintsShouldWork18.ts @@ -1,16 +1,8 @@ /// //// 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 }); diff --git a/tests/cases/fourslash/inlayHintsShouldWork22.ts b/tests/cases/fourslash/inlayHintsShouldWork22.ts index 91fa9eb02c3a5..65896860ee889 100644 --- a/tests/cases/fourslash/inlayHintsShouldWork22.ts +++ b/tests/cases/fourslash/inlayHintsShouldWork22.ts @@ -1,15 +1,7 @@ /// -//// 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 }); diff --git a/tests/cases/fourslash/inlayHintsShouldWork56.ts b/tests/cases/fourslash/inlayHintsShouldWork56.ts index 98c483625539f..beeaba23a15fa 100644 --- a/tests/cases/fourslash/inlayHintsShouldWork56.ts +++ b/tests/cases/fourslash/inlayHintsShouldWork56.ts @@ -1,37 +1,31 @@ /// -//// 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 } diff --git a/tests/cases/fourslash/inlayHintsShouldWork65.ts b/tests/cases/fourslash/inlayHintsShouldWork65.ts index a80c102149db6..b66723322be61 100644 --- a/tests/cases/fourslash/inlayHintsShouldWork65.ts +++ b/tests/cases/fourslash/inlayHintsShouldWork65.ts @@ -1,21 +1,15 @@ /// //// 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 diff --git a/tests/cases/fourslash/inlayHintsShouldWork69.ts b/tests/cases/fourslash/inlayHintsShouldWork69.ts new file mode 100644 index 0000000000000..a056402a24c3d --- /dev/null +++ b/tests/cases/fourslash/inlayHintsShouldWork69.ts @@ -0,0 +1,25 @@ +/// + +////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 = {}) {} +////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 +});