From 1450908e5c5406fc467f9205c13410196211a286 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 11 May 2020 21:36:43 +0000 Subject: [PATCH 1/4] Added tests for union types with identical doc comments. --- ...rtiesWithIdenticalJSDocComments01.baseline | 76 +++++++++++++++++++ ...nPropertiesWithIdenticalJSDocComments01.ts | 29 +++++++ 2 files changed, 105 insertions(+) create mode 100644 tests/baselines/reference/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.baseline create mode 100644 tests/cases/fourslash/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.ts diff --git a/tests/baselines/reference/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.baseline b/tests/baselines/reference/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.baseline new file mode 100644 index 0000000000000..1fd2e593d2d68 --- /dev/null +++ b/tests/baselines/reference/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.baseline @@ -0,0 +1,76 @@ +[ + { + "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" + }, + { + "text": "\n", + "kind": "lineBreak" + }, + { + "text": "A language id, like `typescript`.", + "kind": "text" + }, + { + "text": "\n", + "kind": "lineBreak" + }, + { + "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 From 74d6d04d703c742f505d7a72cb7c549c27e26ea4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 11 May 2020 21:39:47 +0000 Subject: [PATCH 2/4] Don't add duplicates of JSDoc comments. --- src/services/jsDoc.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 63297c7868d21..2616aea314496 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -93,10 +93,13 @@ namespace ts.JsDoc { forEachUnique(declarations, declaration => { for (const { comment } of getCommentHavingNodes(declaration)) { if (comment === undefined) continue; - if (documentationComment.length) { - documentationComment.push(lineBreakPart()); + const commentTextPart = textPart(comment); + if (!contains(documentationComment, commentTextPart)) { + if (documentationComment.length) { + documentationComment.push(lineBreakPart()); + } + documentationComment.push(commentTextPart); } - documentationComment.push(textPart(comment)); } }); return documentationComment; From e9867a73532d7235cfc57210f0f59bdcd4967f5e Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 11 May 2020 22:14:45 +0000 Subject: [PATCH 3/4] 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 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 2616aea314496..3115109572a0d 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)[] { From 355c28ea70233a27c43a95a7fe1d1384b736c434 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 11 May 2020 22:15:05 +0000 Subject: [PATCH 4/4] Update baselines. --- ...opertiesWithIdenticalJSDocComments01.baseline | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/tests/baselines/reference/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.baseline b/tests/baselines/reference/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.baseline index 1fd2e593d2d68..b03ca277801b2 100644 --- a/tests/baselines/reference/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.baseline +++ b/tests/baselines/reference/quickInfoOnUnionPropertiesWithIdenticalJSDocComments01.baseline @@ -50,22 +50,6 @@ } ], "documentation": [ - { - "text": "A language id, like `typescript`.", - "kind": "text" - }, - { - "text": "\n", - "kind": "lineBreak" - }, - { - "text": "A language id, like `typescript`.", - "kind": "text" - }, - { - "text": "\n", - "kind": "lineBreak" - }, { "text": "A language id, like `typescript`.", "kind": "text"