Skip to content

Commit

Permalink
New audit: using deprecated APIs (#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebidel authored and brendankenny committed Jan 19, 2017
1 parent 56c9bd3 commit 3c8f57c
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 3 deletions.
33 changes: 32 additions & 1 deletion lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.js
Expand Up @@ -40,4 +40,10 @@ if (location.search === '' || params.has('passiveEvents')) {
});
}

if (location.search === '' || params.has('deprecations')) {
const div = document.createElement('div');
div.createShadowRoot();
div.createShadowRoot(); // FAIL - multiple shadow v0 roots.
}

})();
12 changes: 10 additions & 2 deletions lighthouse-cli/test/smokehouse/dobetterweb/dbw-expectations.js
Expand Up @@ -31,7 +31,7 @@ module.exports = [
score: false,
extendedInfo: {
value: {
length: 10
length: 11
}
}
},
Expand Down Expand Up @@ -117,7 +117,7 @@ module.exports = [
score: false,
extendedInfo: {
value: {
length: 4
length: 5
}
}
},
Expand All @@ -134,6 +134,14 @@ module.exports = [
'uses-optimized-images': {
score: false
},
'deprecations': {
score: false,
extendedInfo: {
value: {
length: 4
}
}
}
}
}, {
initialUrl: 'http://localhost:10200/online-only.html',
Expand Down
84 changes: 84 additions & 0 deletions lighthouse-core/audits/deprecations.js
@@ -0,0 +1,84 @@
/**
* @license
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

/**
* @fileoverview Audits a page to determine if it is calling deprecated APIs.
* This is done by collecting console log messages and filtering them by ones
* that contain deprecated API warnings sent by Chrome.
*/

const Audit = require('./audit');
const Formatter = require('../formatters/formatter');

class Deprecations extends Audit {
/**
* @return {!AuditMeta}
*/
static get meta() {
return {
category: 'Deprecations',
name: 'deprecations',
description: 'Site does not use deprecated APIs',
helpText: 'We found some uses of deprecated APIs. Please consider migrating ' +
'to a newer option. [Learn more](https://www.chromestatus.com/features#deprecated).',
requiredArtifacts: ['ChromeConsoleMessages']
};
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.ChromeConsoleMessages.rawValue === -1) {
return Deprecations.generateAuditResult(artifacts.ChromeConsoleMessages);
}

const entries = artifacts.ChromeConsoleMessages;

const deprecations = entries.filter(log => log.entry.source === 'deprecation')
.map(log => {
// CSS deprecations can have missing URLs and lineNumbers. See https://crbug.com/680832.
const label = log.entry.lineNumber ? `line: ${log.entry.lineNumber}` : 'line: ???';
const url = log.entry.url || 'Unable to determine URL';
return Object.assign({
label,
url,
code: log.entry.text
}, log.entry);
});

let displayValue = '';
if (deprecations.length > 1) {
displayValue = `${deprecations.length} warnings found`;
} else if (deprecations.length === 1) {
displayValue = `${deprecations.length} warning found`;
}

return Deprecations.generateAuditResult({
rawValue: deprecations.length === 0,
displayValue,
extendedInfo: {
formatter: Formatter.SUPPORTED_FORMATS.URLLIST,
value: deprecations
}
});
}
}

module.exports = Deprecations;
7 changes: 7 additions & 0 deletions lighthouse-core/config/default.json
Expand Up @@ -29,6 +29,7 @@
"recordNetwork": true,
"passName": "dbw",
"gatherers": [
"chrome-console-messages",
"styles",
"css-usage",
"dobetterweb/all-event-listeners",
Expand Down Expand Up @@ -72,6 +73,7 @@
"theme-color-meta",
"unused-css-rules",
"content-width",
"deprecations",
"accessibility/aria-allowed-attr",
"accessibility/aria-required-attr",
"accessibility/aria-valid-attr-value",
Expand Down Expand Up @@ -333,6 +335,11 @@
"expectedValue": false
}
}
}, {
"name": "Avoiding deprecated APIs and browser interventions",
"audits": {
"deprecations": {}
}
}, {
"name": "Accessibility",
"audits": {
Expand Down
56 changes: 56 additions & 0 deletions lighthouse-core/gather/gatherers/chrome-console-messages.js
@@ -0,0 +1,56 @@
/**
* @license
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @fileoverview Gathers console deprecation and intervention warnings logged by Chrome.
*/

'use strict';

const Gatherer = require('./gatherer');

class ChromeConsoleMessages extends Gatherer {

constructor() {
super();
this._logEntries = [];
this._onConsoleEntryAdded = this.onConsoleEntry.bind(this);
}

onConsoleEntry(entry) {
this._logEntries.push(entry);
}

beforePass(options) {
options.driver.on('Log.entryAdded', this._onConsoleEntryAdded);
return options.driver.sendCommand('Log.enable');
}

afterPass(options) {
options.driver.off('Log.entryAdded', this._onConsoleEntryAdded);
return options.driver.sendCommand('Log.disable')
.then(_ => this._logEntries)
.catch(err => {
return {
rawValue: -1,
debugString: err.message
};
});
}
}

module.exports = ChromeConsoleMessages;
99 changes: 99 additions & 0 deletions lighthouse-core/test/audits/deprecations-test.js
@@ -0,0 +1,99 @@
/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const DeprecationsAudit = require('../../audits/deprecations.js');
const assert = require('assert');

/* eslint-env mocha */

describe('Console deprecations audit', () => {
it('fails when gather fails', () => {
const debugString = 'the uniquest debug string';
const auditResult = DeprecationsAudit.audit({
ChromeConsoleMessages: {
rawValue: -1,
debugString
}
});
assert.equal(auditResult.rawValue, -1);
assert.strictEqual(auditResult.debugString, debugString);
});

it('passes when no console messages were found', () => {
const auditResult = DeprecationsAudit.audit({
ChromeConsoleMessages: []
});
assert.equal(auditResult.rawValue, true);
assert.ok(!auditResult.debugString);
assert.equal(auditResult.extendedInfo.value.length, 0);
});

it('handles deprecations that do not have url or line numbers', () => {
const auditResult = DeprecationsAudit.audit({
ChromeConsoleMessages: [
{
entry: {
source: 'deprecation',
text: 'Deprecation message'
}
}
]
});
assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.displayValue, '1 warning found');
assert.equal(auditResult.extendedInfo.value.length, 1);
assert.equal(auditResult.extendedInfo.value[0].url, 'Unable to determine URL');
assert.equal(auditResult.extendedInfo.value[0].label, 'line: ???');
});

it('fails when deprecation messages are found', () => {
const URL = 'http://example.com';

const auditResult = DeprecationsAudit.audit({
ChromeConsoleMessages: [
{
entry: {
source: 'deprecation',
lineNumber: 123,
url: URL,
text: 'Deprecation message 123'
}
}, {
entry: {
source: 'deprecation',
lineNumber: 456,
url: 'http://example2.com',
text: 'Deprecation message 456'
}
}, {
entry: {
source: 'somethingelse',
lineNumber: 789,
url: 'http://example3.com',
text: 'Not a deprecation message 456'
}
}
]
});
assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.displayValue, '2 warnings found');
assert.equal(auditResult.extendedInfo.value.length, 2);
assert.equal(auditResult.extendedInfo.value[0].url, URL);
assert.equal(auditResult.extendedInfo.value[0].label, 'line: 123');
});
});

0 comments on commit 3c8f57c

Please sign in to comment.