Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: subRow refactor, rename to subItem #10867

Merged
merged 17 commits into from
Jun 8, 2020
Next Next commit
proposal
  • Loading branch information
brendankenny committed Apr 22, 2020
commit 536a3feba2613676a7bc8b61805161d19579d6d9
71 changes: 33 additions & 38 deletions lighthouse-core/audits/byte-efficiency/duplicated-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,12 @@ class DuplicatedJavascript extends ByteEfficiencyAudit {
const duplication =
await DuplicatedJavascript._getDuplicationGroupedByNodeModules(artifacts, context);

/**
* @typedef ItemSubrows
* @property {string[]} urls
* @property {number[]} sourceBytes
*/

/**
* @typedef {LH.Audit.ByteEfficiencyItem & ItemSubrows} Item
*/

/** @type {Item[]} */
/** @type {LH.Audit.ByteEfficiencyItem[]} */
const items = [];

let overflowWastedBytes = 0;
const overflowUrls = new Set();

/** @type {Map<string, number>} */
const wastedBytesByUrl = new Map();
for (const [source, sourceDatas] of duplication.entries()) {
Expand All @@ -139,52 +132,54 @@ class DuplicatedJavascript extends ByteEfficiencyAudit {
// is not present. Instead, size is used as a heuristic for latest version. This makes the
// audit conserative in its estimation.

const urls = [];
const bytesValues = [];
const subRowItems = [];

let wastedBytesTotal = 0;
for (let i = 0; i < sourceDatas.length; i++) {
const sourceData = sourceDatas[i];
const url = sourceData.scriptUrl;
urls.push(url);
bytesValues.push(sourceData.size);
subRowItems.push({
url,
sourceBytes: sourceData.size,
});
if (i === 0) continue;
wastedBytesTotal += sourceData.size;
wastedBytesByUrl.set(url, (wastedBytesByUrl.get(url) || 0) + sourceData.size);
}

if (wastedBytesTotal <= ignoreThresholdInBytes) {
overflowWastedBytes += wastedBytesTotal;
for (const subRowItem of subRowItems) {
overflowUrls.add(subRowItem.url);
}
continue;
}

items.push({
source,
wastedBytes: wastedBytesTotal,
// Not needed, but keeps typescript happy.
url: '',
// Not needed, but keeps typescript happy.
totalBytes: 0,
urls,
sourceBytes: bytesValues,
subRows: {
type: 'subrows',
items: subRowItems,
},
});
}

/** @type {Item} */
const otherItem = {
source: 'Other',
wastedBytes: 0,
url: '',
totalBytes: 0,
urls: [],
sourceBytes: [],
};
for (const item of items.filter(item => item.wastedBytes <= ignoreThresholdInBytes)) {
otherItem.wastedBytes += item.wastedBytes;
for (let i = 0; i < item.urls.length; i++) {
const url = item.urls[i];
if (!otherItem.urls.includes(url)) {
otherItem.urls.push(url);
}
}
items.splice(items.indexOf(item), 1);
}
if (otherItem.wastedBytes > ignoreThresholdInBytes) {
items.push(otherItem);
if (overflowWastedBytes > ignoreThresholdInBytes) {
items.push({
source: 'Other',
wastedBytes: overflowWastedBytes,
url: '',
totalBytes: 0,
subRows: {
type: 'subrows',
items: Array.from(overflowUrls).map(url => ({url})),
},
});
}

// Convert bytes to transfer size estimation.
Expand Down
22 changes: 12 additions & 10 deletions lighthouse-core/audits/legacy-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class LegacyJavascript extends Audit {
devtoolsLog,
}, context);

/** @type {Array<{url: string, signals: string[], locations: LH.Audit.Details.SourceLocationValue[]}>} */
/** @type {Array<{url: string, subRows: LH.Audit.Details.TableSubRows}>} */
const tableRows = [];
let signalCount = 0;

Expand All @@ -327,20 +327,22 @@ class LegacyJavascript extends Audit {
this.detectCodePatternsAcrossScripts(matcher, artifacts.ScriptElements, networkRecords);
urlToMatchResults.forEach((matches, url) => {
/** @type {typeof tableRows[number]} */
const row = {url, signals: [], locations: []};
const row = {url, subRows: {type: 'subrows', items: []}};
for (const match of matches) {
const {name, line, column} = match;
row.signals.push(name);
row.locations.push({
type: 'source-location',
url,
line,
column,
urlProvider: 'network',
row.subRows.items.push({
signal: name,
location: {
type: 'source-location',
url,
line,
column,
urlProvider: 'network',
},
});
}
tableRows.push(row);
signalCount += row.signals.length;
signalCount += row.subRows.items.length;
});

/** @type {LH.Audit.Details.Table['headings']} */
Expand Down
Loading