Skip to content

Commit

Permalink
fix(inferer): allow {} prototype name in generics
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jun 10, 2022
1 parent 2eca5ca commit 2f64c19
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
22 changes: 11 additions & 11 deletions packages/babel-types/src/modifications/flow/removeTypeDuplicates.ts
Expand Up @@ -20,13 +20,13 @@ export default function removeTypeDuplicates(
// todo(babel-8): change type to Array<...>
nodes: ReadonlyArray<t.FlowType | false | null | undefined>,
): t.FlowType[] {
const generics = {};
const bases = {};
const generics = new Map<string, t.GenericTypeAnnotation>();
const bases = new Map<t.FlowBaseAnnotation["type"], t.FlowBaseAnnotation>();

// store union type groups to circular references
const typeGroups = new Set<t.FlowType[]>();

const types = [];
const types: t.FlowType[] = [];

for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
Expand All @@ -43,7 +43,7 @@ export default function removeTypeDuplicates(
}

if (isFlowBaseAnnotation(node)) {
bases[node.type] = node;
bases.set(node.type, node);
continue;
}

Expand All @@ -60,8 +60,8 @@ export default function removeTypeDuplicates(
if (isGenericTypeAnnotation(node)) {
const name = getQualifiedName(node.id);

if (generics[name]) {
let existing = generics[name];
if (generics.has(name)) {
let existing: t.Flow = generics.get(name);
if (existing.typeParameters) {
if (node.typeParameters) {
existing.typeParameters.params = removeTypeDuplicates(
Expand All @@ -72,7 +72,7 @@ export default function removeTypeDuplicates(
existing = node.typeParameters;
}
} else {
generics[name] = node;
generics.set(name, node);
}

continue;
Expand All @@ -82,13 +82,13 @@ export default function removeTypeDuplicates(
}

// add back in bases
for (const type of Object.keys(bases)) {
types.push(bases[type]);
for (const [, baseType] of bases) {
types.push(baseType);
}

// add back in generics
for (const name of Object.keys(generics)) {
types.push(generics[name]);
for (const [, genericName] of generics) {
types.push(genericName);
}

return types;
Expand Down
Expand Up @@ -19,8 +19,8 @@ function getQualifiedName(node: t.TSTypeReference["typeName"]) {
export default function removeTypeDuplicates(
nodes: Array<t.TSType>,
): Array<t.TSType> {
const generics = {};
const bases = {};
const generics = new Map<string, t.TSTypeReference>();
const bases = new Map<t.TSBaseType["type"], t.TSBaseType>();

// store union type groups to circular references
const typeGroups = new Set<t.TSType[]>();
Expand All @@ -43,7 +43,7 @@ export default function removeTypeDuplicates(

// Analogue of FlowBaseAnnotation
if (isTSBaseType(node)) {
bases[node.type] = node;
bases.set(node.type, node);
continue;
}

Expand All @@ -59,8 +59,8 @@ export default function removeTypeDuplicates(
if (isTSTypeReference(node) && node.typeParameters) {
const name = getQualifiedName(node.typeName);

if (generics[name]) {
let existing = generics[name];
if (generics.has(name)) {
let existing: t.TypeScript = generics.get(name);
if (existing.typeParameters) {
if (node.typeParameters) {
existing.typeParameters.params = removeTypeDuplicates(
Expand All @@ -71,7 +71,7 @@ export default function removeTypeDuplicates(
existing = node.typeParameters;
}
} else {
generics[name] = node;
generics.set(name, node);
}

continue;
Expand All @@ -81,13 +81,13 @@ export default function removeTypeDuplicates(
}

// add back in bases
for (const type of Object.keys(bases)) {
types.push(bases[type]);
for (const [, baseType] of bases) {
types.push(baseType);
}

// add back in generics
for (const name of Object.keys(generics)) {
types.push(generics[name]);
for (const [, genericName] of generics) {
types.push(genericName);
}

return types;
Expand Down

0 comments on commit 2f64c19

Please sign in to comment.