Skip to content

Commit

Permalink
core(tsc): use Config class to define Config type (#5525)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny authored Jun 20, 2018
1 parent 3f8b498 commit 6bf2f45
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
41 changes: 23 additions & 18 deletions lighthouse-core/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const Runner = require('../runner');
/** @typedef {InstanceType<GathererConstructor>} Gatherer */

/**
* @param {LH.Config['passes']} passes
* @param {LH.Config['audits']} audits
* @param {Config['passes']} passes
* @param {Config['audits']} audits
*/
function validatePasses(passes, audits) {
if (!Array.isArray(passes)) {
Expand Down Expand Up @@ -54,9 +54,9 @@ function validatePasses(passes, audits) {
}

/**
* @param {LH.Config['categories']} categories
* @param {LH.Config['audits']} audits
* @param {LH.Config['groups']} groups
* @param {Config['categories']} categories
* @param {Config['audits']} audits
* @param {Config['groups']} groups
*/
function validateCategories(categories, audits, groups) {
if (!categories) {
Expand Down Expand Up @@ -303,6 +303,7 @@ const mergeOptionsOfItems = (function(items) {
class Config {
/**
* @constructor
* @implements {LH.Config.Json}
* @param {LH.Config.Json=} configJSON
* @param {LH.Flags=} flags
*/
Expand Down Expand Up @@ -340,21 +341,25 @@ class Config {
Config.adjustDefaultPassForThrottling(settings, passesWithDefaults);
const passes = Config.requireGatherers(passesWithDefaults, configDir);

/** @type {LH.Config['settings']} */
/** @type {LH.Config.Settings} */
this.settings = settings;
/** @type {LH.Config['passes']} */
/** @type {?Array<LH.Config.Pass>} */
this.passes = passes;
/** @type {LH.Config['audits']} */
/** @type {?Array<LH.Config.AuditDefn>} */
this.audits = Config.requireAudits(configJSON.audits, configDir);
/** @type {LH.Config['categories']} */
/** @type {?Record<string, LH.Config.Category>} */
this.categories = configJSON.categories || null;
/** @type {LH.Config['groups']} */
/** @type {?Record<string, LH.Config.Group>} */
this.groups = configJSON.groups || null;

Config.filterConfigIfNeeded(this);

validatePasses(this.passes, this.audits);
validateCategories(this.categories, this.audits, this.groups);

// TODO(bckenny): until tsc adds @implements support, assert that Config is a ConfigJson.
/** @type {LH.Config.Json} */
const configJson = this; // eslint-disable-line no-unused-vars
}

/**
Expand Down Expand Up @@ -535,9 +540,9 @@ class Config {

/**
* Filter out any unrequested categories or audits from the categories object.
* @param {LH.Config['categories']} oldCategories
* @param {Config['categories']} oldCategories
* @param {LH.Config.Settings} settings
* @return {{categories: LH.Config['categories'], requestedAuditNames: Set<string>}}
* @return {{categories: Config['categories'], requestedAuditNames: Set<string>}}
*/
static filterCategoriesAndAudits(oldCategories, settings) {
if (!oldCategories) {
Expand All @@ -548,7 +553,7 @@ class Config {
throw new Error('Cannot set both skipAudits and onlyAudits');
}

/** @type {NonNullable<LH.Config['categories']>} */
/** @type {NonNullable<Config['categories']>} */
const categories = {};
const filterByIncludedCategory = !!settings.onlyCategories;
const filterByIncludedAudit = !!settings.onlyAudits;
Expand Down Expand Up @@ -630,7 +635,7 @@ class Config {

/**
* From some requested audits, return names of all required artifacts
* @param {LH.Config['audits']} audits
* @param {Config['audits']} audits
* @return {Set<string>}
*/
static getGatherersNeededByAudits(audits) {
Expand All @@ -648,9 +653,9 @@ class Config {

/**
* Filters to only required passes and gatherers, returning a new passes array.
* @param {LH.Config['passes']} passes
* @param {Config['passes']} passes
* @param {Set<string>} requiredGatherers
* @return {LH.Config['passes']}
* @return {Config['passes']}
*/
static generatePassesNeededByGatherers(passes, requiredGatherers) {
if (!passes) {
Expand Down Expand Up @@ -689,7 +694,7 @@ class Config {
* leaving only an array of AuditDefns.
* @param {LH.Config.Json['audits']} audits
* @param {string=} configPath
* @return {LH.Config['audits']}
* @return {Config['audits']}
*/
static requireAudits(audits, configPath) {
const expandedAudits = Config.expandAuditShorthand(audits);
Expand Down Expand Up @@ -758,7 +763,7 @@ class Config {
* provided) using `Runner.resolvePlugin`, returning an array of full Passes.
* @param {?Array<Required<LH.Config.PassJson>>} passes
* @param {string=} configPath
* @return {LH.Config['passes']}
* @return {Config['passes']}
*/
static requireGatherers(passes, configPath) {
if (!passes) {
Expand Down
7 changes: 4 additions & 3 deletions lighthouse-core/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ const URL = require('./lib/url-shim');
const Sentry = require('./lib/sentry');
const generateReport = require('./report/report-generator').generateReport;

const Connection = require('./gather/connections/connection.js'); // eslint-disable-line no-unused-vars
/** @typedef {import('./gather/connections/connection.js')} Connection */
/** @typedef {import('./config/config.js')} Config */

class Runner {
/**
* @param {Connection} connection
* @param {{config: LH.Config, url?: string, driverMock?: Driver}} opts
* @param {{config: Config, url?: string, driverMock?: Driver}} opts
* @return {Promise<LH.RunnerResult|undefined>}
*/
static async run(connection, opts) {
Expand Down Expand Up @@ -145,7 +146,7 @@ class Runner {
/**
* Establish connection, load page and collect all required artifacts
* @param {string} requestedUrl
* @param {{config: LH.Config, driverMock?: Driver}} runnerOpts
* @param {{config: Config, driverMock?: Driver}} runnerOpts
* @param {Connection} connection
* @return {Promise<LH.Artifacts>}
*/
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"checkJs": true,
"strict": true,
// "listFiles": true,
// "noErrorTruncation": true,
"typeRoots": [
"@types",
"./typings"
Expand Down
11 changes: 0 additions & 11 deletions typings/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ import Audit = require('../lighthouse-core/audits/audit.js');

declare global {
module LH {
/**
* The full, normalized Lighthouse Config.
*/
export interface Config extends Config.Json {
settings: Config.Settings;
passes: Config.Pass[] | null;
audits: Config.AuditDefn[] | null;
categories: Record<string, Config.Category> | null;
groups: Record<string, Config.Group> | null;
}

module Config {
/**
* The pre-normalization Lighthouse Config format.
Expand Down

0 comments on commit 6bf2f45

Please sign in to comment.