From e9867a73532d7235cfc57210f0f59bdcd4967f5e Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 11 May 2020 22:14:45 +0000 Subject: [PATCH] Add and use the 'intersperse' helper function. --- src/compiler/core.ts | 16 ++++++++++++++++ src/services/jsDoc.ts | 12 +++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 2841b80aedf..63e293f2e8f 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -129,6 +129,22 @@ namespace ts { return map; } + /** + * Creates a new array with `element` interspersed in between each element of `input` + * if there is more than 1 value in `input`. Otherwise, returns the existing array. + */ + export function intersperse(input: T[], element: T): T[] { + if (input.length <= 1) { + return input; + } + const result: T[] = []; + for (let i = 0, n = input.length; i < n; i++) { + if (i) result.push(element); + result.push(input[i]); + } + return result; + } + /** * Iterates through `array` by index and performs the callback on each element of array until the callback * returns a falsey value, then returns false. diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 2616aea3144..3115109572a 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -89,20 +89,14 @@ namespace ts.JsDoc { // Eg. const a: Array | Array; a.length // The property length will have two declarations of property length coming // from Array - Array and Array - const documentationComment: SymbolDisplayPart[] = []; + const documentationComment: string[] = []; forEachUnique(declarations, declaration => { for (const { comment } of getCommentHavingNodes(declaration)) { if (comment === undefined) continue; - const commentTextPart = textPart(comment); - if (!contains(documentationComment, commentTextPart)) { - if (documentationComment.length) { - documentationComment.push(lineBreakPart()); - } - documentationComment.push(commentTextPart); - } + pushIfUnique(documentationComment, comment); } }); - return documentationComment; + return intersperse(map(documentationComment, textPart), lineBreakPart()); } function getCommentHavingNodes(declaration: Declaration): readonly (JSDoc | JSDocTag)[] {