forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprecations.js
114 lines (100 loc) · 4.35 KB
/
deprecations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Audits a page to determine if it is calling deprecated APIs.
*/
import {Audit} from './audit.js';
import {JSBundles} from '../computed/js-bundles.js';
import * as i18n from '../lib/i18n/i18n.js';
import {getIssueDetailDescription} from '../lib/deprecation-description.js';
/* eslint-disable max-len */
const UIStrings = {
/** Title of a Lighthouse audit that provides detail on the use of deprecated APIs. This descriptive title is shown to users when the page does not use deprecated APIs. */
title: 'Avoids deprecated APIs',
/** Title of a Lighthouse audit that provides detail on the use of deprecated APIs. This descriptive title is shown to users when the page uses deprecated APIs. */
failureTitle: 'Uses deprecated APIs',
/** Description of a Lighthouse audit that tells the user why they should not use deprecated APIs on their page. This is displayed after a user expands the section to see more. No character length limits. The last sentence starting with 'Learn' becomes link text to additional documentation. */
description: 'Deprecated APIs will eventually be removed from the browser. ' +
'[Learn more about deprecated APIs](https://developer.chrome.com/docs/lighthouse/best-practices/deprecations/).',
/** [ICU Syntax] Label for the audit identifying the number of warnings generated by using deprecated APIs. */
displayValue: `{itemCount, plural,
=1 {1 warning found}
other {# warnings found}
}`,
/** Header of the table column which displays the warning message describing use of a deprecated API by code running in the web page. */
columnDeprecate: 'Deprecation / Warning',
/** Table column header for line of code (eg. 432) that is using a deprecated API. */
columnLine: 'Line',
};
/* eslint-enable max-len */
const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings);
class Deprecations extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'deprecations',
title: str_(UIStrings.title),
failureTitle: str_(UIStrings.failureTitle),
description: str_(UIStrings.description),
requiredArtifacts: ['InspectorIssues', 'SourceMaps', 'Scripts'],
};
}
/**
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts, context) {
const bundles = await JSBundles.request(artifacts, context);
const deprecations = artifacts.InspectorIssues.deprecationIssue
.map(deprecation => {
const {scriptId, url, lineNumber, columnNumber} = deprecation.sourceCodeLocation;
const bundle = bundles.find(bundle => bundle.script.scriptId === scriptId);
const deprecationMeta = getIssueDetailDescription(deprecation);
/** @type {LH.Audit.Details.TableSubItems=} */
let subItems = undefined;
if (deprecationMeta.links.length) {
subItems = {
type: 'subitems',
items: deprecationMeta.links.map(link => ({
type: 'link',
url: link.link,
text: link.linkTitle,
})),
};
}
// @ts-expect-error: The english string used to be included on the protocol, but no longer is.
const legacyMessage = deprecation.message;
/** @type {LH.Audit.Details.TableItem} */
const item = {
value: deprecationMeta.message || legacyMessage || deprecation.type,
// Protocol.Audits.SourceCodeLocation.columnNumber is 1-indexed, but we use 0-indexed.
source: Audit.makeSourceLocation(url, lineNumber, columnNumber - 1, bundle),
subItems,
};
return item;
});
/** @type {LH.Audit.Details.Table['headings']} */
const headings = [
{key: 'value', valueType: 'text', label: str_(UIStrings.columnDeprecate)},
{key: 'source', valueType: 'source-location', label: str_(i18n.UIStrings.columnSource)},
];
const details = Audit.makeTableDetails(headings, deprecations);
let displayValue;
if (deprecations.length > 0) {
displayValue = str_(UIStrings.displayValue, {itemCount: deprecations.length});
}
return {
score: Number(deprecations.length === 0),
displayValue,
details,
};
}
}
export default Deprecations;
export {UIStrings};