Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions src/utils/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const buildPackageMetricValue = ({
buildTime,
}: Pick<Package, 'name' | 'buildTime'>) => `${name}_${buildTime || 'unknown'}`;

const getPackageTimestampWarnings = ({
export const getPackageTimestampWarnings = ({
dict,
packages,
}: {
Expand All @@ -30,12 +30,25 @@ const getPackageTimestampWarnings = ({
return new Map<number, string[]>();
}

const packageCandidates = packages
.map((pkg) => ({
pkg,
currentMetricValue: buildPackageMetricValue(pkg),
}))
.sort((a, b) => b.pkg.name.length - a.pkg.name.length);
const packageCandidates = packages.map((pkg) => ({
pkg,
currentMetricValue: buildPackageMetricValue(pkg),
}));

const exactMatchMap = new Map<string, (typeof packageCandidates)[0]>();
const nameMatchMap = new Map<string, (typeof packageCandidates)[0]>();

// Sort by name length descending to ensure the longest package name wins in prefix match
for (const candidate of [...packageCandidates].sort(
(a, b) => b.pkg.name.length - a.pkg.name.length,
)) {
if (!exactMatchMap.has(candidate.currentMetricValue)) {
exactMatchMap.set(candidate.currentMetricValue, candidate);
}
if (!nameMatchMap.has(candidate.pkg.name)) {
nameMatchMap.set(candidate.pkg.name, candidate);
}
}

for (const entry of dict) {
if (!entry.startsWith(PACKAGE_METRIC_PREFIX)) {
Expand All @@ -47,11 +60,19 @@ const getPackageTimestampWarnings = ({
continue;
}

const matchedPackage = packageCandidates.find(
({ pkg, currentMetricValue }) =>
metricValue === currentMetricValue ||
metricValue.startsWith(`${pkg.name}_`),
);
let matchedPackage = exactMatchMap.get(metricValue);

if (!matchedPackage) {
let idx = metricValue.lastIndexOf('_');
while (idx !== -1) {
const potentialName = metricValue.slice(0, idx);
matchedPackage = nameMatchMap.get(potentialName);
if (matchedPackage) {
break;
}
idx = metricValue.lastIndexOf('_', idx - 1);
}
}

if (!matchedPackage || metricValue === matchedPackage.currentMetricValue) {
continue;
Expand Down