From c76ba6380afbc8487f95de9afed0f78b64c8c895 Mon Sep 17 00:00:00 2001 From: Brendan Kenny Date: Mon, 20 Aug 2018 15:46:30 -0700 Subject: [PATCH 1/2] core(tsc): remove more reliance on implicit index signatures --- lighthouse-cli/bin.js | 1 + lighthouse-core/audits/dobetterweb/dom-size.js | 1 + lighthouse-core/audits/user-timings.js | 1 + .../gather/computed/metrics/lantern-metric.js | 1 + .../dependency-graph/simulator/network-analyzer.js | 12 +++++++++--- lighthouse-core/lib/i18n.js | 3 +++ .../report/html/renderer/report-ui-features.js | 3 ++- lighthouse-core/report/html/renderer/util.js | 5 +++++ lighthouse-core/runner.js | 1 + lighthouse-core/scoring.js | 1 + lighthouse-core/scripts/i18n/collect-strings.js | 1 + .../app/src/lighthouse-ext-background.js | 3 +++ 12 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lighthouse-cli/bin.js b/lighthouse-cli/bin.js index d02844d89b12..4baa04b15ee8 100644 --- a/lighthouse-cli/bin.js +++ b/lighthouse-cli/bin.js @@ -107,6 +107,7 @@ function run() { } }) .then(_ => { + // @ts-ignore TODO(bckenny): Sentry type checking Sentry.init({ url, flags: cliFlags, diff --git a/lighthouse-core/audits/dobetterweb/dom-size.js b/lighthouse-core/audits/dobetterweb/dom-size.js index de0872d36096..38cb9e6e0fbc 100644 --- a/lighthouse-core/audits/dobetterweb/dom-size.js +++ b/lighthouse-core/audits/dobetterweb/dom-size.js @@ -101,6 +101,7 @@ class DOMSize extends Audit { {key: 'width', itemType: 'text', text: str_(UIStrings.columnDOMWidth)}, ]; + /** @type {Array>} */ const items = [ { totalNodes: Util.formatNumber(stats.totalDOMNodes), diff --git a/lighthouse-core/audits/user-timings.js b/lighthouse-core/audits/user-timings.js index 6429bfd7c918..d3015f13d0a7 100644 --- a/lighthouse-core/audits/user-timings.js +++ b/lighthouse-core/audits/user-timings.js @@ -56,6 +56,7 @@ class UserTimings extends Audit { static filterTrace(tabTrace) { /** @type {Array} */ const userTimings = []; + /** @type {Record} */ const measuresStartTimes = {}; // Get all blink.user_timing events diff --git a/lighthouse-core/gather/computed/metrics/lantern-metric.js b/lighthouse-core/gather/computed/metrics/lantern-metric.js index 43a15c2b09f0..425ce6d06cc0 100644 --- a/lighthouse-core/gather/computed/metrics/lantern-metric.js +++ b/lighthouse-core/gather/computed/metrics/lantern-metric.js @@ -85,6 +85,7 @@ class LanternMetricArtifact extends ComputedArtifact { const optimisticGraph = this.getOptimisticGraph(graph, traceOfTab); const pessimisticGraph = this.getPessimisticGraph(graph, traceOfTab); + /** @type {{flexibleOrdering?: boolean, label?: string}} */ let simulateOptions = {label: `optimistic${metricName}`}; const optimisticSimulation = simulator.simulate(optimisticGraph, simulateOptions); diff --git a/lighthouse-core/lib/dependency-graph/simulator/network-analyzer.js b/lighthouse-core/lib/dependency-graph/simulator/network-analyzer.js index 2eaac3dd762e..c2ad615fa0e8 100644 --- a/lighthouse-core/lib/dependency-graph/simulator/network-analyzer.js +++ b/lighthouse-core/lib/dependency-graph/simulator/network-analyzer.js @@ -11,8 +11,11 @@ const NetworkRequest = require('../../network-request'); // Assume that 40% of TTFB was server response time by default for static assets const DEFAULT_SERVER_RESPONSE_PERCENTAGE = 0.4; -// For certain resource types, server response time takes up a greater percentage of TTFB (dynamic -// assets like HTML documents, XHR/API calls, etc) +/** + * For certain resource types, server response time takes up a greater percentage of TTFB (dynamic + * assets like HTML documents, XHR/API calls, etc) + * @type {Partial>} + */ const SERVER_RESPONSE_PERCENTAGE_OF_TTFB = { Document: 0.9, XHR: 0.9, @@ -73,9 +76,11 @@ class NetworkAnalyzer { return summaryByKey; } + /** @typedef {{record: LH.Artifacts.NetworkRequest, timing: LH.Crdp.Network.ResourceTiming, connectionReused?: boolean}} RequestInfo */ + /** * @param {LH.Artifacts.NetworkRequest[]} records - * @param {function(any):any} iteratee + * @param {(e: RequestInfo) => number | number[] | undefined} iteratee * @return {Map} */ static _estimateValueByOrigin(records, iteratee) { @@ -190,6 +195,7 @@ class NetworkAnalyzer { static _estimateRTTByOriginViaHeadersEndTiming(records) { return NetworkAnalyzer._estimateValueByOrigin(records, ({record, timing, connectionReused}) => { if (!Number.isFinite(timing.receiveHeadersEnd) || timing.receiveHeadersEnd < 0) return; + if (!record.resourceType) return; const serverResponseTimePercentage = SERVER_RESPONSE_PERCENTAGE_OF_TTFB[record.resourceType] || DEFAULT_SERVER_RESPONSE_PERCENTAGE; diff --git a/lighthouse-core/lib/i18n.js b/lighthouse-core/lib/i18n.js index 05512a2f32dd..2741374647f5 100644 --- a/lighthouse-core/lib/i18n.js +++ b/lighthouse-core/lib/i18n.js @@ -175,6 +175,7 @@ function _formatPathAsString(pathInLHR) { */ function getRendererFormattedStrings(locale) { const icuMessageIds = Object.keys(LOCALES[locale]).filter(f => f.includes('core/report/html/')); + /** @type {LH.I18NRendererStrings} */ const strings = {}; for (const icuMessageId of icuMessageIds) { const [filename, varName] = icuMessageId.split(' | '); @@ -190,6 +191,7 @@ function getRendererFormattedStrings(locale) { * @param {Record} fileStrings */ function createMessageInstanceIdFn(filename, fileStrings) { + /** @type {Record} */ const mergedStrings = {...UIStrings, ...fileStrings}; /** @param {string} icuMessage @param {*} [values] */ @@ -284,6 +286,7 @@ function replaceIcuMessageInstanceIds(lhr, locale) { } } + /** @type {LH.I18NMessages} */ const icuMessagePaths = {}; replaceInObject(lhr, icuMessagePaths); return icuMessagePaths; diff --git a/lighthouse-core/report/html/renderer/report-ui-features.js b/lighthouse-core/report/html/renderer/report-ui-features.js index c3ea5bcb0977..97d590c59272 100644 --- a/lighthouse-core/report/html/renderer/report-ui-features.js +++ b/lighthouse-core/report/html/renderer/report-ui-features.js @@ -90,7 +90,7 @@ class ReportUIFeatures { this._setupHeaderAnimation(); this._resetUIState(); this._document.addEventListener('keydown', this.printShortCutDetect); - // @ts-ignore - tsc thinks document can't listen for `copy` + // @ts-ignore - TODO(bckenny): tsc thinks document can't listen for `copy`. Remove ignore in 3.1. this._document.addEventListener('copy', this.onCopy); } @@ -447,6 +447,7 @@ class ReportUIFeatures { */ getReportHtml() { this._resetUIState(); + // @ts-ignore - technically documentElement can be null, but that's dumb - https://dom.spec.whatwg.org/#document-element return this._document.documentElement.outerHTML; } diff --git a/lighthouse-core/report/html/renderer/util.js b/lighthouse-core/report/html/renderer/util.js index b5c3ad0c3457..b059a2c75faf 100644 --- a/lighthouse-core/report/html/renderer/util.js +++ b/lighthouse-core/report/html/renderer/util.js @@ -223,6 +223,7 @@ class Util { * @return {string} */ static formatDateTime(date) { + /** @type {Intl.DateTimeFormatOptions} */ const options = { month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: 'numeric', timeZoneName: 'short', @@ -444,6 +445,10 @@ class Util { */ Util.numberDateLocale = 'en'; +/** + * Report-renderer-specific strings. + * @type {LH.I18NRendererStrings} + */ Util.UIStrings = { /** Disclaimer shown to users below the metric values (First Contentful Paint, Time to Interactive, etc) to warn them that the numbers they see will likely change slightly the next time they run Lighthouse. */ varianceDisclaimer: 'Values are estimated and may vary.', diff --git a/lighthouse-core/runner.js b/lighthouse-core/runner.js index c0eeb0d2b8f4..117f115ed3ff 100644 --- a/lighthouse-core/runner.js +++ b/lighthouse-core/runner.js @@ -368,6 +368,7 @@ class Runner { const ArtifactClass = require('./gather/computed/' + filename); const artifact = new ArtifactClass(computedArtifacts); // define the request* function that will be exposed on `artifacts` + // @ts-ignore - doesn't have an index signature, so can't be set dynamically. computedArtifacts['request' + artifact.name] = artifact.request.bind(artifact); }); diff --git a/lighthouse-core/scoring.js b/lighthouse-core/scoring.js index 1768c782bdbb..ddbab9fdbf1a 100644 --- a/lighthouse-core/scoring.js +++ b/lighthouse-core/scoring.js @@ -50,6 +50,7 @@ class ReportScoring { * @return {Object} */ static scoreAllCategories(configCategories, resultsByAuditId) { + /** @type {Record} */ const scoredCategories = {}; for (const [categoryId, configCategory] of Object.entries(configCategories)) { diff --git a/lighthouse-core/scripts/i18n/collect-strings.js b/lighthouse-core/scripts/i18n/collect-strings.js index f67274823489..4a5fc225eb42 100644 --- a/lighthouse-core/scripts/i18n/collect-strings.js +++ b/lighthouse-core/scripts/i18n/collect-strings.js @@ -103,6 +103,7 @@ function collectAllStringsInDir(dir, strings = {}) { */ function writeStringsToLocaleFormat(locale, strings) { const fullPath = path.join(LH_ROOT, `lighthouse-core/lib/locales/${locale}.json`); + /** @type {Record} */ const output = {}; const sortedEntries = Object.entries(strings).sort(([keyA], [keyB]) => keyA.localeCompare(keyB)); for (const [key, defn] of sortedEntries) { diff --git a/lighthouse-extension/app/src/lighthouse-ext-background.js b/lighthouse-extension/app/src/lighthouse-ext-background.js index 4b8b46146f7f..59d11c125e47 100644 --- a/lighthouse-extension/app/src/lighthouse-ext-background.js +++ b/lighthouse-extension/app/src/lighthouse-ext-background.js @@ -122,7 +122,9 @@ function createReportPageAsBlob(reportHtml) { */ function saveSettings(settings) { const storage = { + /** @type {Record} */ [STORAGE_KEY]: {}, + /** @type {Record} */ [SETTINGS_KEY]: {}, }; @@ -149,6 +151,7 @@ function loadSettings() { chrome.storage.local.get([STORAGE_KEY, SETTINGS_KEY], result => { // Start with list of all default categories set to true so list is // always up to date. + /** @type {Record} */ const defaultCategories = {}; background.getDefaultCategories().forEach(category => { defaultCategories[category.id] = true; From 6c896b1b522cd64cf5e8cbd15f25de0362087d74 Mon Sep 17 00:00:00 2001 From: Brendan Kenny Date: Mon, 20 Aug 2018 18:30:20 -0700 Subject: [PATCH 2/2] add dummy resourceType to test --- .../lib/dependency-graph/simulator/network-analyzer-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lighthouse-core/test/lib/dependency-graph/simulator/network-analyzer-test.js b/lighthouse-core/test/lib/dependency-graph/simulator/network-analyzer-test.js index 2bdae4f4ea3f..db6444f71c3e 100644 --- a/lighthouse-core/test/lib/dependency-graph/simulator/network-analyzer-test.js +++ b/lighthouse-core/test/lib/dependency-graph/simulator/network-analyzer-test.js @@ -170,7 +170,7 @@ describe('DependencyGraph/Simulator/NetworkAnalyzer', () => { it('should infer from TTFB when available', () => { const timing = {receiveHeadersEnd: 1000}; - const record = createRecord({startTime: 0, endTime: 1, timing}); + const record = createRecord({startTime: 0, endTime: 1, timing, resourceType: 'Other'}); const result = NetworkAnalyzer.estimateRTTByOrigin([record], { coarseEstimateMultiplier: 1, });