From bf27c77b2446e064c8be2660fbe1c0fb478a9326 Mon Sep 17 00:00:00 2001 From: Matthieu Riegler Date: Mon, 20 May 2024 16:02:22 +0200 Subject: [PATCH] fix(58584): formatJSDocLink shouldn't introduce a trailing space when non link text. fixes #58584 --- src/compiler/utilitiesPublic.ts | 2 +- src/testRunner/unittests/jsDocParsing.ts | 18 +++++++++ tests/baselines/reference/linkTagEmit1.js | 46 +++++++++++------------ 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/compiler/utilitiesPublic.ts b/src/compiler/utilitiesPublic.ts index 45ec4057f6b79..2ff6e7adaa867 100644 --- a/src/compiler/utilitiesPublic.ts +++ b/src/compiler/utilitiesPublic.ts @@ -1253,7 +1253,7 @@ function formatJSDocLink(link: JSDocLink | JSDocLinkCode | JSDocLinkPlain) { : link.kind === SyntaxKind.JSDocLinkCode ? "linkcode" : "linkplain"; const name = link.name ? entityNameToString(link.name) : ""; - const space = link.name && link.text.startsWith("://") ? "" : " "; + const space = link.name && (link.text === "" || link.text.startsWith("://")) ? "" : " "; return `{@${kind} ${name}${space}${link.text}}`; } diff --git a/src/testRunner/unittests/jsDocParsing.ts b/src/testRunner/unittests/jsDocParsing.ts index bc0781229783e..546bffaa777e1 100644 --- a/src/testRunner/unittests/jsDocParsing.ts +++ b/src/testRunner/unittests/jsDocParsing.ts @@ -560,4 +560,22 @@ class Foo {}; assert.equal(ts.getTextOfJSDocComment(seeTag.comment), "{@link foo#bar label}"); }); }); + + describe("getTextOfJSDocComment", () => { + it("should preserve link without introducing space", () => { + const sourceText = ` +/** + * + * @see {@link foo} + */ +class Foo {}; +`; + + const root = ts.createSourceFile("foo.ts", sourceText, ts.ScriptTarget.ES5, /*setParentNodes*/ true); + const [classDecl] = root.statements; + const [seeTag] = ts.getJSDocTags(classDecl); + + assert.equal(ts.getTextOfJSDocComment(seeTag.comment), "{@link foo}"); + }); + }); }); diff --git a/tests/baselines/reference/linkTagEmit1.js b/tests/baselines/reference/linkTagEmit1.js index 0fe024f665afa..e244bbedc1f67 100644 --- a/tests/baselines/reference/linkTagEmit1.js +++ b/tests/baselines/reference/linkTagEmit1.js @@ -1,30 +1,30 @@ //// [tests/cases/conformance/jsdoc/linkTagEmit1.ts] //// //// [declarations.d.ts] -declare namespace NS { - type R = number +declare namespace NS { + type R = number } //// [linkTagEmit1.js] -/** @typedef {number} N */ -/** - * @typedef {Object} D1 - * @property {1} e Just link to {@link NS.R} this time - * @property {1} m Wyatt Earp loved {@link N integers} I bet. - */ - -/** @typedef {number} Z @see N {@link N} */ - -/** - * @param {number} integer {@link Z} - */ -function computeCommonSourceDirectoryOfFilenames(integer) { - return integer + 1 // pls pls pls -} - -/** {@link https://hvad} */ -var see3 = true - -/** @typedef {number} Attempt {@link https://wat} {@linkcode I think lingcod is better} {@linkplain or lutefisk}*/ +/** @typedef {number} N */ +/** + * @typedef {Object} D1 + * @property {1} e Just link to {@link NS.R} this time + * @property {1} m Wyatt Earp loved {@link N integers} I bet. + */ + +/** @typedef {number} Z @see N {@link N} */ + +/** + * @param {number} integer {@link Z} + */ +function computeCommonSourceDirectoryOfFilenames(integer) { + return integer + 1 // pls pls pls +} + +/** {@link https://hvad} */ +var see3 = true + +/** @typedef {number} Attempt {@link https://wat} {@linkcode I think lingcod is better} {@linkplain or lutefisk}*/ //// [linkTagEmit1.js] @@ -63,7 +63,7 @@ declare var see3: boolean; type N = number; type D1 = { /** - * Just link to {@link NS.R } this time + * Just link to {@link NS.R} this time */ e: 1; /**