Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: migrate more files to strict-mode TypeScript #7950

Merged
merged 2 commits into from Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/common/Errors.ts
Expand Up @@ -43,7 +43,7 @@ export class TimeoutError extends CustomError {}
*/
export class ProtocolError extends CustomError {
public code?: number;
public originalMessage: string;
public originalMessage = '';
}
/**
* @public
Expand Down
2 changes: 1 addition & 1 deletion src/common/EventEmitter.ts
Expand Up @@ -144,6 +144,6 @@ export class EventEmitter implements CommonEventEmitter {
}

private eventListenersCount(event: EventType): number {
return this.eventsMap.has(event) ? this.eventsMap.get(event).length : 0;
return this.eventsMap.get(event)?.length || 0;
}
}
32 changes: 18 additions & 14 deletions src/common/helper.ts
Expand Up @@ -134,7 +134,9 @@ async function waitForEvent<T extends any>(
timeout: number,
abortPromise: Promise<Error>
): Promise<T> {
let eventTimeout, resolveCallback, rejectCallback;
let eventTimeout: NodeJS.Timeout;
let resolveCallback: (value: T | PromiseLike<T>) => void;
let rejectCallback: (value: Error) => void;
const promise = new Promise<T>((resolve, reject) => {
resolveCallback = resolve;
rejectCallback = reject;
Expand Down Expand Up @@ -192,7 +194,7 @@ function pageBindingInitString(type: string, name: string): string {
const binding = win[bindingName];

win[bindingName] = (...args: unknown[]): Promise<unknown> => {
const me = window[bindingName];
const me = (window as any)[bindingName];
let callbacks = me.callbacks;
if (!callbacks) {
callbacks = new Map();
Expand All @@ -216,8 +218,8 @@ function pageBindingDeliverResultString(
result: unknown
): string {
function deliverResult(name: string, seq: number, result: unknown): void {
window[name].callbacks.get(seq).resolve(result);
window[name].callbacks.delete(seq);
(window as any)[name].callbacks.get(seq).resolve(result);
(window as any)[name].callbacks.delete(seq);
}
return evaluationString(deliverResult, name, seq, result);
}
Expand All @@ -236,8 +238,8 @@ function pageBindingDeliverErrorString(
): void {
const error = new Error(message);
error.stack = stack;
window[name].callbacks.get(seq).reject(error);
window[name].callbacks.delete(seq);
(window as any)[name].callbacks.get(seq).reject(error);
(window as any)[name].callbacks.delete(seq);
}
return evaluationString(deliverError, name, seq, message, stack);
}
Expand All @@ -248,8 +250,8 @@ function pageBindingDeliverErrorValueString(
value: unknown
): string {
function deliverErrorValue(name: string, seq: number, value: unknown): void {
window[name].callbacks.get(seq).reject(value);
window[name].callbacks.delete(seq);
(window as any)[name].callbacks.get(seq).reject(value);
(window as any)[name].callbacks.delete(seq);
}
return evaluationString(deliverErrorValue, name, seq, value);
}
Expand All @@ -266,7 +268,9 @@ function makePredicateString(
if (!node) return waitForHidden;
if (!waitForVisible && !waitForHidden) return node;
const element =
node.nodeType === Node.TEXT_NODE ? node.parentElement : (node as Element);
node.nodeType === Node.TEXT_NODE
? (node.parentElement as Element)
: (node as Element);

const style = window.getComputedStyle(element);
const isVisible =
Expand Down Expand Up @@ -296,7 +300,7 @@ async function waitWithTimeout<T extends any>(
taskName: string,
timeout: number
): Promise<T> {
let reject;
let reject: (reason?: Error) => void;
const timeoutError = new TimeoutError(
`waiting for ${taskName} failed: timeout ${timeout}ms exceeded`
);
Expand All @@ -313,27 +317,27 @@ async function waitWithTimeout<T extends any>(
async function getReadableAsBuffer(
readable: Readable,
path?: string
): Promise<Buffer> {
): Promise<Buffer | null> {
if (!isNode && path) {
throw new Error('Cannot write to a path outside of Node.js environment.');
}

const fs = isNode ? await importFSModule() : null;

let fileHandle: import('fs').promises.FileHandle;
let fileHandle: import('fs').promises.FileHandle | undefined;

if (path && fs) {
fileHandle = await fs.promises.open(path, 'w');
}
const buffers = [];
for await (const chunk of readable) {
buffers.push(chunk);
if (fileHandle) {
if (fileHandle && fs) {
await fs.promises.writeFile(fileHandle, chunk);
}
}

if (path) await fileHandle.close();
if (path && fileHandle) await fileHandle.close();
let resultBuffer = null;
try {
resultBuffer = Buffer.concat(buffers);
Expand Down