From 7e8576c212f82beee675acd112c62da492ab707d Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Tue, 12 Jul 2022 22:10:13 -0700 Subject: [PATCH 1/4] Fixed closing JSDoc when adding multiple blocks --- src/services/jsDoc.ts | 21 ++++++++++++++++--- .../docCommentTemplateWithMultipleJSDoc.ts | 7 +++++++ ...tTemplateWithMultipleJSDocAndParameters.ts | 12 +++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 tests/cases/fourslash/docCommentTemplateWithMultipleJSDoc.ts create mode 100644 tests/cases/fourslash/docCommentTemplateWithMultipleJSDocAndParameters.ts diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index e85246e9eb5b9..93ec7a0dda24e 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -357,8 +357,12 @@ namespace ts.JsDoc { } const { commentOwner, parameters, hasReturn } = commentOwnerInfo; - const commentOwnerJSDoc = hasJSDocNodes(commentOwner) && commentOwner.jsDoc ? lastOrUndefined(commentOwner.jsDoc) : undefined; - if (commentOwner.getStart(sourceFile) < position || commentOwnerJSDoc && commentOwnerJSDoc !== existingDocComment) { + const commentOwnerJsDoc = hasJSDocNodes(commentOwner) && commentOwner.jsDoc ? commentOwner.jsDoc : undefined; + const lastJsDoc = lastOrUndefined(commentOwnerJsDoc); + if (commentOwner.getStart(sourceFile) < position + || lastJsDoc + && existingDocComment + && lastJsDoc !== existingDocComment) { return undefined; } @@ -378,7 +382,18 @@ namespace ts.JsDoc { // * if the caret was directly in front of the object, then we add an extra line and indentation. const openComment = "/**"; const closeComment = " */"; - if (tags) { + + // If any of the existing jsDoc has tags, ignore adding new ones. + let hasTag = false; + for (let jsDoc of commentOwnerJsDoc || []) { + if (jsDoc.tags) { + hasTag = true; + break; + } + } + + + if (tags && !hasTag) { const preamble = openComment + newLine + indentationStr + " * "; const endLine = tokenStart === position ? newLine + indentationStr : ""; const result = preamble + newLine + tags + indentationStr + closeComment + endLine; diff --git a/tests/cases/fourslash/docCommentTemplateWithMultipleJSDoc.ts b/tests/cases/fourslash/docCommentTemplateWithMultipleJSDoc.ts new file mode 100644 index 0000000000000..f1b2c3fad3cab --- /dev/null +++ b/tests/cases/fourslash/docCommentTemplateWithMultipleJSDoc.ts @@ -0,0 +1,7 @@ +/// + +/////** */ +/////*/**/ +////function foo() {} + +verify.docCommentTemplateAt("", 3, "/** */"); diff --git a/tests/cases/fourslash/docCommentTemplateWithMultipleJSDocAndParameters.ts b/tests/cases/fourslash/docCommentTemplateWithMultipleJSDocAndParameters.ts new file mode 100644 index 0000000000000..add01b90006cf --- /dev/null +++ b/tests/cases/fourslash/docCommentTemplateWithMultipleJSDocAndParameters.ts @@ -0,0 +1,12 @@ +/// + +/////** */ +/////** +//// * +//// * @param p +//// */ +/////** */ +/////*/**/ +////function foo(p) {} + +verify.docCommentTemplateAt("", 3, "/** */"); From d29f0ac720d1534f65c42f375784a350214a32c7 Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Wed, 20 Jul 2022 13:56:39 -0700 Subject: [PATCH 2/4] Fixed linting errors --- src/services/jsDoc.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 93ec7a0dda24e..d40ea66fb975d 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -382,10 +382,10 @@ namespace ts.JsDoc { // * if the caret was directly in front of the object, then we add an extra line and indentation. const openComment = "/**"; const closeComment = " */"; - + // If any of the existing jsDoc has tags, ignore adding new ones. let hasTag = false; - for (let jsDoc of commentOwnerJsDoc || []) { + for (const jsDoc of commentOwnerJsDoc || []) { if (jsDoc.tags) { hasTag = true; break; From 188953f6630ec80bea126f56b4bd0f2da97b0ef6 Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Thu, 21 Jul 2022 11:03:45 -0700 Subject: [PATCH 3/4] Refactored to use `some` Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> --- src/services/jsDoc.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index d40ea66fb975d..1026fa47a5e2e 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -384,13 +384,7 @@ namespace ts.JsDoc { const closeComment = " */"; // If any of the existing jsDoc has tags, ignore adding new ones. - let hasTag = false; - for (const jsDoc of commentOwnerJsDoc || []) { - if (jsDoc.tags) { - hasTag = true; - break; - } - } + const hasTag = (commentOwnerJsDoc || []).some(jsDoc => !!jsDoc.tags); if (tags && !hasTag) { From 8b75c7d801ddc4329321cdddc6e0d40d60afdbc3 Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Thu, 21 Jul 2022 11:05:25 -0700 Subject: [PATCH 4/4] Removed empty lines --- src/services/jsDoc.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 1026fa47a5e2e..257531c14e8d9 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -386,7 +386,6 @@ namespace ts.JsDoc { // If any of the existing jsDoc has tags, ignore adding new ones. const hasTag = (commentOwnerJsDoc || []).some(jsDoc => !!jsDoc.tags); - if (tags && !hasTag) { const preamble = openComment + newLine + indentationStr + " * "; const endLine = tokenStart === position ? newLine + indentationStr : "";