From 2299f944c396da6ad0fb75fd628dfd0ad808a3dc Mon Sep 17 00:00:00 2001 From: Eric Bidelman Date: Fri, 23 Dec 2016 10:45:29 -0800 Subject: [PATCH] handle Date.now uses with call site URL (#1277) --- lighthouse-core/audits/dobetterweb/no-datenow.js | 9 ++++++++- .../test/audits/dobetterweb/no-datenow-test.js | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lighthouse-core/audits/dobetterweb/no-datenow.js b/lighthouse-core/audits/dobetterweb/no-datenow.js index 0151f1a3c49a..de27b37d8646 100644 --- a/lighthouse-core/audits/dobetterweb/no-datenow.js +++ b/lighthouse-core/audits/dobetterweb/no-datenow.js @@ -63,8 +63,15 @@ class NoDateNowAudit extends Audit { const pageHost = new URL(artifacts.URL.finalUrl).host; // Filter usage from other hosts and keep eval'd code. + // If there is no .url in the violation, include it in the results because + // we cannot determine if it was from the user's page or a third party. + // TODO: better extendedInfo for violations with no URL. + // https://github.com/GoogleChrome/lighthouse/issues/1263 const results = artifacts.DateNowUse.usage.filter(err => { - return err.isEval ? !!err.url : new URL(err.url).host === pageHost; + if (err.isEval) { + return !!err.url; + } + return err.url ? new URL(err.url).host === pageHost : true; }).map(err => { return Object.assign({ label: `line: ${err.line}, col: ${err.col}`, diff --git a/lighthouse-core/test/audits/dobetterweb/no-datenow-test.js b/lighthouse-core/test/audits/dobetterweb/no-datenow-test.js index 54a5e5f94f3c..328cdfaf407e 100644 --- a/lighthouse-core/test/audits/dobetterweb/no-datenow-test.js +++ b/lighthouse-core/test/audits/dobetterweb/no-datenow-test.js @@ -99,4 +99,19 @@ describe('Page does not use Date.now()', () => { assert.equal(auditResult.rawValue, false); assert.equal(auditResult.extendedInfo.value.length, 3); }); + + it('includes results when there is no .url', () => { + const auditResult = DateNowUseAudit.audit({ + DateNowUse: { + usage: [ + {line: 10, col: 1}, + {line: 2, col: 22} + ] + }, + URL: {finalUrl: URL}, + }); + + assert.equal(auditResult.rawValue, false); + assert.equal(auditResult.extendedInfo.value.length, 2); + }); });