Skip to content

Commit 4569707

Browse files
authored
Permit URLs in @see tags and make links for name references (#2359)
1 parent 032909d commit 4569707

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

internal/ls/hover.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ func (l *LanguageService) getDocumentationFromDeclaration(c *checker.Checker, de
9999
writeOptionalEntityName(&b, tag.Name())
100100
case ast.KindJSDocAugmentsTag:
101101
writeOptionalEntityName(&b, tag.ClassName())
102-
case ast.KindJSDocSeeTag:
103-
writeOptionalEntityName(&b, tag.AsJSDocSeeTag().NameExpression)
104102
case ast.KindJSDocTemplateTag:
105103
for i, tp := range tag.TypeParameters() {
106104
if i != 0 {
@@ -119,6 +117,13 @@ func (l *LanguageService) getDocumentationFromDeclaration(c *checker.Checker, de
119117
} else {
120118
writeCode(&b, "tsx", commentText)
121119
}
120+
} else if tag.Kind == ast.KindJSDocSeeTag && tag.AsJSDocSeeTag().NameExpression != nil {
121+
b.WriteString(" — ")
122+
l.writeNameLink(&b, c, tag.AsJSDocSeeTag().NameExpression.Name(), "", false /*quote*/, isMarkdown)
123+
if len(comments) != 0 {
124+
b.WriteString(" ")
125+
l.writeComments(&b, c, comments, isMarkdown)
126+
}
122127
} else if len(comments) != 0 {
123128
b.WriteString(" ")
124129
if !commentHasPrefix(comments, "-") {
@@ -550,6 +555,10 @@ func (l *LanguageService) writeJSDocLink(b *strings.Builder, c *checker.Checker,
550555
}
551556
return
552557
}
558+
l.writeNameLink(b, c, name, text, quote, isMarkdown)
559+
}
560+
561+
func (l *LanguageService) writeNameLink(b *strings.Builder, c *checker.Checker, name *ast.Node, text string, quote bool, isMarkdown bool) {
553562
declarations := getDeclarationsFromLocation(c, name)
554563
if len(declarations) != 0 {
555564
declaration := declarations[0]
@@ -569,7 +578,7 @@ func (l *LanguageService) writeJSDocLink(b *strings.Builder, c *checker.Checker,
569578
}
570579
return
571580
}
572-
writeQuotedString(b, getEntityNameString(name)+" "+text, quote && isMarkdown)
581+
writeQuotedString(b, getEntityNameString(name)+core.IfElse(len(text) != 0, " ", "")+text, quote && isMarkdown)
573582
}
574583

575584
func trimCommentPrefix(text string) string {

internal/parser/jsdoc.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,12 @@ func (p *Parser) parseJSDocTypeExpression(mayOmitBraces bool) *ast.Node {
8989
func (p *Parser) parseJSDocNameReference() *ast.Node {
9090
pos := p.nodePos()
9191
hasBrace := p.parseOptional(ast.KindOpenBraceToken)
92-
p2 := p.nodePos()
93-
entityName := p.parseEntityName(false, nil)
94-
for p.token == ast.KindPrivateIdentifier {
95-
p.scanner.ReScanHashToken() // rescan #id as # id
96-
p.nextTokenJSDoc() // then skip the #
97-
entityName = p.finishNode(p.factory.NewQualifiedName(entityName, p.parseIdentifier()), p2)
98-
}
92+
entityName := p.parseJSDocLinkName()
9993
if hasBrace {
10094
p.parseExpectedJSDoc(ast.KindCloseBraceToken)
10195
}
102-
96+
p.scanner.ResetPos(p.scanner.TokenFullStart())
97+
p.nextTokenJSDoc()
10398
return p.finishNode(p.factory.NewJSDocNameReference(entityName), pos)
10499
}
105100

@@ -627,7 +622,6 @@ func (p *Parser) parseJSDocLink(start int) *ast.Node {
627622
func (p *Parser) parseJSDocLinkName() *ast.Node {
628623
if tokenIsIdentifierOrKeyword(p.token) {
629624
pos := p.nodePos()
630-
631625
name := p.parseIdentifierName()
632626
for p.parseOptional(ast.KindDotToken) {
633627
var right *ast.IdentifierNode
@@ -638,7 +632,6 @@ func (p *Parser) parseJSDocLinkName() *ast.Node {
638632
}
639633
name = p.finishNode(p.factory.NewQualifiedName(name, right), pos)
640634
}
641-
642635
for p.token == ast.KindPrivateIdentifier {
643636
p.scanner.ReScanHashToken()
644637
p.nextTokenJSDoc()
@@ -794,11 +787,10 @@ func (p *Parser) parseTypeTag(previousTags []*ast.Node, start int, tagName *ast.
794787
}
795788

796789
func (p *Parser) parseSeeTag(start int, tagName *ast.IdentifierNode, indent int, indentText string) *ast.Node {
797-
isMarkdownOrJSDocLink := p.token == ast.KindOpenBracketToken || p.lookAhead(func(p *Parser) bool {
798-
return p.nextTokenJSDoc() == ast.KindAtToken && tokenIsIdentifierOrKeyword(p.nextTokenJSDoc()) && isJSDocLinkTag(p.scanner.TokenValue())
799-
})
790+
hasNameReference := p.isIdentifier() && !strings.HasPrefix(p.sourceText[p.scanner.TokenEnd():], "://") ||
791+
p.token == ast.KindOpenBraceToken && p.lookAhead((*Parser).nextTokenIsIdentifierOrKeyword)
800792
var nameExpression *ast.Node
801-
if !isMarkdownOrJSDocLink {
793+
if hasNameReference {
802794
nameExpression = p.parseJSDocNameReference()
803795
}
804796
comments := p.parseTrailingTagComments(start, p.nodePos(), indent, indentText)

testdata/baselines/reference/fourslash/quickInfo/quickInfoForJSDocWithHttpLinks.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
// | ```
4141
// |
4242
// |
43-
// | *@see* `https` — ://hvad
43+
// | *@see* — https://hvad
4444
// | ----------------------------------------------------------------------
4545
//
4646
// /** @see {@link https://hva} */
@@ -159,7 +159,7 @@
159159
"item": {
160160
"contents": {
161161
"kind": "markdown",
162-
"value": "```tsx\nvar see1: boolean\n```\n\n\n*@see* `https` — ://hvad "
162+
"value": "```tsx\nvar see1: boolean\n```\n\n\n*@see* — https://hvad "
163163
},
164164
"range": {
165165
"start": {

testdata/baselines/reference/fourslash/quickInfo/quickInfoJsDocTags4.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
// | *@author* — Me <me@domain.tld>
2727
// |
2828
// |
29-
// | *@see* `x` — (the parameter)
29+
// | *@see* — x (the parameter)
3030
// |
3131
// |
3232
// | *@param* `x` - x comment
@@ -56,7 +56,7 @@
5656
"item": {
5757
"contents": {
5858
"kind": "markdown",
59-
"value": "```tsx\n(method) Bar.method(x: number, y: number): number\n```\ncomment\n\n*@author* — Me <me@domain.tld>\n\n\n*@see* `x` — (the parameter)\n\n\n*@param* `x` - x comment\n\n\n*@param* `y` - y comment\n\n\n*@returns* — The result\n"
59+
"value": "```tsx\n(method) Bar.method(x: number, y: number): number\n```\ncomment\n\n*@author* — Me <me@domain.tld>\n\n\n*@see* — x (the parameter)\n\n\n*@param* `x` - x comment\n\n\n*@param* `y` - y comment\n\n\n*@returns* — The result\n"
6060
},
6161
"range": {
6262
"start": {

testdata/baselines/reference/fourslash/quickInfo/quickInfoJsDocTags5.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
// | *@author* — Me <me@domain.tld>
2727
// |
2828
// |
29-
// | *@see* `x` — (the parameter)
29+
// | *@see* — x (the parameter)
3030
// |
3131
// |
3232
// | *@param* `x` - x comment
@@ -56,7 +56,7 @@
5656
"item": {
5757
"contents": {
5858
"kind": "markdown",
59-
"value": "```tsx\n(method) Bar.method(x: any, y: any): number\n```\ncomment\n\n*@author* — Me <me@domain.tld>\n\n\n*@see* `x` — (the parameter)\n\n\n*@param* `x` - x comment\n\n\n*@param* `y` - y comment\n\n\n*@returns* — The result\n"
59+
"value": "```tsx\n(method) Bar.method(x: any, y: any): number\n```\ncomment\n\n*@author* — Me <me@domain.tld>\n\n\n*@see* — x (the parameter)\n\n\n*@param* `x` - x comment\n\n\n*@param* `y` - y comment\n\n\n*@returns* — The result\n"
6060
},
6161
"range": {
6262
"start": {

0 commit comments

Comments
 (0)