diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 2841b80aedf95..63e293f2e8f1a 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 63297c7868d21..3115109572a0d 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -89,17 +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; - if (documentationComment.length) { - documentationComment.push(lineBreakPart()); - } - documentationComment.push(textPart(comment)); + pushIfUnique(documentationComment, comment); } }); - return documentationComment; + return intersperse(map(documentationComment, textPart), lineBreakPart()); } function getCommentHavingNodes(declaration: Declaration): readonly (JSDoc | JSDocTag)[] { diff --git a/tests/baselines/reference/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.baseline b/tests/baselines/reference/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.baseline new file mode 100644 index 0000000000000..b03ca277801b2 --- /dev/null +++ b/tests/baselines/reference/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.baseline @@ -0,0 +1,60 @@ +[ + { + "marker": { + "fileName": "/tests/cases/fourslash/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.ts", + "position": 746 + }, + "quickInfo": { + "kind": "property", + "kindModifiers": "optional", + "textSpan": { + "start": 746, + "length": 8 + }, + "displayParts": [ + { + "text": "(", + "kind": "punctuation" + }, + { + "text": "property", + "kind": "text" + }, + { + "text": ")", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "language", + "kind": "propertyName" + }, + { + "text": "?", + "kind": "punctuation" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "string", + "kind": "keyword" + } + ], + "documentation": [ + { + "text": "A language id, like `typescript`.", + "kind": "text" + } + ] + } + } +] \ No newline at end of file diff --git a/tests/cases/fourslash/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.ts b/tests/cases/fourslash/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.ts new file mode 100644 index 0000000000000..132130da22f78 --- /dev/null +++ b/tests/cases/fourslash/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.ts @@ -0,0 +1,29 @@ +/// + +////export type DocumentFilter = { +//// /** A language id, like `typescript`. */ +//// language: string; +//// /** A Uri [scheme](#Uri.scheme), like `file` or `untitled`. */ +//// scheme?: string; +//// /** A glob pattern, like `*.{ts,js}`. */ +//// pattern?: string; +////} | { +//// /** A language id, like `typescript`. */ +//// language?: string; +//// /** A Uri [scheme](#Uri.scheme), like `file` or `untitled`. */ +//// scheme: string; +//// /** A glob pattern, like `*.{ts,js}`. */ +//// pattern?: string; +////} | { +//// /** A language id, like `typescript`. */ +//// language?: string; +//// /** A Uri [scheme](#Uri.scheme), like `file` or `untitled`. */ +//// scheme?: string; +//// /** A glob pattern, like `*.{ts,js}`. */ +//// pattern: string; +////}; +//// +////declare let x: DocumentFilter; +////x./**/language + +verify.baselineQuickInfo(); \ No newline at end of file