Skip to content

Remove duplicate JSDoc comments #38489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(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.
Expand Down
9 changes: 3 additions & 6 deletions src/services/jsDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,14 @@ namespace ts.JsDoc {
// Eg. const a: Array<string> | Array<number>; a.length
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on 86 claims it's already deduping? Is the forEachUnique meant to do this? Why didn't that work

Copy link
Member Author

@DanielRosenwasser DanielRosenwasser May 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I thought the same thing (i.e. "whoever wrote this must be mistaken!") but reading that comment, I understand forEachUnique must dedupe across declarations, and something like

(Array<string> | Array<number>)["length"]

has the same declarations between Array<string> and Array<number> for either of them.

Copy link
Member Author

@DanielRosenwasser DanielRosenwasser May 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could technically let the identical string checks handle this instead.

// The property length will have two declarations of property length coming
// from Array<T> - Array<string> and Array<number>
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)[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path="fourslash.ts" />

////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();