From 42893d875553e14d8319ed74abe562ed4aafeee7 Mon Sep 17 00:00:00 2001 From: Jack Franklin Date: Tue, 21 Apr 2020 11:45:29 +0100 Subject: [PATCH] chore: migrate src/coverage to TypeScript (#5702) --- src/{Coverage.js => Coverage.ts} | 139 ++++++++++++------------------- 1 file changed, 54 insertions(+), 85 deletions(-) rename src/{Coverage.js => Coverage.ts} (76%) diff --git a/src/Coverage.js b/src/Coverage.ts similarity index 76% rename from src/Coverage.js rename to src/Coverage.ts index 437469bb..4e46464f 100644 --- a/src/Coverage.js +++ b/src/Coverage.ts @@ -14,77 +14,57 @@ * limitations under the License. */ -const {helper, debugError, assert} = require('./helper'); -// Used as a TypeDef -// eslint-disable-next-line no-unused-vars -const {CDPSession} = require('./Connection'); +import {helper, debugError, assert, PuppeteerEventListener} from './helper'; +import {CDPSession} from './Connection'; -const {EVALUATION_SCRIPT_URL} = require('./ExecutionContext'); +import {EVALUATION_SCRIPT_URL} from './ExecutionContext'; -/** - * @typedef {Object} CoverageEntry - * @property {string} url - * @property {string} text - * @property {!Array} ranges - */ +interface CoverageEntry { + url: string; + text: string; + ranges: Array<{start: number; end: number}>; +} -class Coverage { - /** - * @param {!CDPSession} client - */ - constructor(client) { +export class Coverage { + _jsCoverage: JSCoverage; + _cssCoverage: CSSCoverage; + + constructor(client: CDPSession) { this._jsCoverage = new JSCoverage(client); this._cssCoverage = new CSSCoverage(client); } - /** - * @param {!{resetOnNavigation?: boolean, reportAnonymousScripts?: boolean}} options - */ - async startJSCoverage(options) { + async startJSCoverage(options: {resetOnNavigation?: boolean; reportAnonymousScripts?: boolean}): Promise { return await this._jsCoverage.start(options); } - /** - * @return {!Promise>} - */ - async stopJSCoverage() { + async stopJSCoverage(): Promise { return await this._jsCoverage.stop(); } - /** - * @param {{resetOnNavigation?: boolean}=} options - */ - async startCSSCoverage(options) { + async startCSSCoverage(options: {resetOnNavigation?: boolean}): Promise { return await this._cssCoverage.start(options); } - /** - * @return {!Promise>} - */ - async stopCSSCoverage() { + async stopCSSCoverage(): Promise { return await this._cssCoverage.stop(); } } -module.exports = {Coverage}; - class JSCoverage { - /** - * @param {!CDPSession} client - */ - constructor(client) { + _client: CDPSession; + _enabled = false; + _scriptURLs = new Map(); + _scriptSources = new Map(); + _eventListeners: PuppeteerEventListener[] = []; + _resetOnNavigation = false; + _reportAnonymousScripts = false; + + constructor(client: CDPSession) { this._client = client; - this._enabled = false; - this._scriptURLs = new Map(); - this._scriptSources = new Map(); - this._eventListeners = []; - this._resetOnNavigation = false; } - /** - * @param {!{resetOnNavigation?: boolean, reportAnonymousScripts?: boolean}} options - */ - async start(options = {}) { + async start(options: {resetOnNavigation?: boolean; reportAnonymousScripts?: boolean} = {}): Promise { assert(!this._enabled, 'JSCoverage is already enabled'); const { resetOnNavigation = true, @@ -107,17 +87,14 @@ class JSCoverage { ]); } - _onExecutionContextsCleared() { + _onExecutionContextsCleared(): void { if (!this._resetOnNavigation) return; this._scriptURLs.clear(); this._scriptSources.clear(); } - /** - * @param {!Protocol.Debugger.scriptParsedPayload} event - */ - async _onScriptParsed(event) { + async _onScriptParsed(event: Protocol.Debugger.scriptParsedPayload): Promise { // Ignore puppeteer-injected scripts if (event.url === EVALUATION_SCRIPT_URL) return; @@ -134,22 +111,27 @@ class JSCoverage { } } - /** - * @return {!Promise>} - */ - async stop() { + async stop(): Promise { assert(this._enabled, 'JSCoverage is not enabled'); 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.stopPreciseCoverage'), this._client.send('Profiler.disable'), this._client.send('Debugger.disable'), ]); + helper.removeEventListeners(this._eventListeners); const coverage = []; - const profileResponse = /** @type Protocol.Profiler.takePreciseCoverageReturnValue */ (result[0]); + const profileResponse = result[0]; + for (const entry of profileResponse.result) { let url = this._scriptURLs.get(entry.scriptId); if (!url && this._reportAnonymousScripts) @@ -168,22 +150,19 @@ class JSCoverage { } class CSSCoverage { - /** - * @param {!CDPSession} client - */ - constructor(client) { + _client: CDPSession; + _enabled = false; + _stylesheetURLs = new Map(); + _stylesheetSources = new Map(); + _eventListeners: PuppeteerEventListener[] = []; + _resetOnNavigation = false; + _reportAnonymousScripts = false; + + constructor(client: CDPSession) { this._client = client; - this._enabled = false; - this._stylesheetURLs = new Map(); - this._stylesheetSources = new Map(); - this._eventListeners = []; - this._resetOnNavigation = false; } - /** - * @param {{resetOnNavigation?: boolean}=} options - */ - async start(options = {}) { + async start(options: {resetOnNavigation?: boolean} = {}): Promise { assert(!this._enabled, 'CSSCoverage is already enabled'); const {resetOnNavigation = true} = options; this._resetOnNavigation = resetOnNavigation; @@ -201,17 +180,14 @@ class CSSCoverage { ]); } - _onExecutionContextsCleared() { + _onExecutionContextsCleared(): void { if (!this._resetOnNavigation) return; this._stylesheetURLs.clear(); this._stylesheetSources.clear(); } - /** - * @param {!Protocol.CSS.styleSheetAddedPayload} event - */ - async _onStyleSheet(event) { + async _onStyleSheet(event: Protocol.CSS.styleSheetAddedPayload): Promise { const header = event.header; // Ignore anonymous scripts if (!header.sourceURL) @@ -226,10 +202,7 @@ class CSSCoverage { } } - /** - * @return {!Promise>} - */ - async stop() { + async stop(): Promise { assert(this._enabled, 'CSSCoverage is not enabled'); this._enabled = false; const ruleTrackingResponse = await this._client.send('CSS.stopRuleUsageTracking'); @@ -266,11 +239,7 @@ class CSSCoverage { } } -/** - * @param {!Array} nestedRanges - * @return {!Array} - */ -function convertToDisjointRanges(nestedRanges) { +function convertToDisjointRanges(nestedRanges: Array<{startOffset: number; endOffset: number; count: number}>): Array<{start: number; end: number}> { const points = []; for (const range of nestedRanges) { points.push({offset: range.startOffset, type: 0, range});