Urara-Blog/node_modules/.pnpm-store/v3/files/81/eadb2eb72846714b330c1d0c0c96275819d380efc5f528197193129054e7f3da3d0831a3e04895ee715e6f256d35152524a27b27c0430b608625e5bc9e3eb1
2022-08-14 01:14:53 +08:00

78 lines
No EOL
1.7 KiB
Text

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = removeTypeDuplicates;
var _generated = require("../../validators/generated");
function getQualifiedName(node) {
return (0, _generated.isIdentifier)(node) ? node.name : `${node.id.name}.${getQualifiedName(node.qualification)}`;
}
function removeTypeDuplicates(nodes) {
const generics = new Map();
const bases = new Map();
const typeGroups = new Set();
const types = [];
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
if (!node) continue;
if (types.indexOf(node) >= 0) {
continue;
}
if ((0, _generated.isAnyTypeAnnotation)(node)) {
return [node];
}
if ((0, _generated.isFlowBaseAnnotation)(node)) {
bases.set(node.type, node);
continue;
}
if ((0, _generated.isUnionTypeAnnotation)(node)) {
if (!typeGroups.has(node.types)) {
nodes = nodes.concat(node.types);
typeGroups.add(node.types);
}
continue;
}
if ((0, _generated.isGenericTypeAnnotation)(node)) {
const name = getQualifiedName(node.id);
if (generics.has(name)) {
let existing = generics.get(name);
if (existing.typeParameters) {
if (node.typeParameters) {
existing.typeParameters.params = removeTypeDuplicates(existing.typeParameters.params.concat(node.typeParameters.params));
}
} else {
existing = node.typeParameters;
}
} else {
generics.set(name, node);
}
continue;
}
types.push(node);
}
for (const [, baseType] of bases) {
types.push(baseType);
}
for (const [, genericName] of generics) {
types.push(genericName);
}
return types;
}