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

convert DoBetterWeb gatherers to new error system #1641

Merged
merged 7 commits into from
Feb 6, 2017
7 changes: 0 additions & 7 deletions lighthouse-core/audits/dobetterweb/appcache-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ class AppCacheManifestAttr extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.AppCacheManifest === -1) {
return AppCacheManifestAttr.generateAuditResult({
rawValue: false,
debugString: 'Unable to determine if you\'re using AppCache.'
});
}

const usingAppcache = artifacts.AppCacheManifest !== null;
const debugString = usingAppcache ?
`Found <html manifest="${artifacts.AppCacheManifest}">.` : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,13 @@ class ExternalAnchorsUseRelNoopenerAudit extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.AnchorsWithNoRelNoopener === -1) {
return ExternalAnchorsUseRelNoopenerAudit.generateAuditResult({
rawValue: -1,
debugString: 'Unknown error with the AnchorsWithNoRelNoopener gatherer.'
});
}

let debugString;
const pageHost = new URL(artifacts.URL.finalUrl).host;
// Filter usages to exclude anchors that are same origin
// TODO: better extendedInfo for anchors with no href attribute:
// https://github.com/GoogleChrome/lighthouse/issues/1233
// https://github.com/GoogleChrome/lighthouse/issues/1345
const failingAnchors = artifacts.AnchorsWithNoRelNoopener.usages
const failingAnchors = artifacts.AnchorsWithNoRelNoopener
.filter(anchor => {
try {
return anchor.href === '' || new URL(anchor.href).host !== pageHost;
Expand Down
14 changes: 1 addition & 13 deletions lighthouse-core/audits/dobetterweb/geolocation-on-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,7 @@ class GeolocationOnStart extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.GeolocationOnStart.value === -1) {
let debugString = 'Unknown error with the GeolocationOnStart gatherer';
if (artifacts.GeolocationOnStart.debugString) {
debugString = artifacts.GeolocationOnStart.debugString;
}

return GeolocationOnStart.generateAuditResult({
rawValue: -1,
debugString
});
}

const results = artifacts.GeolocationOnStart.usage.map(err => {
const results = artifacts.GeolocationOnStart.map(err => {
return Object.assign({
label: `line: ${err.line}, col: ${err.col}`
}, err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ class LinkBlockingFirstPaintAudit extends Audit {
*/
static computeAuditResultForTags(artifacts, tagFilter) {
const artifact = artifacts.TagsBlockingFirstPaint;
if (artifact.value === -1) {
return {
rawValue: -1,
debugString: artifact.debugString
};
}

const filtered = artifact.filter(item => item.tag.tagName === tagFilter);

Expand Down
14 changes: 1 addition & 13 deletions lighthouse-core/audits/dobetterweb/no-console-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,9 @@ class NoConsoleTimeAudit extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.ConsoleTimeUsage.value === -1) {
let debugString = 'Unknown error with the ConsoleTimeUsage gatherer';
if (artifacts.ConsoleTimeUsage.debugString) {
debugString = artifacts.ConsoleTimeUsage.debugString;
}

return NoConsoleTimeAudit.generateAuditResult({
rawValue: -1,
debugString
});
}

let debugString;
// Filter usage from other hosts and keep eval'd code.
const results = artifacts.ConsoleTimeUsage.usage.filter(err => {
const results = artifacts.ConsoleTimeUsage.filter(err => {
if (err.isEval) {
return !!err.url;
}
Expand Down
14 changes: 1 addition & 13 deletions lighthouse-core/audits/dobetterweb/no-datenow.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,9 @@ class NoDateNowAudit extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.DateNowUse.value === -1) {
let debugString = 'Unknown error with the DateNowUse gatherer';
if (artifacts.DateNowUse.debugString) {
debugString = artifacts.DateNowUse.debugString;
}

return NoDateNowAudit.generateAuditResult({
rawValue: -1,
debugString
});
}

let debugString;
// Filter usage from other hosts and keep eval'd code.
const results = artifacts.DateNowUse.usage.filter(err => {
const results = artifacts.DateNowUse.filter(err => {
if (err.isEval) {
return !!err.url;
}
Expand Down
14 changes: 1 addition & 13 deletions lighthouse-core/audits/dobetterweb/no-document-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,7 @@ class NoDocWriteAudit extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.DocWriteUse.value === -1) {
let debugString = 'Unknown error with the DocWriteUse gatherer';
if (artifacts.DocWriteUse.debugString) {
debugString = artifacts.DocWriteUse.debugString;
}

return NoDocWriteAudit.generateAuditResult({
rawValue: -1,
debugString
});
}

const results = artifacts.DocWriteUse.usage.map(err => {
const results = artifacts.DocWriteUse.map(err => {
return Object.assign({
label: `line: ${err.line}, col: ${err.col}`
}, err);
Expand Down
4 changes: 0 additions & 4 deletions lighthouse-core/audits/dobetterweb/no-mutation-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ class NoMutationEventsAudit extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.EventListeners.rawValue === -1) {
return NoMutationEventsAudit.generateAuditResult(artifacts.EventListeners);
}

let debugString;
const listeners = artifacts.EventListeners;

Expand Down
13 changes: 3 additions & 10 deletions lighthouse-core/audits/dobetterweb/no-websql.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,9 @@ class NoWebSQLAudit extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.WebSQL.database === -1) {
return NoWebSQLAudit.generateAuditResult({
rawValue: -1,
debugString: artifacts.WebSQL.debugString
});
}

const db = artifacts.WebSQL.database;
const debugString = (db && db.database ?
`Found database "${db.database.name}", version: ${db.database.version}.` : '');
const db = artifacts.WebSQL;
const debugString = (db ?
`Found database "${db.name}", version: ${db.version}.` : '');

return NoWebSQLAudit.generateAuditResult({
rawValue: !db,
Expand Down
14 changes: 1 addition & 13 deletions lighthouse-core/audits/dobetterweb/notification-on-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,7 @@ class NotificationOnStart extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.NotificationOnStart.value === -1) {
let debugString = 'Unknown error with the NotificationOnStart gatherer';
if (artifacts.NotificationOnStart.debugString) {
debugString = artifacts.NotificationOnStart.debugString;
}

return NotificationOnStart.generateAuditResult({
rawValue: -1,
debugString
});
}

const results = artifacts.NotificationOnStart.usage.map(err => {
const results = artifacts.NotificationOnStart.map(err => {
return Object.assign({
label: `line: ${err.line}, col: ${err.col}`
}, err);
Expand Down
4 changes: 0 additions & 4 deletions lighthouse-core/audits/dobetterweb/uses-optimized-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ class UsesOptimizedImages extends Audit {
static audit_(artifacts, networkThroughput) {
const images = artifacts.OptimizedImages;

if (images.rawValue === -1) {
return UsesOptimizedImages.generateAuditResult(images);
}

const failedImages = [];
let totalWastedBytes = 0;
let hasAllEfficientImages = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ class PassiveEventsAudit extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.EventListeners.rawValue === -1) {
return PassiveEventsAudit.generateAuditResult(artifacts.EventListeners);
}

let debugString;
const listeners = artifacts.EventListeners;

Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ class Driver {

/**
* @param {string} selector Selector to find in the DOM
* @return {!Promise<Element[]>} The found elements, or [], resolved in a promise
* @return {!Promise<!Array<!Element>>} The found elements, or [], resolved in a promise
*/
querySelectorAll(selector) {
return this.sendCommand('DOM.getDocument')
Expand Down
27 changes: 13 additions & 14 deletions lighthouse-core/gather/gatherers/dobetterweb/all-event-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class EventListeners extends Gatherer {
/**
* @param {number|string} nodeIdOrObject The node id of the element or the
* string of and object ('document', 'window').
* @return {!Promise<!Array.<EventListener>>}
* @return {!Promise<!Array<{listeners: !Array, tagName: string}>>}
* @private
*/
_listEventListeners(nodeIdOrObject) {
Expand Down Expand Up @@ -74,7 +74,7 @@ class EventListeners extends Gatherer {
* listenForScriptParsedEvents should be called before this method to ensure
* the page's parsed scripts are collected at page load.
* @param {string} nodeId The node to look for attached event listeners.
* @return {!Promise<!Array.<Object>>} List of event listeners attached to
* @return {!Promise<!Array<!Object>>} List of event listeners attached to
* the node.
*/
getEventListeners(nodeId) {
Expand Down Expand Up @@ -107,8 +107,8 @@ class EventListeners extends Gatherer {

/**
* Aggregates the event listeners used on each element into a single list.
* @param {Array.<Element>} nodes List of elements to fetch event listeners for.
* @return {!Promise<!Array.<Object>>} Resolves to a list of all the event
* @param {!Array<!Element>} nodes List of elements to fetch event listeners for.
* @return {!Promise<!Array<!Object>>} Resolves to a list of all the event
* listeners found across the elements.
*/
collectListeners(nodes) {
Expand All @@ -127,18 +127,17 @@ class EventListeners extends Gatherer {
return this.listenForScriptParsedEvents();
}

/**
* @param {!Object} options
* @return {!Promise<!Array<!Object>>}
*/
afterPass(options) {
return this.unlistenForScriptParsedEvents()
.then(_ => options.driver.querySelectorAll('body, body /deep/ *')) // drill into shadow trees
.then(nodes => {
nodes.push('document', 'window');
return this.collectListeners(nodes);
}).catch(_ => {
return {
rawValue: -1,
debugString: 'Unable to collect passive events listener usage.'
};
});
.then(_ => options.driver.querySelectorAll('body, body /deep/ *')) // drill into shadow trees
.then(nodes => {
nodes.push('document', 'window');
return this.collectListeners(nodes);
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
const Gatherer = require('../gatherer');

class AnchorsWithNoRelNoopener extends Gatherer {

/**
* @param {!Object} options
* @return {!Promise<!Array<{href: string, rel: string, target: string}>>}
*/
afterPass(options) {
const driver = options.driver;
return driver.querySelectorAll('a[target="_blank"]:not([rel~="noopener"])')
Expand All @@ -34,18 +37,13 @@ class AnchorsWithNoRelNoopener extends Gatherer {
return Promise.all(failingNodes);
})
.then(failingNodes => {
return {
usages: failingNodes.map(node => {
return {
href: node[0],
rel: node[1],
target: node[2]
};
})
};
})
.catch(_ => {
return -1;
return failingNodes.map(node => {
return {
href: node[0],
rel: node[1],
target: node[2]
};
});
});
}
}
Expand Down
12 changes: 7 additions & 5 deletions lighthouse-core/gather/gatherers/dobetterweb/appcache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
const Gatherer = require('../gatherer');

class AppCacheManifest extends Gatherer {

/**
* Retrurns the value of the html element's manifest attribute or null if it
* is not defined.
* @param {!Object} options
* @return {!Promise<?string>}
*/
afterPass(options) {
const driver = options.driver;

return driver.querySelector('html')
.then(node => node && node.getAttribute('manifest'))
.catch(_ => {
return -1;
});
.then(node => node && node.getAttribute('manifest'));
}
}

Expand Down
14 changes: 4 additions & 10 deletions lighthouse-core/gather/gatherers/dobetterweb/console-time-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,11 @@ class ConsoleTimeUsage extends Gatherer {
this.collectUsage = options.driver.captureFunctionCallSites('console.time');
}

/**
* @return {!Promise<!Array<{url: string, line: number, col: number}>>}
*/
afterPass() {
return this.collectUsage().then(consoleTimeUsage => {
return {
usage: consoleTimeUsage
};
}, e => {
return {
value: -1,
debugString: e.message
};
});
return this.collectUsage();
}
}

Expand Down
14 changes: 4 additions & 10 deletions lighthouse-core/gather/gatherers/dobetterweb/datenow.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,11 @@ class DateNowUse extends Gatherer {
this.collectUsage = options.driver.captureFunctionCallSites('Date.now');
}

/**
* @return {!Promise<!Array<{url: string, line: number, col: number}>>}
*/
afterPass() {
return this.collectUsage().then(dateNowUses => {
return {
usage: dateNowUses
};
}, e => {
return {
value: -1,
debugString: e.message
};
});
return this.collectUsage();
}
}

Expand Down
Loading