(report: Report)
| 18 | } |
| 19 | |
| 20 | export function scoreReport(report: Report): ScoredReport { |
| 21 | const allScoredAuditsAndGroups = new Map<string, AuditReport | ScoredGroup>(); |
| 22 | |
| 23 | const scoredPlugins = report.plugins.map(plugin => { |
| 24 | const { groups, ...pluginProps } = plugin; |
| 25 | |
| 26 | plugin.audits.forEach(audit => { |
| 27 | allScoredAuditsAndGroups.set(`${plugin.slug}-${audit.slug}-audit`, audit); |
| 28 | }); |
| 29 | |
| 30 | function groupScoreFn(ref: GroupRef) { |
| 31 | const score = allScoredAuditsAndGroups.get( |
| 32 | `${plugin.slug}-${ref.slug}-audit`, |
| 33 | )?.score; |
| 34 | if (score == null) { |
| 35 | throw new GroupRefInvalidError(ref.slug, plugin.slug); |
| 36 | } |
| 37 | return score; |
| 38 | } |
| 39 | |
| 40 | const scoredGroups = |
| 41 | groups?.map(group => ({ |
| 42 | ...group, |
| 43 | score: calculateScore(group.refs, groupScoreFn), |
| 44 | })) ?? []; |
| 45 | |
| 46 | scoredGroups.forEach(group => { |
| 47 | allScoredAuditsAndGroups.set(`${plugin.slug}-${group.slug}-group`, group); |
| 48 | }); |
| 49 | |
| 50 | return { |
| 51 | ...pluginProps, |
| 52 | ...(scoredGroups.length > 0 && { groups: scoredGroups }), |
| 53 | }; |
| 54 | }); |
| 55 | |
| 56 | function catScoreFn(ref: CategoryRef) { |
| 57 | const key = `${ref.plugin}-${ref.slug}-${ref.type}`; |
| 58 | const item = allScoredAuditsAndGroups.get(key); |
| 59 | if (!item) { |
| 60 | throw new Error( |
| 61 | `Category has invalid ref - ${ref.type} with slug ${key} not found in ${ref.plugin} plugin`, |
| 62 | ); |
| 63 | } |
| 64 | return item.score; |
| 65 | } |
| 66 | |
| 67 | const scoredCategories = report.categories?.map(category => ({ |
| 68 | ...category, |
| 69 | score: calculateScore(category.refs, catScoreFn), |
| 70 | })); |
| 71 | |
| 72 | return { |
| 73 | ...deepClone(report), |
| 74 | plugins: scoredPlugins, |
| 75 | categories: scoredCategories, |
| 76 | }; |
| 77 | } |
no test coverage detected