Skip to content

Commit

Permalink
chore: migrate src/JSHandle to TS (#5703)
Browse files Browse the repository at this point in the history
* chore: migrate src/JSHandle to TS

There's a few TODOs in here that all depend on typing the
`ExecutionContext.evaluateHandle` properly so that you can properly
declare what types you're expecting back. Once I've done that file (it's
next on my list) I will loop back and improve the types here, fixing
these TODOs.

* Fix doclint for {}
  • Loading branch information
jackfranklin committed Apr 21, 2020
1 parent 42893d8 commit 8d5d76e
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 207 deletions.
5 changes: 4 additions & 1 deletion src/Accessibility.js
Expand Up @@ -17,6 +17,9 @@
// Used as a TypeDef
// eslint-disable-next-line no-unused-vars
const {CDPSession} = require('./Connection');
// Used as a TypeDef
// eslint-disable-next-line no-unused-vars
const {ElementHandle} = require('./JSHandle');

/**
* @typedef {Object} SerializedAXNode
Expand Down Expand Up @@ -64,7 +67,7 @@ class Accessibility {
}

/**
* @param {{interestingOnly?: boolean, root?: ?Puppeteer.ElementHandle}=} options
* @param {{interestingOnly?: boolean, root?: ?ElementHandle}=} options
* @return {!Promise<!SerializedAXNode>}
*/
async snapshot(options = {}) {
Expand Down
29 changes: 16 additions & 13 deletions src/DOMWorld.js
Expand Up @@ -18,6 +18,9 @@ const fs = require('fs');
const {helper, assert} = require('./helper');
const {LifecycleWatcher} = require('./LifecycleWatcher');
const {TimeoutError} = require('./Errors');
// Used as a TypeDef
// eslint-disable-next-line no-unused-vars
const {JSHandle, ElementHandle} = require('./JSHandle');

// Used as a TypeDef
// eslint-disable-next-line no-unused-vars
Expand All @@ -39,7 +42,7 @@ class DOMWorld {
this._frame = frame;
this._timeoutSettings = timeoutSettings;

/** @type {?Promise<!Puppeteer.ElementHandle>} */
/** @type {?Promise<!ElementHandle>} */
this._documentPromise = null;
/** @type {!Promise<!Puppeteer.ExecutionContext>} */
this._contextPromise;
Expand Down Expand Up @@ -100,7 +103,7 @@ class DOMWorld {
/**
* @param {Function|string} pageFunction
* @param {!Array<*>} args
* @return {!Promise<!Puppeteer.JSHandle>}
* @return {!Promise<!JSHandle>}
*/
async evaluateHandle(pageFunction, ...args) {
const context = await this.executionContext();
Expand All @@ -119,7 +122,7 @@ class DOMWorld {

/**
* @param {string} selector
* @return {!Promise<?Puppeteer.ElementHandle>}
* @return {!Promise<?ElementHandle>}
*/
async $(selector) {
const document = await this._document();
Expand All @@ -128,7 +131,7 @@ class DOMWorld {
}

/**
* @return {!Promise<!Puppeteer.ElementHandle>}
* @return {!Promise<!ElementHandle>}
*/
async _document() {
if (this._documentPromise)
Expand All @@ -142,7 +145,7 @@ class DOMWorld {

/**
* @param {string} expression
* @return {!Promise<!Array<!Puppeteer.ElementHandle>>}
* @return {!Promise<!Array<!ElementHandle>>}
*/
async $x(expression) {
const document = await this._document();
Expand Down Expand Up @@ -175,7 +178,7 @@ class DOMWorld {

/**
* @param {string} selector
* @return {!Promise<!Array<!Puppeteer.ElementHandle>>}
* @return {!Promise<!Array<!ElementHandle>>}
*/
async $$(selector) {
const document = await this._document();
Expand Down Expand Up @@ -225,7 +228,7 @@ class DOMWorld {

/**
* @param {!{url?: string, path?: string, content?: string, type?: string}} options
* @return {!Promise<!Puppeteer.ElementHandle>}
* @return {!Promise<!ElementHandle>}
*/
async addScriptTag(options) {
const {
Expand Down Expand Up @@ -296,7 +299,7 @@ class DOMWorld {

/**
* @param {!{url?: string, path?: string, content?: string}} options
* @return {!Promise<!Puppeteer.ElementHandle>}
* @return {!Promise<!ElementHandle>}
*/
async addStyleTag(options) {
const {
Expand Down Expand Up @@ -431,7 +434,7 @@ class DOMWorld {
/**
* @param {string} selector
* @param {!{visible?: boolean, hidden?: boolean, timeout?: number}=} options
* @return {!Promise<?Puppeteer.ElementHandle>}
* @return {!Promise<?ElementHandle>}
*/
waitForSelector(selector, options) {
return this._waitForSelectorOrXPath(selector, false, options);
Expand All @@ -440,7 +443,7 @@ class DOMWorld {
/**
* @param {string} xpath
* @param {!{visible?: boolean, hidden?: boolean, timeout?: number}=} options
* @return {!Promise<?Puppeteer.ElementHandle>}
* @return {!Promise<?ElementHandle>}
*/
waitForXPath(xpath, options) {
return this._waitForSelectorOrXPath(xpath, true, options);
Expand All @@ -449,7 +452,7 @@ class DOMWorld {
/**
* @param {Function|string} pageFunction
* @param {!{polling?: string|number, timeout?: number}=} options
* @return {!Promise<!Puppeteer.JSHandle>}
* @return {!Promise<!JSHandle>}
*/
waitForFunction(pageFunction, options = {}, ...args) {
const {
Expand All @@ -470,7 +473,7 @@ class DOMWorld {
* @param {string} selectorOrXPath
* @param {boolean} isXPath
* @param {!{visible?: boolean, hidden?: boolean, timeout?: number}=} options
* @return {!Promise<?Puppeteer.ElementHandle>}
* @return {!Promise<?ElementHandle>}
*/
async _waitForSelectorOrXPath(selectorOrXPath, isXPath, options = {}) {
const {
Expand Down Expand Up @@ -568,7 +571,7 @@ class WaitTask {

async rerun() {
const runCount = ++this._runCount;
/** @type {?Puppeteer.JSHandle} */
/** @type {?JSHandle} */
let success = null;
let error = null;
try {
Expand Down
12 changes: 7 additions & 5 deletions src/ExecutionContext.js
Expand Up @@ -15,10 +15,12 @@
*/

const {helper, assert} = require('./helper');
const {createJSHandle, JSHandle} = require('./JSHandle');
// Used as a TypeDef
// eslint-disable-next-line no-unused-vars
const {CDPSession} = require('./Connection');
// Used as a TypeDef
// eslint-disable-next-line no-unused-vars
const {createJSHandle, JSHandle, ElementHandle} = require('./JSHandle');

const EVALUATION_SCRIPT_URL = '__puppeteer_evaluation_script__';
const SOURCE_URL_REGEX = /^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/m;
Expand Down Expand Up @@ -187,19 +189,19 @@ class ExecutionContext {

/**
* @param {Protocol.DOM.BackendNodeId} backendNodeId
* @return {Promise<Puppeteer.ElementHandle>}
* @return {Promise<ElementHandle>}
*/
async _adoptBackendNodeId(backendNodeId) {
const {object} = await this._client.send('DOM.resolveNode', {
backendNodeId: backendNodeId,
executionContextId: this._contextId,
});
return /** @type {Puppeteer.ElementHandle}*/(createJSHandle(this, object));
return /** @type {ElementHandle}*/(createJSHandle(this, object));
}

/**
* @param {Puppeteer.ElementHandle} elementHandle
* @return {Promise<Puppeteer.ElementHandle>}
* @param {ElementHandle} elementHandle
* @return {Promise<ElementHandle>}
*/
async _adoptElementHandle(elementHandle) {
assert(elementHandle.executionContext() !== this, 'Cannot adopt handle that already belongs to this execution context');
Expand Down
23 changes: 13 additions & 10 deletions src/FrameManager.js
Expand Up @@ -27,6 +27,9 @@ const {TimeoutSettings} = require('./TimeoutSettings');
// Used as a TypeDef
// eslint-disable-next-line no-unused-vars
const {CDPSession} = require('./Connection');
// Used as a TypeDef
// eslint-disable-next-line no-unused-vars
const {JSHandle, ElementHandle} = require('./JSHandle');

const UTILITY_WORLD_NAME = '__puppeteer_utility_world__';

Expand Down Expand Up @@ -432,7 +435,7 @@ class Frame {
/**
* @param {Function|string} pageFunction
* @param {!Array<*>} args
* @return {!Promise<!Puppeteer.JSHandle>}
* @return {!Promise<!JSHandle>}
*/
async evaluateHandle(pageFunction, ...args) {
return this._mainWorld.evaluateHandle(pageFunction, ...args);
Expand All @@ -449,15 +452,15 @@ class Frame {

/**
* @param {string} selector
* @return {!Promise<?Puppeteer.ElementHandle>}
* @return {!Promise<?ElementHandle>}
*/
async $(selector) {
return this._mainWorld.$(selector);
}

/**
* @param {string} expression
* @return {!Promise<!Array<!Puppeteer.ElementHandle>>}
* @return {!Promise<!Array<!ElementHandle>>}
*/
async $x(expression) {
return this._mainWorld.$x(expression);
Expand Down Expand Up @@ -485,7 +488,7 @@ class Frame {

/**
* @param {string} selector
* @return {!Promise<!Array<!Puppeteer.ElementHandle>>}
* @return {!Promise<!Array<!ElementHandle>>}
*/
async $$(selector) {
return this._mainWorld.$$(selector);
Expand Down Expand Up @@ -543,15 +546,15 @@ class Frame {

/**
* @param {!{url?: string, path?: string, content?: string, type?: string}} options
* @return {!Promise<!Puppeteer.ElementHandle>}
* @return {!Promise<!ElementHandle>}
*/
async addScriptTag(options) {
return this._mainWorld.addScriptTag(options);
}

/**
* @param {!{url?: string, path?: string, content?: string}} options
* @return {!Promise<!Puppeteer.ElementHandle>}
* @return {!Promise<!ElementHandle>}
*/
async addStyleTag(options) {
return this._mainWorld.addStyleTag(options);
Expand Down Expand Up @@ -608,7 +611,7 @@ class Frame {
* @param {(string|number|Function)} selectorOrFunctionOrTimeout
* @param {!Object=} options
* @param {!Array<*>} args
* @return {!Promise<?Puppeteer.JSHandle>}
* @return {!Promise<?JSHandle>}
*/
waitFor(selectorOrFunctionOrTimeout, options = {}, ...args) {
const xPathPattern = '//';
Expand All @@ -629,7 +632,7 @@ class Frame {
/**
* @param {string} selector
* @param {!{visible?: boolean, hidden?: boolean, timeout?: number}=} options
* @return {!Promise<?Puppeteer.ElementHandle>}
* @return {!Promise<?ElementHandle>}
*/
async waitForSelector(selector, options) {
const handle = await this._secondaryWorld.waitForSelector(selector, options);
Expand All @@ -644,7 +647,7 @@ class Frame {
/**
* @param {string} xpath
* @param {!{visible?: boolean, hidden?: boolean, timeout?: number}=} options
* @return {!Promise<?Puppeteer.ElementHandle>}
* @return {!Promise<?ElementHandle>}
*/
async waitForXPath(xpath, options) {
const handle = await this._secondaryWorld.waitForXPath(xpath, options);
Expand All @@ -659,7 +662,7 @@ class Frame {
/**
* @param {Function|string} pageFunction
* @param {!{polling?: string|number, timeout?: number}=} options
* @return {!Promise<!Puppeteer.JSHandle>}
* @return {!Promise<!JSHandle>}
*/
waitForFunction(pageFunction, options = {}, ...args) {
return this._mainWorld.waitForFunction(pageFunction, options, ...args);
Expand Down

0 comments on commit 8d5d76e

Please sign in to comment.