Skip to content

Commit 6d6af51

Browse files
patrickhulcebrendankenny
authored andcommitted
core(config): clean flags for config settings (#4960)
1 parent 48120d0 commit 6d6af51

File tree

6 files changed

+55
-16
lines changed

6 files changed

+55
-16
lines changed

lighthouse-core/config/config.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,27 @@ function expandArtifacts(artifacts) {
246246
return artifacts;
247247
}
248248

249+
/**
250+
* Creates a settings object from potential flags object by dropping all the properties
251+
* that don't exist on Config.Settings.
252+
*
253+
* @param {LH.Flags=} flags
254+
* @return {Partial<LH.Config.Settings>}
255+
*/
256+
function cleanFlagsForSettings(flags = {}) {
257+
const settings = {};
258+
for (const key of Object.keys(flags)) {
259+
if (typeof constants.defaultSettings[key] !== 'undefined') {
260+
settings[key] = flags[key];
261+
}
262+
}
263+
264+
return settings;
265+
}
266+
249267
function merge(base, extension) {
250-
if (typeof base === 'undefined') {
268+
// If the default value doesn't exist or is explicitly null, defer to the extending value
269+
if (typeof base === 'undefined' || base === null) {
251270
return extension;
252271
} else if (Array.isArray(extension)) {
253272
if (!Array.isArray(base)) throw new TypeError(`Expected array but got ${typeof base}`);
@@ -330,7 +349,7 @@ class Config {
330349
configJSON.passes = Config.expandGathererShorthandAndMergeOptions(configJSON.passes);
331350

332351
// Override any applicable settings with CLI flags
333-
configJSON.settings = merge(configJSON.settings || {}, flags || {});
352+
configJSON.settings = merge(configJSON.settings || {}, cleanFlagsForSettings(flags));
334353

335354
// Generate a limited config if specified
336355
if (Array.isArray(configJSON.settings.onlyCategories) ||

lighthouse-core/config/constants.js

+14
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,24 @@ const throttling = {
2626
},
2727
};
2828

29+
/** @type {LH.Config.Settings} */
2930
const defaultSettings = {
3031
maxWaitForLoad: 45 * 1000,
3132
throttlingMethod: 'devtools',
3233
throttling: throttling.mobile3G,
34+
auditMode: false,
35+
gatherMode: false,
36+
disableStorageReset: false,
37+
disableDeviceEmulation: false,
38+
39+
// the following settings have no defaults but we still want ensure that `key in settings`
40+
// in config will work in a typechecked way
41+
blockedUrlPatterns: null,
42+
additionalTraceCategories: null,
43+
extraHeaders: null,
44+
onlyAudits: null,
45+
onlyCategories: null,
46+
skipAudits: null,
3347
};
3448

3549
const defaultPassConfig = {

lighthouse-core/gather/driver.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ class Driver {
828828
}
829829

830830
/**
831-
* @param {{additionalTraceCategories?: string}=} settings
831+
* @param {{additionalTraceCategories?: string|null}=} settings
832832
* @return {Promise<void>}
833833
*/
834834
beginTrace(settings) {
@@ -1019,7 +1019,7 @@ class Driver {
10191019
}
10201020

10211021
/**
1022-
* @param {LH.Crdp.Network.Headers=} headers key/value pairs of HTTP Headers.
1022+
* @param {LH.Crdp.Network.Headers|null} headers key/value pairs of HTTP Headers.
10231023
* @return {Promise<void>}
10241024
*/
10251025
async setExtraHTTPHeaders(headers) {

lighthouse-core/test/config/config-test.js

+6
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,12 @@ describe('Config', () => {
418418
});
419419
});
420420

421+
it('cleans up flags for settings', () => {
422+
const config = new Config({extends: true}, {nonsense: 1, foo: 2, throttlingMethod: 'provided'});
423+
assert.equal(config.settings.throttlingMethod, 'provided');
424+
assert.ok(config.settings.nonsense === undefined, 'did not cleanup settings');
425+
});
426+
421427
it('extends the full config', () => {
422428
class CustomAudit extends Audit {
423429
static get meta() {

typings/config.d.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ declare global {
2424
settings?: SettingsJson;
2525
passes?: PassJson[];
2626
}
27-
27+
2828
export interface SettingsJson extends SharedFlagsSettings {
29-
extraHeaders?: Crdp.Network.Headers;
29+
extraHeaders?: Crdp.Network.Headers | null;
3030
}
31-
31+
3232
export interface PassJson {
3333
passName: string;
3434
recordTrace?: boolean;
@@ -41,7 +41,7 @@ declare global {
4141
blankDuration?: number;
4242
gatherers: GathererJson[];
4343
}
44-
44+
4545
export type GathererJson = {
4646
path: string;
4747
options?: {};
@@ -52,14 +52,14 @@ declare global {
5252
instance: InstanceType<typeof Gatherer>;
5353
options?: {};
5454
} | string;
55-
55+
5656
// TODO(bckenny): we likely don't want to require all these
5757
export type Settings = Required<SettingsJson>;
58-
58+
5959
export interface Pass extends Required<PassJson> {
6060
gatherers: GathererDefn[];
6161
}
62-
62+
6363
export interface GathererDefn {
6464
implementation: typeof Gatherer;
6565
instance: InstanceType<typeof Gatherer>;

typings/externs.d.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ declare global {
3535

3636
interface SharedFlagsSettings {
3737
maxWaitForLoad?: number;
38-
blockedUrlPatterns?: string[];
39-
additionalTraceCategories?: string;
38+
blockedUrlPatterns?: string[] | null;
39+
additionalTraceCategories?: string | null;
4040
auditMode?: boolean | string;
4141
gatherMode?: boolean | string;
4242
disableStorageReset?: boolean;
4343
disableDeviceEmulation?: boolean;
4444
throttlingMethod?: 'devtools'|'simulate'|'provided';
4545
throttling?: ThrottlingSettings;
46-
onlyAudits?: string[];
47-
onlyCategories?: string[];
48-
skipAudits?: string[];
46+
onlyAudits?: string[] | null;
47+
onlyCategories?: string[] | null;
48+
skipAudits?: string[] | null;
4949
}
5050

5151
export interface Flags extends SharedFlagsSettings {

0 commit comments

Comments
 (0)