From 008d40a680a0b30d772da21ad21d52f1fc7a9816 Mon Sep 17 00:00:00 2001 From: Adam Raine Date: Thu, 4 Jan 2024 12:50:17 -0800 Subject: [PATCH 1/2] core: create separate product savings type --- core/audits/audit.js | 11 ++++++----- core/audits/byte-efficiency/byte-efficiency-audit.js | 2 +- report/renderer/performance-category-renderer.js | 4 ++-- types/audit.d.ts | 10 +++++++++- types/config.d.ts | 2 +- types/lhr/audit-result.d.ts | 5 +---- types/lhr/lhr.d.ts | 4 +--- 7 files changed, 21 insertions(+), 17 deletions(-) diff --git a/core/audits/audit.js b/core/audits/audit.js index eddbc74e565e..ba82eb62ba7c 100644 --- a/core/audits/audit.js +++ b/core/audits/audit.js @@ -10,7 +10,7 @@ import {Util} from '../../shared/util.js'; const DEFAULT_PASS = 'defaultPass'; -/** @type {LH.Audit.MetricSavings} */ +/** @type {Record} */ const METRIC_SAVINGS_PRECISION = { FCP: 50, LCP: 50, @@ -349,16 +349,17 @@ class Audit { } /** - * @param {LH.Audit.MetricSavings|undefined} metricSavings - * @return {LH.Audit.MetricSavings|undefined} + * @param {LH.Audit.ProductMetricSavings|undefined} metricSavings + * @return {LH.Audit.ProductMetricSavings|undefined} */ static _quantizeMetricSavings(metricSavings) { if (!metricSavings) return; - /** @type {LH.Audit.MetricSavings} */ + /** @type {LH.Audit.ProductMetricSavings} */ const normalizedMetricSavings = {...metricSavings}; - for (const key of /** @type {Array} */ (Object.keys(metricSavings))) { + // eslint-disable-next-line max-len + for (const key of /** @type {Array} */ (Object.keys(metricSavings))) { let value = metricSavings[key]; if (value === undefined) continue; diff --git a/core/audits/byte-efficiency/byte-efficiency-audit.js b/core/audits/byte-efficiency/byte-efficiency-audit.js index 84db2310f687..fe0b10ed33b7 100644 --- a/core/audits/byte-efficiency/byte-efficiency-audit.js +++ b/core/audits/byte-efficiency/byte-efficiency-audit.js @@ -194,7 +194,7 @@ class ByteEfficiencyAudit extends Audit { const wastedBytes = results.reduce((sum, item) => sum + item.wastedBytes, 0); - /** @type {LH.Audit.MetricSavings} */ + /** @type {LH.Audit.ProductMetricSavings} */ const metricSavings = { FCP: 0, LCP: 0, diff --git a/report/renderer/performance-category-renderer.js b/report/renderer/performance-category-renderer.js index 2ff17325fccc..0ad848059261 100644 --- a/report/renderer/performance-category-renderer.js +++ b/report/renderer/performance-category-renderer.js @@ -5,7 +5,7 @@ */ /** @typedef {import('./dom.js').DOM} DOM */ -/** @typedef {LH.Result.MetricAcronym | 'All'} FilterType */ +/** @typedef {string} FilterType */ import {CategoryRenderer} from './category-renderer.js'; import {ReportUtils} from './report-utils.js'; @@ -360,7 +360,7 @@ export class PerformanceCategoryRenderer extends CategoryRenderer { textEl.textContent = Globals.strings.showRelevantAudits; const filterChoices = [ - /** @type {const} */ ({acronym: 'All'}), + /** @type {const} */ ({acronym: 'All', id: 'All'}), ...filterableMetrics, ]; diff --git a/types/audit.d.ts b/types/audit.d.ts index 9df7851432be..0201a68902ab 100644 --- a/types/audit.d.ts +++ b/types/audit.d.ts @@ -20,6 +20,14 @@ declare module Audit { export type ScoreDisplayModes = AuditResult.ScoreDisplayModes; export type MetricSavings = AuditResult.MetricSavings; + export type ProductMetricSavings = { + FCP?: number; + LCP?: number; + TBT?: number; + CLS?: number; + INP?: number; + }; + type Context = Util.Immutable<{ /** audit options */ options: Record; @@ -87,7 +95,7 @@ declare module Audit { /** If an audit encounters unusual execution circumstances, strings can be put in this optional array to add top-level warnings to the LHR. */ runWarnings?: Array; /** Estimates of how much this audit affects various performance metrics. Values will be in the unit of the respective metrics. */ - metricSavings?: MetricSavings; + metricSavings?: ProductMetricSavings; /** Score details including p10 and median for calculating an audit's log-normal score. */ scoringOptions?: ScoreOptions; /** A string identifying how the score should be interpreted for display. Overrides audit meta `scoreDisplayMode` if defined. */ diff --git a/types/config.d.ts b/types/config.d.ts index 6fbd7213b18e..e3e1d29909dc 100644 --- a/types/config.d.ts +++ b/types/config.d.ts @@ -82,7 +82,7 @@ declare module Config { id: string; weight: number; group?: string; - acronym?: Result.MetricAcronym; + acronym?: string; relevantAudits?: string[]; } diff --git a/types/lhr/audit-result.d.ts b/types/lhr/audit-result.d.ts index ef84db955995..be6450a77328 100644 --- a/types/lhr/audit-result.d.ts +++ b/types/lhr/audit-result.d.ts @@ -6,7 +6,6 @@ import {FormattedIcu} from './i18n.js'; import AuditDetails from './audit-details.js'; -import LHResult from './lhr.js'; interface ScoreDisplayModes { /** Scores of 0-1 (map to displayed scores of 0-100). */ @@ -32,9 +31,7 @@ interface ScoreDisplayModes { type ScoreDisplayMode = ScoreDisplayModes[keyof ScoreDisplayModes]; -export type MetricSavings = { - [M in LHResult.MetricAcronym]?: number; -}; +export type MetricSavings = Partial>; /** Audit result returned in Lighthouse report. All audits offer a description and score of 0-1. */ export interface Result { diff --git a/types/lhr/lhr.d.ts b/types/lhr/lhr.d.ts index 8fcd907ea4fa..df4edf201ab1 100644 --- a/types/lhr/lhr.d.ts +++ b/types/lhr/lhr.d.ts @@ -115,7 +115,7 @@ declare module Result { /** Optional grouping within the category. Matches the key of a Result.Group. */ group?: string; /** The conventional acronym for the audit/metric. */ - acronym?: MetricAcronym; + acronym?: string; /** Any audit IDs closely relevant to this one. */ relevantAudits?: string[]; } @@ -197,8 +197,6 @@ declare module Result { /** Gather mode used to collect artifacts. */ type GatherMode = 'navigation'|'timespan'|'snapshot'; - - type MetricAcronym = 'FCP' | 'LCP' | 'TBT' | 'CLS' | 'INP' | 'SI' | 'TTI' | 'FMP'; } export default Result; From 6f88b28a5edbc701908c845c258b63fd02ab501c Mon Sep 17 00:00:00 2001 From: Adam Raine Date: Thu, 4 Jan 2024 14:07:39 -0800 Subject: [PATCH 2/2] ope --- report/renderer/performance-category-renderer.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/report/renderer/performance-category-renderer.js b/report/renderer/performance-category-renderer.js index 0ad848059261..284422e4a1ca 100644 --- a/report/renderer/performance-category-renderer.js +++ b/report/renderer/performance-category-renderer.js @@ -5,7 +5,6 @@ */ /** @typedef {import('./dom.js').DOM} DOM */ -/** @typedef {string} FilterType */ import {CategoryRenderer} from './category-renderer.js'; import {ReportUtils} from './report-utils.js'; @@ -233,7 +232,7 @@ export class PerformanceCategoryRenderer extends CategoryRenderer { groupEl.classList.add('lh-audit-group--diagnostics'); /** - * @param {FilterType} acronym + * @param {string} acronym */ function refreshFilteredAudits(acronym) { for (const audit of allInsights) { @@ -352,7 +351,7 @@ export class PerformanceCategoryRenderer extends CategoryRenderer { * Render the control to filter the audits by metric. The filtering is done at runtime by CSS only * @param {LH.ReportResult.AuditRef[]} filterableMetrics * @param {HTMLDivElement} categoryEl - * @param {(acronym: FilterType) => void} onFilterChange + * @param {(acronym: string) => void} onFilterChange */ renderMetricAuditFilter(filterableMetrics, categoryEl, onFilterChange) { const metricFilterEl = this.dom.createElement('div', 'lh-metricfilter');