Skip to content

Commit 827b307

Browse files
Use the checker's alias declaration, skip tag-only alias comments
On a merged symbol Declarations[0] is the namespace, so the alias comment was never read. An alias comment with no description was hiding the target's.
1 parent 997a83d commit 827b307

4 files changed

Lines changed: 134 additions & 33 deletions

File tree

‎tsc/internal/checker/services.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ func (c *Checker) SkipAlias(symbol *ast.Symbol) *ast.Symbol {
400400
return symbol
401401
}
402402

403+
func (c *Checker) GetDeclarationOfAliasSymbol(symbol *ast.Symbol) *ast.Node {
404+
return c.getDeclarationOfAliasSymbol(symbol)
405+
}
406+
403407
func (c *Checker) GetRootSymbols(symbol *ast.Symbol) []*ast.Symbol {
404408
roots := c.getImmediateRootSymbols(symbol)
405409
if len(roots) == 0 {

‎tsc/internal/fourslash/tests/hoverImportEqualsAliasJSDoc_test.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func TestHoverImportEqualsAliasJSDoc(t *testing.T) {
2020
/** on the declaration */
2121
export interface Shape {}
2222
export function bare(): void;
23+
/** on the declaration */
24+
export function tagged(): void;
25+
/** on the declaration */
26+
export function plain(): void;
2327
}
2428
declare namespace b {
2529
/** on the alias */
@@ -30,13 +34,18 @@ declare namespace b {
3034
export import Shape = a.Shape;
3135
/** on the alias */
3236
export import bare = a.bare;
37+
/** @deprecated on the alias */
38+
export import tagged = a.tagged;
39+
export import plain = a.plain;
3340
}
3441
b./*fn*/fn;
3542
b./*fnCall*/fn();
3643
b./*value*/value;
3744
let shape: b./*Shape*/Shape;
3845
b./*bare*/bare;
39-
b./*bareCall*/bare();`
46+
b./*bareCall*/bare();
47+
b./*tagged*/tagged;
48+
b./*plain*/plain;`
4049
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
4150
defer done()
4251
f.VerifyBaselineHover(t)

‎tsc/internal/ls/hover.go‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,19 @@ func (l *LanguageService) documentationLocationMapper(feature spanmap.Feature) d
211211
}
212212

213213
func getDocumentationForSymbol(getMappedLocation documentationLocationMapper, c *checker.Checker, symbol *ast.Symbol, node *ast.Node, declaration *ast.Node, contentFormat lsproto.MarkupKind, commentOnly bool) string {
214-
documentation := documentationFromSignature(getMappedLocation, c, symbol, getCallOrNewExpression(node), node, contentFormat, commentOnly)
214+
callNode := getCallOrNewExpression(node)
215+
documentation := documentationFromSignature(getMappedLocation, c, symbol, callNode, node, contentFormat, commentOnly)
215216
if documentation != "" {
216217
return documentation
217218
}
218219

219220
// For an alias, declaration is the target's, and the alias's own JSDoc comes before it. At a
220221
// call site declaration is the resolved signature's instead, and that comes first.
221-
aliasDocumentation := documentationFromAliasDeclaration(getMappedLocation, c, symbol, node, contentFormat, commentOnly)
222-
if aliasDocumentation != "" && getCallOrNewExpression(node) == nil {
223-
return aliasDocumentation
222+
if callNode == nil {
223+
documentation = documentationFromAliasDeclaration(getMappedLocation, c, symbol, node, contentFormat, commentOnly)
224+
if documentation != "" {
225+
return documentation
226+
}
224227
}
225228

226229
documentation = documentationFromRootSymbols(getMappedLocation, c, symbol, node, contentFormat, commentOnly)
@@ -233,8 +236,11 @@ func getDocumentationForSymbol(getMappedLocation documentationLocationMapper, c
233236
return documentation
234237
}
235238

236-
if aliasDocumentation != "" {
237-
return aliasDocumentation
239+
if callNode != nil {
240+
documentation = documentationFromAliasDeclaration(getMappedLocation, c, symbol, node, contentFormat, commentOnly)
241+
if documentation != "" {
242+
return documentation
243+
}
238244
}
239245

240246
return documentationFromAlias(getMappedLocation, c, symbol, node, contentFormat, commentOnly)
@@ -259,12 +265,17 @@ func documentationFromSignature(getMappedLocation documentationLocationMapper, c
259265
}
260266

261267
// documentationFromAliasDeclaration returns the JSDoc written on the alias itself, as on
262-
// `/** ... */ import x = a.x`.
268+
// `/** ... */ import x = a.x`. A comment made only of tags leaves the target's documentation in
269+
// place, so it returns nothing for one.
263270
func documentationFromAliasDeclaration(getMappedLocation documentationLocationMapper, c *checker.Checker, symbol *ast.Symbol, node *ast.Node, contentFormat lsproto.MarkupKind, commentOnly bool) string {
264271
if symbol == nil || symbol.Flags&ast.SymbolFlagsAlias == 0 {
265272
return ""
266273
}
267-
return getDocumentationFromDeclaration(getMappedLocation, c, symbol, core.FirstOrNil(symbol.Declarations), node, contentFormat, commentOnly)
274+
declaration := c.GetDeclarationOfAliasSymbol(symbol)
275+
if getDocumentationFromDeclaration(getMappedLocation, c, symbol, declaration, node, contentFormat, true /*commentOnly*/) == "" {
276+
return ""
277+
}
278+
return getDocumentationFromDeclaration(getMappedLocation, c, symbol, declaration, node, contentFormat, commentOnly)
268279
}
269280

270281
func documentationFromAlias(getMappedLocation documentationLocationMapper, c *checker.Checker, symbol *ast.Symbol, node *ast.Node, contentFormat lsproto.MarkupKind, commentOnly bool) string {

‎tsc/testdata/baselines/reference/fourslash/quickInfo/hoverImportEqualsAliasJSDoc.baseline‎

Lines changed: 101 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
// /** on the declaration */
99
// export interface Shape {}
1010
// export function bare(): void;
11+
// /** on the declaration */
12+
// export function tagged(): void;
13+
// /** on the declaration */
14+
// export function plain(): void;
1115
// }
1216
// declare namespace b {
1317
// /** on the alias */
@@ -18,6 +22,9 @@
1822
// export import Shape = a.Shape;
1923
// /** on the alias */
2024
// export import bare = a.bare;
25+
// /** @deprecated on the alias */
26+
// export import tagged = a.tagged;
27+
// export import plain = a.plain;
2128
// }
2229
// b.fn;
2330
// ^^
@@ -67,12 +74,28 @@
6774
// | ```
6875
// | on the alias
6976
// | ----------------------------------------------------------------------
77+
// b.tagged;
78+
// ^^^^^^
79+
// | ----------------------------------------------------------------------
80+
// | ```typescript
81+
// | (alias) function a.tagged(): void
82+
// | ```
83+
// | on the declaration
84+
// | ----------------------------------------------------------------------
85+
// b.plain;
86+
// ^^^^^
87+
// | ----------------------------------------------------------------------
88+
// | ```typescript
89+
// | (alias) function a.plain(): void
90+
// | ```
91+
// | on the declaration
92+
// | ----------------------------------------------------------------------
7093
[
7194
{
7295
"marker": {
73-
"Position": 496,
96+
"Position": 735,
7497
"LSPosition": {
75-
"line": 19,
98+
"line": 26,
7699
"character": 2
77100
},
78101
"Name": "fn",
@@ -85,21 +108,21 @@
85108
},
86109
"range": {
87110
"start": {
88-
"line": 19,
111+
"line": 26,
89112
"character": 2
90113
},
91114
"end": {
92-
"line": 19,
115+
"line": 26,
93116
"character": 4
94117
}
95118
}
96119
}
97120
},
98121
{
99122
"marker": {
100-
"Position": 502,
123+
"Position": 741,
101124
"LSPosition": {
102-
"line": 20,
125+
"line": 27,
103126
"character": 2
104127
},
105128
"Name": "fnCall",
@@ -112,21 +135,21 @@
112135
},
113136
"range": {
114137
"start": {
115-
"line": 20,
138+
"line": 27,
116139
"character": 2
117140
},
118141
"end": {
119-
"line": 20,
142+
"line": 27,
120143
"character": 4
121144
}
122145
}
123146
}
124147
},
125148
{
126149
"marker": {
127-
"Position": 510,
150+
"Position": 749,
128151
"LSPosition": {
129-
"line": 21,
152+
"line": 28,
130153
"character": 2
131154
},
132155
"Name": "value",
@@ -139,21 +162,21 @@
139162
},
140163
"range": {
141164
"start": {
142-
"line": 21,
165+
"line": 28,
143166
"character": 2
144167
},
145168
"end": {
146-
"line": 21,
169+
"line": 28,
147170
"character": 7
148171
}
149172
}
150173
}
151174
},
152175
{
153176
"marker": {
154-
"Position": 530,
177+
"Position": 769,
155178
"LSPosition": {
156-
"line": 22,
179+
"line": 29,
157180
"character": 13
158181
},
159182
"Name": "Shape",
@@ -166,11 +189,11 @@
166189
},
167190
"range": {
168191
"start": {
169-
"line": 22,
192+
"line": 29,
170193
"character": 13
171194
},
172195
"end": {
173-
"line": 22,
196+
"line": 29,
174197
"character": 18
175198
}
176199
},
@@ -179,9 +202,9 @@
179202
},
180203
{
181204
"marker": {
182-
"Position": 539,
205+
"Position": 778,
183206
"LSPosition": {
184-
"line": 23,
207+
"line": 30,
185208
"character": 2
186209
},
187210
"Name": "bare",
@@ -194,21 +217,21 @@
194217
},
195218
"range": {
196219
"start": {
197-
"line": 23,
220+
"line": 30,
198221
"character": 2
199222
},
200223
"end": {
201-
"line": 23,
224+
"line": 30,
202225
"character": 6
203226
}
204227
}
205228
}
206229
},
207230
{
208231
"marker": {
209-
"Position": 547,
232+
"Position": 786,
210233
"LSPosition": {
211-
"line": 24,
234+
"line": 31,
212235
"character": 2
213236
},
214237
"Name": "bareCall",
@@ -221,14 +244,68 @@
221244
},
222245
"range": {
223246
"start": {
224-
"line": 24,
247+
"line": 31,
225248
"character": 2
226249
},
227250
"end": {
228-
"line": 24,
251+
"line": 31,
229252
"character": 6
230253
}
231254
}
232255
}
256+
},
257+
{
258+
"marker": {
259+
"Position": 796,
260+
"LSPosition": {
261+
"line": 32,
262+
"character": 2
263+
},
264+
"Name": "tagged",
265+
"Data": {}
266+
},
267+
"item": {
268+
"contents": {
269+
"kind": "markdown",
270+
"value": "```typescript\n(alias) function a.tagged(): void\n```\non the declaration"
271+
},
272+
"range": {
273+
"start": {
274+
"line": 32,
275+
"character": 2
276+
},
277+
"end": {
278+
"line": 32,
279+
"character": 8
280+
}
281+
}
282+
}
283+
},
284+
{
285+
"marker": {
286+
"Position": 806,
287+
"LSPosition": {
288+
"line": 33,
289+
"character": 2
290+
},
291+
"Name": "plain",
292+
"Data": {}
293+
},
294+
"item": {
295+
"contents": {
296+
"kind": "markdown",
297+
"value": "```typescript\n(alias) function a.plain(): void\n```\non the declaration"
298+
},
299+
"range": {
300+
"start": {
301+
"line": 33,
302+
"character": 2
303+
},
304+
"end": {
305+
"line": 33,
306+
"character": 7
307+
}
308+
}
309+
}
233310
}
234311
]

0 commit comments

Comments
 (0)