fix: fix null-type bugs (#3137)

I ran TypeScript against our code with `strictNullChecks` on. Most of the errors generated are noise, because TypeScript doesn't understand how our `assert` method works. But some were legitimate bugs. They are fixed in this patch.
This commit is contained in:
Joel Einbinder 2018-08-24 15:17:36 -07:00 committed by Andrey Lushnikov
parent d1105afaf8
commit 3d7ae2a259
9 changed files with 16 additions and 12 deletions

View File

@ -161,7 +161,7 @@ class Browser extends EventEmitter {
} }
/** /**
* @param {string} contextId * @param {?string} contextId
* @return {!Promise<!Puppeteer.Page>} * @return {!Promise<!Puppeteer.Page>}
*/ */
async _createPageInContext(contextId) { async _createPageInContext(contextId) {

View File

@ -172,6 +172,7 @@ class CDPSession extends EventEmitter {
this._lastId = 0; this._lastId = 0;
/** @type {!Map<number, {resolve: function, reject: function, error: !Error, method: string}>}*/ /** @type {!Map<number, {resolve: function, reject: function, error: !Error, method: string}>}*/
this._callbacks = new Map(); this._callbacks = new Map();
/** @type {null|Connection|CDPSession} */
this._connection = connection; this._connection = connection;
this._targetType = targetType; this._targetType = targetType;
this._sessionId = sessionId; this._sessionId = sessionId;
@ -234,6 +235,8 @@ class CDPSession extends EventEmitter {
} }
async detach() { async detach() {
if (!this._connection)
throw new Error(`Session already detached. Most likely the ${this._targetType} has been closed.`);
await this._connection.send('Target.detachFromTarget', {sessionId: this._sessionId}); await this._connection.send('Target.detachFromTarget', {sessionId: this._sessionId});
} }
@ -266,8 +269,7 @@ function createProtocolError(error, method, object) {
let message = `Protocol error (${method}): ${object.error.message}`; let message = `Protocol error (${method}): ${object.error.message}`;
if ('data' in object.error) if ('data' in object.error)
message += ` ${object.error.data}`; message += ` ${object.error.data}`;
if (object.error.message) return rewriteError(error, message);
return rewriteError(error, message);
} }
/** /**

View File

@ -473,7 +473,7 @@ class ElementHandle extends JSHandle {
const viewport = this._page.viewport(); const viewport = this._page.viewport();
if (boundingBox.width > viewport.width || boundingBox.height > viewport.height) { if (viewport && (boundingBox.width > viewport.width || boundingBox.height > viewport.height)) {
const newViewport = { const newViewport = {
width: Math.max(viewport.width, Math.ceil(boundingBox.width)), width: Math.max(viewport.width, Math.ceil(boundingBox.width)),
height: Math.max(viewport.height, Math.ceil(boundingBox.height)), height: Math.max(viewport.height, Math.ceil(boundingBox.height)),

View File

@ -118,7 +118,6 @@ class FrameManager extends EventEmitter {
/** /**
* @param {string} frameId * @param {string} frameId
* @param {?string} parentFrameId * @param {?string} parentFrameId
* @return {?Frame}
*/ */
_onFrameAttached(frameId, parentFrameId) { _onFrameAttached(frameId, parentFrameId) {
if (this._frames.has(frameId)) if (this._frames.has(frameId))
@ -265,14 +264,16 @@ class Frame {
this._parentFrame = parentFrame; this._parentFrame = parentFrame;
this._url = ''; this._url = '';
this._id = frameId; this._id = frameId;
this._detached = false;
/** @type {?Promise<!Puppeteer.ElementHandle>} */ /** @type {?Promise<!Puppeteer.ElementHandle>} */
this._documentPromise = null; this._documentPromise = null;
/** @type {?Promise<!ExecutionContext>} */ /** @type {!Promise<!ExecutionContext>} */
this._contextPromise = null; this._contextPromise;
this._contextResolveCallback = null; this._contextResolveCallback = null;
this._setDefaultContext(null); this._setDefaultContext(null);
/** @type {!Set<!WaitTask>} */ /** @type {!Set<!WaitTask>} */
this._waitTasks = new Set(); this._waitTasks = new Set();
this._loaderId = ''; this._loaderId = '';

View File

@ -38,7 +38,7 @@ class Keyboard {
/** /**
* @param {string} key * @param {string} key
* @param {{text: string}=} options * @param {{text?: string}=} options
*/ */
async down(key, options = { text: undefined }) { async down(key, options = { text: undefined }) {
const description = this._keyDescriptionForString(key); const description = this._keyDescriptionForString(key);

View File

@ -258,7 +258,7 @@ class Launcher {
} }
/** /**
* @param {!(BrowserOptions & {browserWSEndpoint: string})=} options * @param {!(BrowserOptions & {browserWSEndpoint: string})} options
* @return {!Promise<!Browser>} * @return {!Promise<!Browser>}
*/ */
static async connect(options) { static async connect(options) {

View File

@ -256,7 +256,7 @@ class NetworkManager extends EventEmitter {
if (!request) if (!request)
return; return;
const response = new Response(this._client, request, event.response.status, event.response.headers, const response = new Response(this._client, request, event.response.status, event.response.headers,
event.response.fromDiskCache, event.response.fromServiceWorker, event.response.securityDetails); !!event.response.fromDiskCache, !!event.response.fromServiceWorker, event.response.securityDetails);
request._response = response; request._response = response;
this.emit(NetworkManager.Events.Response, response); this.emit(NetworkManager.Events.Response, response);
} }
@ -353,7 +353,7 @@ class Request {
} }
/** /**
* @return {string} * @return {string|undefined}
*/ */
postData() { postData() {
return this._postData; return this._postData;

View File

@ -834,6 +834,7 @@ class Page extends EventEmitter {
clip.scale = 1; clip.scale = 1;
if (options.fullPage) { if (options.fullPage) {
assert(this._viewport, 'fullPage screenshots do not work without first setting viewport.');
const metrics = await this._client.send('Page.getLayoutMetrics'); const metrics = await this._client.send('Page.getLayoutMetrics');
const width = Math.ceil(metrics.contentSize.width); const width = Math.ceil(metrics.contentSize.width);
const height = Math.ceil(metrics.contentSize.height); const height = Math.ceil(metrics.contentSize.height);

View File

@ -77,7 +77,7 @@ class Target {
} }
/** /**
* @return {Puppeteer.Target} * @return {?Puppeteer.Target}
*/ */
opener() { opener() {
const { openerId } = this._targetInfo; const { openerId } = this._targetInfo;