Skip to content

Commit

Permalink
core(tsc): more type checking of top-level audits (#5089)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny authored May 2, 2018
1 parent 9ddae1e commit ed14c8e
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 48 deletions.
21 changes: 13 additions & 8 deletions lighthouse-core/audits/multi-check-audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ const Audit = require('./audit');

class MultiCheckAudit extends Audit {
/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
* @param {LH.Artifacts} artifacts
* @return {Promise<LH.Audit.Product>}
*/
static audit(artifacts) {
return Promise.resolve(this.audit_(artifacts)).then(result => this.createAuditResult(result));
return Promise.resolve(this.audit_(artifacts)).then(result => this.createAuditProduct(result));
}

/**
* @param {!{failures: !Array<!string>, themeColor: ?string, manifestValues: ?Object, }} result
* @return {!AuditResult}
* @param {{failures: Array<string>, warnings?: Array<string>, manifestValues?: LH.Artifacts.ManifestValues}} result
* @return {LH.Audit.Product}
*/
static createAuditResult(result) {
static createAuditProduct(result) {
const extendedInfo = {
value: result,
};
Expand All @@ -51,12 +51,17 @@ class MultiCheckAudit extends Audit {
};
}

/* eslint-disable no-unused-vars */

/**
* @param {!Artifacts} artifacts
* @param {LH.Artifacts} artifacts
* @return {Promise<{failures: Array<string>, warnings?: Array<string>, manifestValues?: LH.Artifacts.ManifestValues}>}
*/
static audit_() {
static audit_(artifacts) {
throw new Error('audit_ unimplemented');
}

/* eslint-enable no-unused-vars */
}

module.exports = MultiCheckAudit;
7 changes: 3 additions & 4 deletions lighthouse-core/audits/redirects-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Audit = require('./audit');

class RedirectsHTTP extends Audit {
/**
* @return {!AuditMeta}
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
Expand All @@ -23,13 +23,12 @@ class RedirectsHTTP extends Audit {
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
* @param {LH.Artifacts} artifacts
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
return {
rawValue: artifacts.HTTPRedirect.value,
debugString: artifacts.HTTPRedirect.debugString,
};
}
}
Expand Down
16 changes: 11 additions & 5 deletions lighthouse-core/audits/screenshot-thumbnails.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ const jpeg = require('jpeg-js');
const NUMBER_OF_THUMBNAILS = 10;
const THUMBNAIL_WIDTH = 120;

/** @typedef {LH.Artifacts.Speedline['frames'][0]} SpeedlineFrame */

class ScreenshotThumbnails extends Audit {
/**
* @return {!AuditMeta}
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
Expand All @@ -30,8 +32,8 @@ class ScreenshotThumbnails extends Audit {
* Scales down an image to THUMBNAIL_WIDTH using nearest neighbor for speed, maintains aspect
* ratio of the original thumbnail.
*
* @param {{width: number, height: number, data: !Array<number>}} imageData
* @return {{width: number, height: number, data: !Array<number>}}
* @param {ReturnType<SpeedlineFrame['getParsedImage']>} imageData
* @return {{width: number, height: number, data: Uint8Array}}
*/
static scaleImageToThumbnail(imageData) {
const scaledWidth = THUMBNAIL_WIDTH;
Expand Down Expand Up @@ -63,11 +65,13 @@ class ScreenshotThumbnails extends Audit {
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts, context) {
const trace = artifacts.traces[Audit.DEFAULT_PASS];
/** @type {Map<SpeedlineFrame, string>} */
const cachedThumbnails = new Map();

const speedline = await artifacts.requestSpeedline(trace);
Expand Down Expand Up @@ -99,6 +103,8 @@ class ScreenshotThumbnails extends Audit {
for (let i = 1; i <= NUMBER_OF_THUMBNAILS; i++) {
const targetTimestamp = speedline.beginning + timelineEnd * i / NUMBER_OF_THUMBNAILS;

/** @type {SpeedlineFrame} */
// @ts-ignore - there will always be at least one frame by this point. TODO: use nonnullable assertion in TS2.9
let frameForTimestamp = null;
if (i === NUMBER_OF_THUMBNAILS) {
frameForTimestamp = analyzedFrames[analyzedFrames.length - 1];
Expand Down
6 changes: 3 additions & 3 deletions lighthouse-core/audits/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Audit = require('./audit');

class ServiceWorker extends Audit {
/**
* @return {!AuditMeta}
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
Expand All @@ -25,8 +25,8 @@ class ServiceWorker extends Audit {
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
* @param {LH.Artifacts} artifacts
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
// Find active service worker for this URL. Match against
Expand Down
6 changes: 3 additions & 3 deletions lighthouse-core/audits/speed-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Util = require('../report/html/renderer/util');

class SpeedIndex extends Audit {
/**
* @return {!AuditMeta}
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
Expand Down Expand Up @@ -39,9 +39,9 @@ class SpeedIndex extends Audit {
/**
* Audits the page to give a score for the Speed Index.
* @see https://github.com/GoogleChrome/lighthouse/issues/197
* @param {Artifacts} artifacts The artifacts from the gather phase.
* @param {LH.Artifacts} artifacts The artifacts from the gather phase.
* @param {LH.Audit.Context} context
* @return {Promise<AuditResult>}
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts, context) {
const trace = artifacts.traces[Audit.DEFAULT_PASS];
Expand Down
14 changes: 11 additions & 3 deletions lighthouse-core/audits/splash-screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const MultiCheckAudit = require('./multi-check-audit');

class SplashScreen extends MultiCheckAudit {
/**
* @return {!AuditMeta}
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
Expand All @@ -36,8 +36,12 @@ class SplashScreen extends MultiCheckAudit {
};
}

/**
* @param {LH.Artifacts.ManifestValues} manifestValues
* @param {Array<string>} failures
*/
static assessManifest(manifestValues, failures) {
if (manifestValues.isParseFailure) {
if (manifestValues.isParseFailure && manifestValues.parseFailureReason) {
failures.push(manifestValues.parseFailureReason);
return;
}
Expand All @@ -58,8 +62,12 @@ class SplashScreen extends MultiCheckAudit {
});
}


/**
* @param {LH.Artifacts} artifacts
* @return {Promise<{failures: Array<string>, manifestValues: LH.Artifacts.ManifestValues}>}
*/
static audit_(artifacts) {
/** @type {Array<string>} */
const failures = [];

return artifacts.requestManifestValues(artifacts.Manifest).then(manifestValues => {
Expand Down
19 changes: 16 additions & 3 deletions lighthouse-core/audits/themed-omnibox.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const validColor = require('../lib/web-inspector').Color.parse;

class ThemedOmnibox extends MultiCheckAudit {
/**
* @return {!AuditMeta}
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
Expand All @@ -33,6 +33,10 @@ class ThemedOmnibox extends MultiCheckAudit {
};
}

/**
* @param {LH.Artifacts['ThemeColor']} themeColorMeta
* @param {Array<string>} failures
*/
static assessMetaThemecolor(themeColorMeta, failures) {
if (themeColorMeta === null) {
failures.push('No `<meta name="theme-color">` tag found');
Expand All @@ -41,19 +45,28 @@ class ThemedOmnibox extends MultiCheckAudit {
}
}

/**
* @param {LH.Artifacts.ManifestValues} manifestValues
* @param {Array<string>} failures
*/
static assessManifest(manifestValues, failures) {
if (manifestValues.isParseFailure) {
if (manifestValues.isParseFailure && manifestValues.parseFailureReason) {
failures.push(manifestValues.parseFailureReason);
return;
}

const themeColorCheck = manifestValues.allChecks.find(i => i.id === 'hasThemeColor');
if (!themeColorCheck.passing) {
if (themeColorCheck && !themeColorCheck.passing) {
failures.push(themeColorCheck.failureText);
}
}

/**
* @param {LH.Artifacts} artifacts
* @return {Promise<{failures: Array<string>, manifestValues: LH.Artifacts.ManifestValues, themeColor: ?string}>}
*/
static audit_(artifacts) {
/** @type {Array<string>} */
const failures = [];

return artifacts.requestManifestValues(artifacts.Manifest).then(manifestValues => {
Expand Down
12 changes: 9 additions & 3 deletions lighthouse-core/audits/time-to-first-byte.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const TTFB_THRESHOLD = 600;

class TTFBMetric extends Audit {
/**
* @return {!AuditMeta}
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
Expand All @@ -25,15 +25,18 @@ class TTFBMetric extends Audit {
};
}

/**
* @param {LH.WebInspector.NetworkRequest} record
*/
static caclulateTTFB(record) {
const timing = record._timing;

return timing.receiveHeadersEnd - timing.sendEnd;
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
* @param {LH.Artifacts} artifacts
* @return {Promise<LH.Audit.Product>}
*/
static audit(artifacts) {
const devtoolsLogs = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
Expand All @@ -44,6 +47,9 @@ class TTFBMetric extends Audit {

const finalUrl = artifacts.URL.finalUrl;
const finalUrlRequest = networkRecords.find(record => record._url === finalUrl);
if (!finalUrlRequest) {
throw new Error(`finalUrl '${finalUrl} not found in network records.`);
}
const ttfb = TTFBMetric.caclulateTTFB(finalUrlRequest);
const passed = ttfb < TTFB_THRESHOLD;

Expand Down
6 changes: 3 additions & 3 deletions lighthouse-core/audits/viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Parser = require('metaviewport-parser');

class Viewport extends Audit {
/**
* @return {!AuditMeta}
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
Expand All @@ -25,8 +25,8 @@ class Viewport extends Audit {
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
* @param {LH.Artifacts} artifacts
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
if (artifacts.Viewport === null) {
Expand Down
24 changes: 21 additions & 3 deletions lighthouse-core/audits/webapp-install-banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const SWAudit = require('./service-worker');

class WebappInstallBanner extends MultiCheckAudit {
/**
* @return {!AuditMeta}
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
Expand All @@ -44,11 +44,16 @@ class WebappInstallBanner extends MultiCheckAudit {
};
}

/**
* @param {LH.Artifacts.ManifestValues} manifestValues
* @return {Array<string>}
*/
static assessManifest(manifestValues) {
if (manifestValues.isParseFailure) {
if (manifestValues.isParseFailure && manifestValues.parseFailureReason) {
return [manifestValues.parseFailureReason];
}

/** @type {Array<string>} */
const failures = [];
const bannerCheckIds = [
'hasName',
Expand All @@ -68,7 +73,10 @@ class WebappInstallBanner extends MultiCheckAudit {
return failures;
}


/**
* @param {LH.Artifacts} artifacts
* @return {Array<string>}
*/
static assessServiceWorker(artifacts) {
const failures = [];
const hasServiceWorker = SWAudit.audit(artifacts).rawValue;
Expand All @@ -79,6 +87,10 @@ class WebappInstallBanner extends MultiCheckAudit {
return failures;
}

/**
* @param {LH.Artifacts} artifacts
* @return {{failures: Array<string>, warnings: Array<string>}}
*/
static assessOfflineStartUrl(artifacts) {
const failures = [];
const warnings = [];
Expand All @@ -98,8 +110,14 @@ class WebappInstallBanner extends MultiCheckAudit {
return {failures, warnings};
}

/**
* @param {LH.Artifacts} artifacts
* @return {Promise<{failures: Array<string>, warnings: Array<string>, manifestValues: LH.Artifacts.ManifestValues}>}
*/
static audit_(artifacts) {
/** @type {Array<string>} */
let offlineFailures = [];
/** @type {Array<string>} */
let offlineWarnings = [];

return artifacts.requestManifestValues(artifacts.Manifest).then(manifestValues => {
Expand Down
6 changes: 3 additions & 3 deletions lighthouse-core/audits/without-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Audit = require('./audit');

class WithoutJavaScript extends Audit {
/**
* @return {!AuditMeta}
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
Expand All @@ -24,8 +24,8 @@ class WithoutJavaScript extends Audit {
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
* @param {LH.Artifacts} artifacts
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
const artifact = artifacts.HTMLWithoutJavaScript;
Expand Down
Loading

0 comments on commit ed14c8e

Please sign in to comment.