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

new_audit: add js-libraries audit, just listing detected js libs #6081

Merged
merged 4 commits into from
Sep 22, 2018
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
52 changes: 52 additions & 0 deletions lighthouse-core/audits/dobetterweb/js-libraries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
Copy link
Member

Choose a reason for hiding this comment

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

2018

* 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 Diagnostic audit that lists all JavaScript libraries detected on the page
*/

'use strict';

const Audit = require('../audit');

class JsLibrariesAudit extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'js-libraries',
title: 'Detected JavaScript libraries',
description: 'All front-end JavaScript libraries detected on the page.',
exterkamp marked this conversation as resolved.
Show resolved Hide resolved
requiredArtifacts: ['JSLibraries'],
};
}

/**
* @param {LH.Artifacts} artifacts
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
const libDetails = artifacts.JSLibraries.map(lib => ({
name: lib.name,
version: lib.version, // null if not detected
npm: lib.npmPkgName || null, // ~70% of libs come with this field
}));

const headings = [
{key: 'name', itemType: 'text', text: 'Name'},
{key: 'version', itemType: 'text', text: 'Version'},
];
const details = Audit.makeTableDetails(headings, libDetails, {});

return {
rawValue: true, // Always pass for now.
details,
};
}
}

module.exports = JsLibrariesAudit;
2 changes: 2 additions & 0 deletions lighthouse-core/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ const defaultConfig = {
'dobetterweb/geolocation-on-start',
'dobetterweb/no-document-write',
'dobetterweb/no-vulnerable-libraries',
'dobetterweb/js-libraries',
'dobetterweb/no-websql',
'dobetterweb/notification-on-start',
'dobetterweb/password-inputs-can-be-pasted-into',
Expand Down Expand Up @@ -408,6 +409,7 @@ const defaultConfig = {
{id: 'geolocation-on-start', weight: 1},
{id: 'doctype', weight: 1},
{id: 'no-vulnerable-libraries', weight: 1},
{id: 'js-libraries', weight: 0},
{id: 'notification-on-start', weight: 1},
{id: 'deprecations', weight: 1},
{id: 'password-inputs-can-be-pasted-into', weight: 1},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* @license Copyright 2018 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.
*/
Expand Down
64 changes: 64 additions & 0 deletions lighthouse-core/test/audits/dobetterweb/js-libraries-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @license Copyright 2018 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 JsLibrariesAudit = require('../../../audits/dobetterweb/js-libraries.js');
const assert = require('assert');

/* eslint-env jest */
describe('Returns detected front-end JavaScript libraries', () => {
it('always passes', () => {
// no libraries
const auditResult1 = JsLibrariesAudit.audit({
JSLibraries: [],
});
assert.equal(auditResult1.rawValue, true);

// duplicates. TODO: consider failing in this case
Copy link
Member

Choose a reason for hiding this comment

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

TODO: consider failing in this case

it's not that uncommon to have more than one jquery in one page. And if we merge network stuff from iframes, won't it be even more common?

const auditResult2 = JsLibrariesAudit.audit({
JSLibraries: [
{name: 'lib1', version: '3.10.1', npmPkgName: 'lib1'},
{name: 'lib2', version: null, npmPkgName: 'lib2'},
],
});
assert.equal(auditResult2.rawValue, true);

// LOTS of frontend libs
const auditResult3 = JsLibrariesAudit.audit({
JSLibraries: [
{name: 'React', version: null, npmPkgName: 'react'},
{name: 'Polymer', version: null, npmPkgName: 'polymer-core'},
{name: 'Preact', version: null, npmPkgName: 'preact'},
{name: 'Angular', version: null, npmPkgName: 'angular'},
{name: 'jQuery', version: null, npmPkgName: 'jquery'},
],
});
assert.equal(auditResult3.rawValue, true);
});

it('generates expected details', () => {
const auditResult = JsLibrariesAudit.audit({
JSLibraries: [
{name: 'lib1', version: '3.10.1', npmPkgName: 'lib1'},
{name: 'lib2', version: null, npmPkgName: 'lib2'},
],
});
const expected = [
{
name: 'lib1',
npm: 'lib1',
version: '3.10.1',
},
{
name: 'lib2',
npm: 'lib2',
version: null,
},
];
assert.equal(auditResult.rawValue, true);
assert.deepStrictEqual(auditResult.details.items, expected);
});
});
35 changes: 35 additions & 0 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2389,6 +2389,37 @@
"summary": {}
}
},
"js-libraries": {
"id": "js-libraries",
"title": "Detected JavaScript libraries",
"description": "All front-end JavaScript libraries detected on the page.",
"score": 1,
"scoreDisplayMode": "binary",
"rawValue": true,
"details": {
"type": "table",
"headings": [
{
"key": "name",
"itemType": "text",
"text": "Name"
},
{
"key": "version",
"itemType": "text",
"text": "Version"
}
],
"items": [
{
"name": "jQuery",
"version": "2.1.1",
"npm": "jquery"
}
],
"summary": {}
}
},
"no-websql": {
"id": "no-websql",
"title": "Uses WebSQL DB",
Expand Down Expand Up @@ -3255,6 +3286,10 @@
"id": "no-vulnerable-libraries",
"weight": 1
},
{
"id": "js-libraries",
"weight": 0
},
{
"id": "notification-on-start",
"weight": 1
Expand Down
2 changes: 1 addition & 1 deletion typings/audit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ declare global {

export type DetailsItem = string | number | DetailsRendererNodeDetailsJSON |
DetailsRendererLinkDetailsJSON | DetailsRendererCodeDetailJSON | undefined |
boolean | DetailsRendererUrlDetailsJSON;
boolean | DetailsRendererUrlDetailsJSON | null;
Copy link
Member

Choose a reason for hiding this comment

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

details make me sad


export interface DetailsRendererNodeDetailsJSON {
type: 'node';
Expand Down