Skip to content

Commit e9867a7

Browse files
Add and use the 'intersperse' helper function.
1 parent 74d6d04 commit e9867a7

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/compiler/core.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ namespace ts {
129129
return map;
130130
}
131131

132+
/**
133+
* Creates a new array with `element` interspersed in between each element of `input`
134+
* if there is more than 1 value in `input`. Otherwise, returns the existing array.
135+
*/
136+
export function intersperse<T>(input: T[], element: T): T[] {
137+
if (input.length <= 1) {
138+
return input;
139+
}
140+
const result: T[] = [];
141+
for (let i = 0, n = input.length; i < n; i++) {
142+
if (i) result.push(element);
143+
result.push(input[i]);
144+
}
145+
return result;
146+
}
147+
132148
/**
133149
* Iterates through `array` by index and performs the callback on each element of array until the callback
134150
* returns a falsey value, then returns false.

src/services/jsDoc.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,14 @@ namespace ts.JsDoc {
8989
// Eg. const a: Array<string> | Array<number>; a.length
9090
// The property length will have two declarations of property length coming
9191
// from Array<T> - Array<string> and Array<number>
92-
const documentationComment: SymbolDisplayPart[] = [];
92+
const documentationComment: string[] = [];
9393
forEachUnique(declarations, declaration => {
9494
for (const { comment } of getCommentHavingNodes(declaration)) {
9595
if (comment === undefined) continue;
96-
const commentTextPart = textPart(comment);
97-
if (!contains(documentationComment, commentTextPart)) {
98-
if (documentationComment.length) {
99-
documentationComment.push(lineBreakPart());
100-
}
101-
documentationComment.push(commentTextPart);
102-
}
96+
pushIfUnique(documentationComment, comment);
10397
}
10498
});
105-
return documentationComment;
99+
return intersperse(map(documentationComment, textPart), lineBreakPart());
106100
}
107101

108102
function getCommentHavingNodes(declaration: Declaration): readonly (JSDoc | JSDocTag)[] {

0 commit comments

Comments
 (0)