chore: migrate src/coverage to TypeScript (#5702)

This commit is contained in:
Jack Franklin 2020-04-21 11:45:29 +01:00 committed by GitHub
parent e3922ea1f3
commit 42893d8755
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,77 +14,57 @@
* limitations under the License. * limitations under the License.
*/ */
const {helper, debugError, assert} = require('./helper'); import {helper, debugError, assert, PuppeteerEventListener} from './helper';
// Used as a TypeDef import {CDPSession} from './Connection';
// eslint-disable-next-line no-unused-vars
const {CDPSession} = require('./Connection');
const {EVALUATION_SCRIPT_URL} = require('./ExecutionContext'); import {EVALUATION_SCRIPT_URL} from './ExecutionContext';
/** interface CoverageEntry {
* @typedef {Object} CoverageEntry url: string;
* @property {string} url text: string;
* @property {string} text ranges: Array<{start: number; end: number}>;
* @property {!Array<!{start: number, end: number}>} ranges }
*/
class Coverage { export class Coverage {
/** _jsCoverage: JSCoverage;
* @param {!CDPSession} client _cssCoverage: CSSCoverage;
*/
constructor(client) { constructor(client: CDPSession) {
this._jsCoverage = new JSCoverage(client); this._jsCoverage = new JSCoverage(client);
this._cssCoverage = new CSSCoverage(client); this._cssCoverage = new CSSCoverage(client);
} }
/** async startJSCoverage(options: {resetOnNavigation?: boolean; reportAnonymousScripts?: boolean}): Promise<void> {
* @param {!{resetOnNavigation?: boolean, reportAnonymousScripts?: boolean}} options
*/
async startJSCoverage(options) {
return await this._jsCoverage.start(options); return await this._jsCoverage.start(options);
} }
/** async stopJSCoverage(): Promise<CoverageEntry[]> {
* @return {!Promise<!Array<!CoverageEntry>>}
*/
async stopJSCoverage() {
return await this._jsCoverage.stop(); return await this._jsCoverage.stop();
} }
/** async startCSSCoverage(options: {resetOnNavigation?: boolean}): Promise<void> {
* @param {{resetOnNavigation?: boolean}=} options
*/
async startCSSCoverage(options) {
return await this._cssCoverage.start(options); return await this._cssCoverage.start(options);
} }
/** async stopCSSCoverage(): Promise<CoverageEntry[]> {
* @return {!Promise<!Array<!CoverageEntry>>}
*/
async stopCSSCoverage() {
return await this._cssCoverage.stop(); return await this._cssCoverage.stop();
} }
} }
module.exports = {Coverage};
class JSCoverage { class JSCoverage {
/** _client: CDPSession;
* @param {!CDPSession} client _enabled = false;
*/ _scriptURLs = new Map<string, string>();
constructor(client) { _scriptSources = new Map<string, string>();
_eventListeners: PuppeteerEventListener[] = [];
_resetOnNavigation = false;
_reportAnonymousScripts = false;
constructor(client: CDPSession) {
this._client = client; this._client = client;
this._enabled = false;
this._scriptURLs = new Map();
this._scriptSources = new Map();
this._eventListeners = [];
this._resetOnNavigation = false;
} }
/** async start(options: {resetOnNavigation?: boolean; reportAnonymousScripts?: boolean} = {}): Promise<void> {
* @param {!{resetOnNavigation?: boolean, reportAnonymousScripts?: boolean}} options
*/
async start(options = {}) {
assert(!this._enabled, 'JSCoverage is already enabled'); assert(!this._enabled, 'JSCoverage is already enabled');
const { const {
resetOnNavigation = true, resetOnNavigation = true,
@ -107,17 +87,14 @@ class JSCoverage {
]); ]);
} }
_onExecutionContextsCleared() { _onExecutionContextsCleared(): void {
if (!this._resetOnNavigation) if (!this._resetOnNavigation)
return; return;
this._scriptURLs.clear(); this._scriptURLs.clear();
this._scriptSources.clear(); this._scriptSources.clear();
} }
/** async _onScriptParsed(event: Protocol.Debugger.scriptParsedPayload): Promise<void> {
* @param {!Protocol.Debugger.scriptParsedPayload} event
*/
async _onScriptParsed(event) {
// Ignore puppeteer-injected scripts // Ignore puppeteer-injected scripts
if (event.url === EVALUATION_SCRIPT_URL) if (event.url === EVALUATION_SCRIPT_URL)
return; return;
@ -134,22 +111,27 @@ class JSCoverage {
} }
} }
/** async stop(): Promise<CoverageEntry[]> {
* @return {!Promise<!Array<!CoverageEntry>>}
*/
async stop() {
assert(this._enabled, 'JSCoverage is not enabled'); assert(this._enabled, 'JSCoverage is not enabled');
this._enabled = false; this._enabled = false;
const result = await Promise.all([
const result = await Promise.all<
Protocol.Profiler.takePreciseCoverageReturnValue,
Protocol.Profiler.stopPreciseCoverageReturnValue,
Protocol.Profiler.disableReturnValue,
Protocol.Debugger.disableReturnValue
>([
this._client.send('Profiler.takePreciseCoverage'), this._client.send('Profiler.takePreciseCoverage'),
this._client.send('Profiler.stopPreciseCoverage'), this._client.send('Profiler.stopPreciseCoverage'),
this._client.send('Profiler.disable'), this._client.send('Profiler.disable'),
this._client.send('Debugger.disable'), this._client.send('Debugger.disable'),
]); ]);
helper.removeEventListeners(this._eventListeners); helper.removeEventListeners(this._eventListeners);
const coverage = []; const coverage = [];
const profileResponse = /** @type Protocol.Profiler.takePreciseCoverageReturnValue */ (result[0]); const profileResponse = result[0];
for (const entry of profileResponse.result) { for (const entry of profileResponse.result) {
let url = this._scriptURLs.get(entry.scriptId); let url = this._scriptURLs.get(entry.scriptId);
if (!url && this._reportAnonymousScripts) if (!url && this._reportAnonymousScripts)
@ -168,22 +150,19 @@ class JSCoverage {
} }
class CSSCoverage { class CSSCoverage {
/** _client: CDPSession;
* @param {!CDPSession} client _enabled = false;
*/ _stylesheetURLs = new Map<string, string>();
constructor(client) { _stylesheetSources = new Map<string, string>();
_eventListeners: PuppeteerEventListener[] = [];
_resetOnNavigation = false;
_reportAnonymousScripts = false;
constructor(client: CDPSession) {
this._client = client; this._client = client;
this._enabled = false;
this._stylesheetURLs = new Map();
this._stylesheetSources = new Map();
this._eventListeners = [];
this._resetOnNavigation = false;
} }
/** async start(options: {resetOnNavigation?: boolean} = {}): Promise<void> {
* @param {{resetOnNavigation?: boolean}=} options
*/
async start(options = {}) {
assert(!this._enabled, 'CSSCoverage is already enabled'); assert(!this._enabled, 'CSSCoverage is already enabled');
const {resetOnNavigation = true} = options; const {resetOnNavigation = true} = options;
this._resetOnNavigation = resetOnNavigation; this._resetOnNavigation = resetOnNavigation;
@ -201,17 +180,14 @@ class CSSCoverage {
]); ]);
} }
_onExecutionContextsCleared() { _onExecutionContextsCleared(): void {
if (!this._resetOnNavigation) if (!this._resetOnNavigation)
return; return;
this._stylesheetURLs.clear(); this._stylesheetURLs.clear();
this._stylesheetSources.clear(); this._stylesheetSources.clear();
} }
/** async _onStyleSheet(event: Protocol.CSS.styleSheetAddedPayload): Promise<void> {
* @param {!Protocol.CSS.styleSheetAddedPayload} event
*/
async _onStyleSheet(event) {
const header = event.header; const header = event.header;
// Ignore anonymous scripts // Ignore anonymous scripts
if (!header.sourceURL) if (!header.sourceURL)
@ -226,10 +202,7 @@ class CSSCoverage {
} }
} }
/** async stop(): Promise<CoverageEntry[]> {
* @return {!Promise<!Array<!CoverageEntry>>}
*/
async stop() {
assert(this._enabled, 'CSSCoverage is not enabled'); assert(this._enabled, 'CSSCoverage is not enabled');
this._enabled = false; this._enabled = false;
const ruleTrackingResponse = await this._client.send('CSS.stopRuleUsageTracking'); const ruleTrackingResponse = await this._client.send('CSS.stopRuleUsageTracking');
@ -266,11 +239,7 @@ class CSSCoverage {
} }
} }
/** function convertToDisjointRanges(nestedRanges: Array<{startOffset: number; endOffset: number; count: number}>): Array<{start: number; end: number}> {
* @param {!Array<!{startOffset:number, endOffset:number, count:number}>} nestedRanges
* @return {!Array<!{start:number, end:number}>}
*/
function convertToDisjointRanges(nestedRanges) {
const points = []; const points = [];
for (const range of nestedRanges) { for (const range of nestedRanges) {
points.push({offset: range.startOffset, type: 0, range}); points.push({offset: range.startOffset, type: 0, range});