|
| 1 | +package fourslash_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/microsoft/TypeScript/tsc/internal/fourslash" |
| 7 | + "github.com/microsoft/TypeScript/tsc/internal/testutil" |
| 8 | +) |
| 9 | + |
| 10 | +func TestHoverFunctionSignatureDocumentation(t *testing.T) { |
| 11 | + t.Parallel() |
| 12 | + for _, test := range []struct { |
| 13 | + name string |
| 14 | + declaration string |
| 15 | + }{ |
| 16 | + {name: "declaration", declaration: "function func() {}"}, |
| 17 | + {name: "arrow", declaration: "const func = () => {};"}, |
| 18 | + {name: "expression", declaration: "const func = function () {};"}, |
| 19 | + } { |
| 20 | + t.Run(test.name, func(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + defer testutil.RecoverAndFail(t, "Panic on fourslash test") |
| 23 | + content := "/** doc */\n" + test.declaration + ` |
| 24 | +const obj = { func }; |
| 25 | +obj./*reference*/func; |
| 26 | +obj./*call*/func(); |
| 27 | +const alias = func; |
| 28 | +/*alias*/alias();` |
| 29 | + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) |
| 30 | + defer done() |
| 31 | + |
| 32 | + f.VerifyQuickInfoAt(t, "reference", "(property) func: () => void", "") |
| 33 | + f.VerifyQuickInfoAt(t, "call", "(property) func: () => void", "doc") |
| 34 | + f.VerifyQuickInfoAt(t, "alias", "const alias: () => void", "doc") |
| 35 | + }) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestHoverFunctionSignatureDocumentationDirectAssignment(t *testing.T) { |
| 40 | + t.Parallel() |
| 41 | + for _, test := range []struct { |
| 42 | + name string |
| 43 | + content string |
| 44 | + display string |
| 45 | + documentation string |
| 46 | + }{ |
| 47 | + { |
| 48 | + name: "variable", |
| 49 | + content: `/** declaration doc */ |
| 50 | +const func = /** initializer doc */ () => {}; |
| 51 | +/*call*/func();`, |
| 52 | + display: "const func: () => void", |
| 53 | + documentation: "declaration doc", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "property", |
| 57 | + content: `const obj = { |
| 58 | + /** declaration doc */ |
| 59 | + func: /** initializer doc */ () => {}, |
| 60 | +}; |
| 61 | +obj./*call*/func();`, |
| 62 | + display: "(property) func: () => void", |
| 63 | + documentation: "declaration doc", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "variableParenthesizedArrow", |
| 67 | + content: `/** declaration doc */ |
| 68 | +const func = (/** initializer doc */ () => {}); |
| 69 | +/*call*/func();`, |
| 70 | + display: "const func: () => void", |
| 71 | + documentation: "declaration doc", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "variableParenthesizedFunctionExpression", |
| 75 | + content: `/** declaration doc */ |
| 76 | +const func = (/** initializer doc */ function () {}); |
| 77 | +/*call*/func();`, |
| 78 | + display: "const func: () => void", |
| 79 | + documentation: "declaration doc", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "variableSatisfiesArrow", |
| 83 | + content: `/** declaration doc */ |
| 84 | +const func = (/** initializer doc */ () => {}) satisfies () => void; |
| 85 | +/*call*/func();`, |
| 86 | + display: "const func: () => void", |
| 87 | + documentation: "declaration doc", |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "propertyParenthesizedArrow", |
| 91 | + content: `const obj = { |
| 92 | + /** declaration doc */ |
| 93 | + func: (/** initializer doc */ () => {}), |
| 94 | +}; |
| 95 | +obj./*call*/func();`, |
| 96 | + display: "(property) func: () => void", |
| 97 | + documentation: "declaration doc", |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "alias", |
| 101 | + content: `/** declaration doc */ |
| 102 | +const func = /** initializer doc */ () => {}; |
| 103 | +const alias = func; |
| 104 | +/*call*/alias();`, |
| 105 | + display: "const alias: () => void", |
| 106 | + documentation: "declaration doc", |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "shorthandProperty", |
| 110 | + content: `/** declaration doc */ |
| 111 | +const func = /** initializer doc */ () => {}; |
| 112 | +const obj = { func }; |
| 113 | +obj./*call*/func();`, |
| 114 | + display: "(property) func: () => void", |
| 115 | + documentation: "declaration doc", |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "variableInitializerOnly", |
| 119 | + content: `const func = /** initializer doc */ () => {}; |
| 120 | +/*call*/func();`, |
| 121 | + display: "const func: () => void", |
| 122 | + documentation: "initializer doc", |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "propertyInitializerOnly", |
| 126 | + content: `const obj = { |
| 127 | + func: /** initializer doc */ () => {}, |
| 128 | +}; |
| 129 | +obj./*call*/func();`, |
| 130 | + display: "(property) func: () => void", |
| 131 | + documentation: "initializer doc", |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "variableFunctionExpressionInitializerOnly", |
| 135 | + content: `const func = /** initializer doc */ function () {}; |
| 136 | +/*call*/func();`, |
| 137 | + display: "const func: () => void", |
| 138 | + documentation: "initializer doc", |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "propertyFunctionExpressionInitializerOnly", |
| 142 | + content: `const obj = { |
| 143 | + func: /** initializer doc */ function () {}, |
| 144 | +}; |
| 145 | +obj./*call*/func();`, |
| 146 | + display: "(property) func: () => void", |
| 147 | + documentation: "initializer doc", |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "variableParenthesizedArrowInitializerOnly", |
| 151 | + content: `const func = (/** initializer doc */ () => {}); |
| 152 | +/*call*/func();`, |
| 153 | + display: "const func: () => void", |
| 154 | + documentation: "initializer doc", |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "emptyDeclarationDocumentation", |
| 158 | + content: `/** */ |
| 159 | +const func = /** initializer doc */ () => {}; |
| 160 | +/*call*/func();`, |
| 161 | + display: "const func: () => void", |
| 162 | + documentation: "initializer doc", |
| 163 | + }, |
| 164 | + } { |
| 165 | + t.Run(test.name, func(t *testing.T) { |
| 166 | + t.Parallel() |
| 167 | + defer testutil.RecoverAndFail(t, "Panic on fourslash test") |
| 168 | + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, test.content) |
| 169 | + defer done() |
| 170 | + |
| 171 | + f.VerifyQuickInfoAt(t, "call", test.display, test.documentation) |
| 172 | + }) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func TestHoverFunctionSignatureDocumentationPrecedence(t *testing.T) { |
| 177 | + t.Parallel() |
| 178 | + for _, test := range []struct { |
| 179 | + name string |
| 180 | + functionDoc string |
| 181 | + expectedDoc string |
| 182 | + }{ |
| 183 | + {name: "signature", functionDoc: "/** function doc */", expectedDoc: "function doc"}, |
| 184 | + {name: "propertyFallback", expectedDoc: "property doc"}, |
| 185 | + } { |
| 186 | + t.Run(test.name, func(t *testing.T) { |
| 187 | + t.Parallel() |
| 188 | + defer testutil.RecoverAndFail(t, "Panic on fourslash test") |
| 189 | + content := test.functionDoc + ` |
| 190 | +function func() {} |
| 191 | +const obj = { |
| 192 | + /** property doc */ |
| 193 | + func, |
| 194 | +}; |
| 195 | +obj./*reference*/func; |
| 196 | +obj./*call*/func();` |
| 197 | + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) |
| 198 | + defer done() |
| 199 | + |
| 200 | + f.VerifyQuickInfoAt(t, "reference", "(property) func: () => void", "property doc") |
| 201 | + f.VerifyQuickInfoAt(t, "call", "(property) func: () => void", test.expectedDoc) |
| 202 | + }) |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +func TestHoverFunctionSignatureDocumentationOverloads(t *testing.T) { |
| 207 | + t.Parallel() |
| 208 | + defer testutil.RecoverAndFail(t, "Panic on fourslash test") |
| 209 | + const content = `/** number doc */ |
| 210 | +function func(value: number): number; |
| 211 | +/** string doc */ |
| 212 | +function func(value: string): string; |
| 213 | +function func(value: number | string) { return value; } |
| 214 | +const obj = { func }; |
| 215 | +obj./*number*/func(1); |
| 216 | +obj./*string*/func("hello");` |
| 217 | + |
| 218 | + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) |
| 219 | + defer done() |
| 220 | + |
| 221 | + f.VerifyQuickInfoAt(t, "number", "(property) func: (value: number) => number", "number doc") |
| 222 | + f.VerifyQuickInfoAt(t, "string", "(property) func: (value: string) => string", "string doc") |
| 223 | +} |
0 commit comments