Skip to content

Commit 3694ed5

Browse files
committed
JS: Deduplicate union/intersection members
1 parent cab7650 commit 3694ed5

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

javascript/extractor/lib/typescript/src/type_table.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,14 @@ export class TypeTable {
614614
// cannot be written using TypeScript syntax - so we ignore them entirely.
615615
return null;
616616
}
617-
return this.makeTypeStringVector("union", unionType.types);
617+
return this.makeDeduplicatedTypeStringVector("union", unionType.types);
618618
}
619619
if (flags & ts.TypeFlags.Intersection) {
620620
let intersectionType = type as ts.IntersectionType;
621621
if (intersectionType.types.length === 0) {
622622
return null; // Ignore malformed type.
623623
}
624-
return this.makeTypeStringVector("intersection", intersectionType.types);
624+
return this.makeDeduplicatedTypeStringVector("intersection", intersectionType.types);
625625
}
626626
if (isTypeReference(type) && (type.target.objectFlags & ts.ObjectFlags.Tuple)) {
627627
// Encode the minimum length and presence of rest element in the first two parts of the type string.
@@ -784,6 +784,27 @@ export class TypeTable {
784784
return hash;
785785
}
786786

787+
/**
788+
* Returns the given string with the IDs of the given types appended,
789+
* ignoring duplicates, and each separated by `;`.
790+
*/
791+
private makeDeduplicatedTypeStringVector(tag: string, types: ReadonlyArray<ts.Type>, length = types.length): string | null {
792+
let seenIds = new Set<number>();
793+
let numberOfSeenIds = 0;
794+
let hash = tag;
795+
for (let i = 0; i < length; ++i) {
796+
let id = this.getId(types[i], false);
797+
if (id == null) return null;
798+
seenIds.add(id);
799+
if (seenIds.size > numberOfSeenIds) {
800+
// This ID was not seen before
801+
++numberOfSeenIds;
802+
hash += ";" + id;
803+
}
804+
}
805+
return hash;
806+
}
807+
787808
/** Returns the type of `symbol` or `null` if it could not be computed. */
788809
private tryGetTypeOfSymbol(symbol: ts.Symbol) {
789810
try {

0 commit comments

Comments
 (0)