Skip to content

Commit b4eaf9d

Browse files
author
Andy Hanson
committed
Keep symbol.declarations unique
1 parent ab8233c commit b4eaf9d

File tree

7 files changed

+32
-42
lines changed

7 files changed

+32
-42
lines changed

src/compiler/checker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6421,7 +6421,7 @@ namespace ts {
64216421
let commonType: Type;
64226422
for (const prop of props) {
64236423
if (prop.declarations) {
6424-
addRange(declarations, prop.declarations);
6424+
addRangeUnique(declarations, prop.declarations);
64256425
}
64266426
const type = getTypeOfSymbol(prop);
64276427
if (!commonType) {
@@ -8468,7 +8468,7 @@ namespace ts {
84688468
const rightProp = members.get(leftProp.escapedName);
84698469
const rightType = getTypeOfSymbol(rightProp);
84708470
if (rightProp.flags & SymbolFlags.Optional) {
8471-
const declarations: Declaration[] = concatenate(leftProp.declarations, rightProp.declarations);
8471+
const declarations: Declaration[] = concatUnique(leftProp.declarations, rightProp.declarations);
84728472
const flags = SymbolFlags.Property | (leftProp.flags & SymbolFlags.Optional);
84738473
const result = createSymbol(flags, leftProp.escapedName);
84748474
result.type = getUnionType([getTypeOfSymbol(leftProp), getTypeWithFacts(rightType, TypeFacts.NEUndefined)]);

src/compiler/core.ts

+14
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,20 @@ namespace ts {
984984
}
985985
}
986986

987+
export function addRangeUnique<T>(to: T[], from: ReadonlyArray<T> | undefined): void {
988+
if (from) {
989+
for (const em of from) {
990+
pushIfUnique(to, em);
991+
}
992+
}
993+
}
994+
995+
export function concatUnique<T>(array1: ReadonlyArray<T>, array2: ReadonlyArray<T>): T[] {
996+
const res = array1.slice();
997+
addRangeUnique(res, array2);
998+
return res;
999+
}
1000+
9871001
/**
9881002
* Unlike `pushIfUnique`, this can take `undefined` as an input, and returns a new array.
9891003
*/

src/services/jsDoc.ts

+6-30
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,24 @@ namespace ts.JsDoc {
4646
let jsDocTagNameCompletionEntries: CompletionEntry[];
4747
let jsDocTagCompletionEntries: CompletionEntry[];
4848

49-
export function getJsDocCommentsFromDeclarations(declarations?: Declaration[]) {
49+
export function getJsDocCommentsFromDeclarations(declarations: Declaration[] | undefined) {
50+
if (!declarations) return emptyArray;
5051
// Only collect doc comments from duplicate declarations once:
5152
// In case of a union property there might be same declaration multiple times
5253
// which only varies in type parameter
5354
// Eg. const a: Array<string> | Array<number>; a.length
5455
// The property length will have two declarations of property length coming
5556
// from Array<T> - Array<string> and Array<number>
5657
const documentationComment: SymbolDisplayPart[] = [];
57-
forEachUnique(declarations, declaration => {
58+
for (const declaration of declarations) {
5859
for (const { comment } of getCommentHavingNodes(declaration)) {
5960
if (comment === undefined) continue;
6061
if (documentationComment.length) {
6162
documentationComment.push(lineBreakPart());
6263
}
6364
documentationComment.push(textPart(comment));
6465
}
65-
});
66+
}
6667
return documentationComment;
6768
}
6869

@@ -77,15 +78,9 @@ namespace ts.JsDoc {
7778
}
7879
}
7980

80-
export function getJsDocTagsFromDeclarations(declarations?: Declaration[]): JSDocTagInfo[] {
81+
export function getJsDocTagsFromDeclarations(declarations: Declaration[] | undefined): JSDocTagInfo[] | undefined {
8182
// Only collect doc comments from duplicate declarations once.
82-
const tags: JSDocTagInfo[] = [];
83-
forEachUnique(declarations, declaration => {
84-
for (const tag of getJSDocTags(declaration)) {
85-
tags.push({ name: tag.tagName.text, text: getCommentText(tag) });
86-
}
87-
});
88-
return tags;
83+
return flatMap(declarations, d => getJSDocTags(d).map(tag => ({ name: tag.tagName.text, text: getCommentText(tag) })));
8984
}
9085

9186
function getCommentText(tag: JSDocTag): string | undefined {
@@ -119,25 +114,6 @@ namespace ts.JsDoc {
119114
}
120115
}
121116

122-
/**
123-
* Iterates through 'array' by index and performs the callback on each element of array until the callback
124-
* returns a truthy value, then returns that value.
125-
* If no such value is found, the callback is applied to each element of array and undefined is returned.
126-
*/
127-
function forEachUnique<T, U>(array: T[], callback: (element: T, index: number) => U): U {
128-
if (array) {
129-
for (let i = 0; i < array.length; i++) {
130-
if (array.indexOf(array[i]) === i) {
131-
const result = callback(array[i], i);
132-
if (result) {
133-
return result;
134-
}
135-
}
136-
}
137-
}
138-
return undefined;
139-
}
140-
141117
export function getJSDocTagNameCompletions(): CompletionEntry[] {
142118
return jsDocTagNameCompletionEntries || (jsDocTagNameCompletionEntries = map(jsDocTagNames, tagName => {
143119
return {

tests/baselines/reference/arraySlice.symbols

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var arr: string[] | number[];
33
>arr : Symbol(arr, Decl(arraySlice.ts, 0, 3))
44

55
arr.splice(1, 1);
6-
>arr.splice : Symbol(splice, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
6+
>arr.splice : Symbol(splice, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
77
>arr : Symbol(arr, Decl(arraySlice.ts, 0, 3))
8-
>splice : Symbol(splice, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
8+
>splice : Symbol(splice, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
99

tests/baselines/reference/controlFlowArrayErrors.symbols

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ function f6() {
121121
>x : Symbol(x, Decl(controlFlowArrayErrors.ts, 37, 7))
122122

123123
x.push(99); // Error
124-
>x.push : Symbol(push, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
124+
>x.push : Symbol(push, Decl(lib.d.ts, --, --))
125125
>x : Symbol(x, Decl(controlFlowArrayErrors.ts, 37, 7))
126-
>push : Symbol(push, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
126+
>push : Symbol(push, Decl(lib.d.ts, --, --))
127127
}
128128

129129
function f7() {

tests/baselines/reference/typeParameterExtendingUnion1.symbols

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ function f<T extends Cat | Dog>(a: T) {
3333
>T : Symbol(T, Decl(typeParameterExtendingUnion1.ts, 8, 11))
3434

3535
a.run();
36-
>a.run : Symbol(run, Decl(typeParameterExtendingUnion1.ts, 0, 14), Decl(typeParameterExtendingUnion1.ts, 0, 14))
36+
>a.run : Symbol(run, Decl(typeParameterExtendingUnion1.ts, 0, 14))
3737
>a : Symbol(a, Decl(typeParameterExtendingUnion1.ts, 8, 32))
38-
>run : Symbol(run, Decl(typeParameterExtendingUnion1.ts, 0, 14), Decl(typeParameterExtendingUnion1.ts, 0, 14))
38+
>run : Symbol(run, Decl(typeParameterExtendingUnion1.ts, 0, 14))
3939

4040
run(a);
4141
>run : Symbol(run, Decl(typeParameterExtendingUnion1.ts, 2, 33))

tests/baselines/reference/typeParameterExtendingUnion2.symbols

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ function run(a: Cat | Dog) {
2020
>Dog : Symbol(Dog, Decl(typeParameterExtendingUnion2.ts, 1, 33))
2121

2222
a.run();
23-
>a.run : Symbol(run, Decl(typeParameterExtendingUnion2.ts, 0, 14), Decl(typeParameterExtendingUnion2.ts, 0, 14))
23+
>a.run : Symbol(run, Decl(typeParameterExtendingUnion2.ts, 0, 14))
2424
>a : Symbol(a, Decl(typeParameterExtendingUnion2.ts, 4, 13))
25-
>run : Symbol(run, Decl(typeParameterExtendingUnion2.ts, 0, 14), Decl(typeParameterExtendingUnion2.ts, 0, 14))
25+
>run : Symbol(run, Decl(typeParameterExtendingUnion2.ts, 0, 14))
2626
}
2727

2828
function f<T extends Cat | Dog>(a: T) {
@@ -34,9 +34,9 @@ function f<T extends Cat | Dog>(a: T) {
3434
>T : Symbol(T, Decl(typeParameterExtendingUnion2.ts, 8, 11))
3535

3636
a.run();
37-
>a.run : Symbol(run, Decl(typeParameterExtendingUnion2.ts, 0, 14), Decl(typeParameterExtendingUnion2.ts, 0, 14))
37+
>a.run : Symbol(run, Decl(typeParameterExtendingUnion2.ts, 0, 14))
3838
>a : Symbol(a, Decl(typeParameterExtendingUnion2.ts, 8, 32))
39-
>run : Symbol(run, Decl(typeParameterExtendingUnion2.ts, 0, 14), Decl(typeParameterExtendingUnion2.ts, 0, 14))
39+
>run : Symbol(run, Decl(typeParameterExtendingUnion2.ts, 0, 14))
4040

4141
run(a);
4242
>run : Symbol(run, Decl(typeParameterExtendingUnion2.ts, 2, 33))

0 commit comments

Comments
 (0)