Skip to content

Commit c4fd002

Browse files
authored
Fix @link https:// formatting (microsoft#47705)
* Fix @link https:// formatting Also improve .d.ts formatting of `@link`,`@linkcode`,`@linkplain`. Fixes microsoft#46734 1. Previously, `@link` incorrectly put a space between "https" and "://" when formatting jsdoc for editors. Now it does not. 2. When fixing the same output for .d.ts, I discovered that all `@link` tags were formatted as `@link`, even if they were `@linkcode` or `@linkplain`. I fixed that too. * semicolon lint
1 parent 854cec7 commit c4fd002

File tree

8 files changed

+181
-4
lines changed

8 files changed

+181
-4
lines changed

src/compiler/utilitiesPublic.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -905,9 +905,16 @@ namespace ts {
905905
/** Gets the text of a jsdoc comment, flattening links to their text. */
906906
export function getTextOfJSDocComment(comment?: string | NodeArray<JSDocComment>) {
907907
return typeof comment === "string" ? comment
908-
: comment?.map(c =>
909-
// TODO: Other kinds here
910-
c.kind === SyntaxKind.JSDocText ? c.text : `{@link ${c.name ? entityNameToString(c.name) + " " : ""}${c.text}}`).join("");
908+
: comment?.map(c => c.kind === SyntaxKind.JSDocText ? c.text : formatJSDocLink(c)).join("");
909+
}
910+
911+
function formatJSDocLink(link: JSDocLink | JSDocLinkCode | JSDocLinkPlain) {
912+
const kind = link.kind === SyntaxKind.JSDocLink ? "link"
913+
: link.kind === SyntaxKind.JSDocLinkCode ? "linkcode"
914+
: "linkplain";
915+
const name = link.name ? entityNameToString(link.name) : "";
916+
const space = link.name && link.text.startsWith("://") ? "" : " ";
917+
return `{@${kind} ${name}${space}${link.text}}`;
911918
}
912919

913920
/**

src/services/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,7 @@ namespace ts {
23202320
if (text) parts.push(linkTextPart(text));
23212321
}
23222322
else {
2323-
parts.push(linkTextPart(name + (suffix ? "" : " ") + text));
2323+
parts.push(linkTextPart(name + (suffix || text.indexOf("://") === 0 ? "" : " ") + text));
23242324
}
23252325
}
23262326
parts.push(linkPart("}"));

tests/baselines/reference/linkTagEmit1.js

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ declare namespace NS {
2020
function computeCommonSourceDirectoryOfFilenames(integer) {
2121
return integer + 1 // pls pls pls
2222
}
23+
24+
/** {@link https://hvad} */
25+
var see3 = true
26+
27+
/** @typedef {number} Attempt {@link https://wat} {@linkcode I think lingcod is better} {@linkplain or lutefisk}*/
2328

2429

2530
//// [linkTagEmit1.js]
@@ -36,6 +41,9 @@ function computeCommonSourceDirectoryOfFilenames(integer) {
3641
function computeCommonSourceDirectoryOfFilenames(integer) {
3742
return integer + 1; // pls pls pls
3843
}
44+
/** {@link https://hvad} */
45+
var see3 = true;
46+
/** @typedef {number} Attempt {@link https://wat} {@linkcode I think lingcod is better} {@linkplain or lutefisk}*/
3947

4048

4149
//// [linkTagEmit1.d.ts]
@@ -50,6 +58,8 @@ function computeCommonSourceDirectoryOfFilenames(integer) {
5058
* @param {number} integer {@link Z}
5159
*/
5260
declare function computeCommonSourceDirectoryOfFilenames(integer: number): number;
61+
/** {@link https://hvad} */
62+
declare var see3: boolean;
5363
type N = number;
5464
type D1 = {
5565
/**
@@ -62,3 +72,7 @@ type D1 = {
6272
m: 1;
6373
};
6474
type Z = number;
75+
/**
76+
* {@link https://wat} {@linkcode I think lingcod is better} {@linkplain or lutefisk}
77+
*/
78+
type Attempt = number;

tests/baselines/reference/linkTagEmit1.symbols

+6
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ function computeCommonSourceDirectoryOfFilenames(integer) {
2626
>integer : Symbol(integer, Decl(linkTagEmit1.js, 12, 49))
2727
}
2828

29+
/** {@link https://hvad} */
30+
var see3 = true
31+
>see3 : Symbol(see3, Decl(linkTagEmit1.js, 17, 3))
32+
33+
/** @typedef {number} Attempt {@link https://wat} {@linkcode I think lingcod is better} {@linkplain or lutefisk}*/
34+

tests/baselines/reference/linkTagEmit1.types

+7
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ function computeCommonSourceDirectoryOfFilenames(integer) {
2626
>1 : 1
2727
}
2828

29+
/** {@link https://hvad} */
30+
var see3 = true
31+
>see3 : boolean
32+
>true : true
33+
34+
/** @typedef {number} Attempt {@link https://wat} {@linkcode I think lingcod is better} {@linkplain or lutefisk}*/
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
[
2+
{
3+
"marker": {
4+
"fileName": "/tests/cases/fourslash/quickInfoForJSDocWithHttpLinks.js",
5+
"position": 36,
6+
"name": "5"
7+
},
8+
"quickInfo": {
9+
"kind": "var",
10+
"kindModifiers": "",
11+
"textSpan": {
12+
"start": 36,
13+
"length": 4
14+
},
15+
"displayParts": [
16+
{
17+
"text": "var",
18+
"kind": "keyword"
19+
},
20+
{
21+
"text": " ",
22+
"kind": "space"
23+
},
24+
{
25+
"text": "see2",
26+
"kind": "localName"
27+
},
28+
{
29+
"text": ":",
30+
"kind": "punctuation"
31+
},
32+
{
33+
"text": " ",
34+
"kind": "space"
35+
},
36+
{
37+
"text": "boolean",
38+
"kind": "keyword"
39+
}
40+
],
41+
"documentation": [],
42+
"tags": [
43+
{
44+
"name": "see",
45+
"text": [
46+
{
47+
"text": "",
48+
"kind": "text"
49+
},
50+
{
51+
"text": "{@link ",
52+
"kind": "link"
53+
},
54+
{
55+
"text": "https://hva",
56+
"kind": "linkText"
57+
},
58+
{
59+
"text": "}",
60+
"kind": "link"
61+
}
62+
]
63+
}
64+
]
65+
}
66+
},
67+
{
68+
"marker": {
69+
"fileName": "/tests/cases/fourslash/quickInfoForJSDocWithHttpLinks.js",
70+
"position": 81,
71+
"name": "6"
72+
},
73+
"quickInfo": {
74+
"kind": "var",
75+
"kindModifiers": "",
76+
"textSpan": {
77+
"start": 81,
78+
"length": 4
79+
},
80+
"displayParts": [
81+
{
82+
"text": "var",
83+
"kind": "keyword"
84+
},
85+
{
86+
"text": " ",
87+
"kind": "space"
88+
},
89+
{
90+
"text": "see3",
91+
"kind": "localName"
92+
},
93+
{
94+
"text": ":",
95+
"kind": "punctuation"
96+
},
97+
{
98+
"text": " ",
99+
"kind": "space"
100+
},
101+
{
102+
"text": "boolean",
103+
"kind": "keyword"
104+
}
105+
],
106+
"documentation": [
107+
{
108+
"text": "",
109+
"kind": "text"
110+
},
111+
{
112+
"text": "{@link ",
113+
"kind": "link"
114+
},
115+
{
116+
"text": "https://hvaD",
117+
"kind": "linkText"
118+
},
119+
{
120+
"text": "}",
121+
"kind": "link"
122+
}
123+
]
124+
}
125+
}
126+
]

tests/cases/conformance/jsdoc/linkTagEmit1.ts

+5
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ declare namespace NS {
2121
function computeCommonSourceDirectoryOfFilenames(integer) {
2222
return integer + 1 // pls pls pls
2323
}
24+
25+
/** {@link https://hvad} */
26+
var see3 = true
27+
28+
/** @typedef {number} Attempt {@link https://wat} {@linkcode I think lingcod is better} {@linkplain or lutefisk}*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
// @checkJs: true
3+
// @filename: quickInfoForJSDocWithHttpLinks.js
4+
// #46734
5+
6+
//// /** @see {@link https://hva} */
7+
//// var /*5*/see2 = true
8+
////
9+
//// /** {@link https://hvaD} */
10+
//// var /*6*/see3 = true
11+
12+
verify.baselineQuickInfo();

0 commit comments

Comments
 (0)