Skip to content

Commit

Permalink
new_audit: add js-libraries audit, just listing detected js libs (#6081)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Sep 22, 2018
1 parent 489da22 commit 417cdbe
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 2 deletions.
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.
* 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.',
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
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 @@ -2394,6 +2394,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 @@ -3260,6 +3291,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 @@ -95,7 +95,7 @@ declare global {

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

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

0 comments on commit 417cdbe

Please sign in to comment.