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

Fixes #1282 #1288

Merged
merged 3 commits into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ <h2>Do better web tester page</h2>
return Date.now();
}
helloDate();
const d = Date.now(); // FAIL
eval('Date.now()'); // FAIL
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given our fail/no fail smoke tests right now, what benefit does this add? (seems like we'd be unable to catch eval not working here too actually)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not much, just wanted to check that Date.now() outside of an inner function was also producing results. Until smokehouse audits number of violations, this won't do much other than a manual verify.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright just checking, sg

new Function('Date.now()')() // FAIL
}
Expand Down
20 changes: 18 additions & 2 deletions lighthouse-core/audits/dobetterweb/no-console-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,25 @@ class NoConsoleTimeAudit extends Audit {
});
}

let debugString;

const pageHost = new URL(artifacts.URL.finalUrl).host;
// Filter usage from other hosts and keep eval'd code.
const results = artifacts.ConsoleTimeUsage.usage.filter(err => {
return err.isEval ? !!err.url : new URL(err.url).host === pageHost;
if (err.isEval) {
return !!err.url;
}

// If the violation doesn't have a valid url, don't filter it out, but
// warn the user that we don't know what the callsite is.
try {
return new URL(err.url).host === pageHost;
} catch (e) {
debugString = 'Lighthouse was unable to determine if some API uses ' +
'were made by this page. It\'s possible a Chrome extension' +
'content script or other eval\'d code is calling this API.';
return true;
}
}).map(err => {
return Object.assign({
label: `line: ${err.line}, col: ${err.col}`
Expand All @@ -76,7 +91,8 @@ class NoConsoleTimeAudit extends Audit {
extendedInfo: {
formatter: Formatter.SUPPORTED_FORMATS.URLLIST,
value: results
}
},
debugString
});
}
}
Expand Down
21 changes: 15 additions & 6 deletions lighthouse-core/audits/dobetterweb/no-datenow.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,25 @@ class NoDateNowAudit extends Audit {
});
}

let debugString;

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 => {
if (err.isEval) {
return !!err.url;
}
return err.url ? new URL(err.url).host === pageHost : true;

// If the violation doesn't have a valid url, don't filter it out, but
// warn the user that we don't know what the callsite is.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one could argue this is ripe for a helper refactor, but we only have 2 atm. Don't think that warrants it just yet.

try {
return new URL(err.url).host === pageHost;
} catch (e) {
debugString = 'Lighthouse was unable to determine if some API uses ' +
'were made by this page. It\'s possible a Chrome extension' +
'content script or other eval\'d code is calling this API.';
return true;
}
}).map(err => {
return Object.assign({
label: `line: ${err.line}, col: ${err.col}`,
Expand All @@ -84,7 +92,8 @@ class NoDateNowAudit extends Audit {
extendedInfo: {
formatter: Formatter.SUPPORTED_FORMATS.URLLIST,
value: results
}
},
debugString
});
}
}
Expand Down
25 changes: 19 additions & 6 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,8 @@ class Driver {
'Driver failure: Expected evaluateAsync results to be an array ' +
`but got "${JSON.stringify(result)}" instead.`);
}
return result;
// Filter out usage from extension content scripts.
return result.filter(item => !item.isExtension);
});
};

Expand Down Expand Up @@ -738,24 +739,36 @@ function captureJSCallUsage(funcRef, set) {
// First frame is the function we injected (the one that just threw).
// Second, is the actual callsite of the funcRef we're after.
const callFrame = structStackTrace[1];
let url = callFrame.getFileName();
let url = callFrame.getFileName() || callFrame.getEvalOrigin();
const line = callFrame.getLineNumber();
const col = callFrame.getColumnNumber();
const isEval = callFrame.isEval();
let isExtension = false;
const stackTrace = structStackTrace.slice(1).map(callsite => callsite.toString());

// If we don't have an URL, (e.g. eval'd code), use the last entry in the
// stack trace to give some context: eval(<context>):<line>:<col>
// If we don't have an URL, (e.g. eval'd code), use the 2nd entry in the
// stack trace. First is eval context: eval(<context>):<line>:<col>.
// Second is the callsite where eval was called.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice I like this improvement

// See https://crbug.com/646849.
if (!url) {
if (isEval) {
url = stackTrace[1];
}

// Chrome extension content scripts can produce an empty .url and
// "<anonymous>:line:col" for the first entry in the stack trace.
if (stackTrace[0].startsWith('<anonymous>')) {
// Note: Although captureFunctionCallSites filters out crx usage,
// filling url here provides context. We may want to keep those results
// some day.
url = stackTrace[0];
isExtension = true;
}

// TODO: add back when we want stack traces.
// Stack traces were removed from the return object in
// https://github.com/GoogleChrome/lighthouse/issues/957 so callsites
// would be unique.
return {url, args, line, col, isEval}; // return value is e.stack
return {url, args, line, col, isEval, isExtension}; // return value is e.stack
};
const e = new __nativeError(`__called ${funcRef.name}__`);
set.add(JSON.stringify(e.stack));
Expand Down
16 changes: 16 additions & 0 deletions lighthouse-core/test/audits/dobetterweb/no-console-time-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,20 @@ describe('Page does not use console.time()', () => {
assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.extendedInfo.value.length, 3);
});

it('includes results when there is no .url', () => {
const auditResult = NoConsoleTimeAudit.audit({
ConsoleTimeUsage: {
usage: [
{line: 10, col: 1},
{line: 2, col: 22}
]
},
URL: {finalUrl: URL},
});

assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.extendedInfo.value.length, 2);
assert.ok(auditResult.debugString, 'includes debugString');
});
});
1 change: 1 addition & 0 deletions lighthouse-core/test/audits/dobetterweb/no-datenow-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,6 @@ describe('Page does not use Date.now()', () => {

assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.extendedInfo.value.length, 2);
assert.ok(auditResult.debugString, 'includes debugString');
});
});