Skip to content

Commit

Permalink
chore: [#0] Improve type accuracy, strict mode compatibility and JSDo…
Browse files Browse the repository at this point in the history
…c documentation (#1677)
  • Loading branch information
wojtekmaj authored Jan 8, 2025
1 parent 8bf1f26 commit 3218bf9
Show file tree
Hide file tree
Showing 46 changed files with 258 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class AsyncTaskManager {
private runningTaskCount = 0;
private runningTimers: NodeJS.Timeout[] = [];
private runningImmediates: NodeJS.Immediate[] = [];
private waitUntilCompleteTimer: NodeJS.Immediate | null = null;
private waitUntilCompleteTimer: NodeJS.Timeout | null = null;
private waitUntilCompleteResolvers: Array<() => void> = [];
private aborted = false;
private destroyed = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default class DetachedBrowser implements IBrowser {
await Promise.all(this.contexts.slice().map((context) => context.close()));
(<DetachedBrowserContext[]>this.contexts) = [];
(<Console | null>this.console) = null;
(<new (browserFrame: IBrowserFrame) => BrowserWindow | null>this.windowClass) = null;
(<(new (browserFrame: IBrowserFrame) => BrowserWindow) | null>this.windowClass) = null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import BrowserWindow from '../../window/BrowserWindow.js';
export default class BrowserExceptionObserver {
private static listenerCount = 0;
private observedWindows: BrowserWindow[] = [];
private uncaughtExceptionListener: (
error: Error,
origin: 'uncaughtException' | 'unhandledRejection'
) => void | null;
private uncaughtRejectionListener: (error: Error) => void | null;
private uncaughtExceptionListener:
| ((error: Error, origin: 'uncaughtException' | 'unhandledRejection') => void)
| null = null;
private uncaughtRejectionListener: ((error: Error) => void) | null = null;

/**
* Observes the Node process for uncaught exceptions.
Expand Down Expand Up @@ -117,7 +116,9 @@ export default class BrowserExceptionObserver {
if (this.observedWindows.length === 0 && this.uncaughtExceptionListener) {
(<typeof BrowserExceptionObserver>this.constructor).listenerCount--;
process.off('uncaughtException', this.uncaughtExceptionListener);
process.off('unhandledRejection', this.uncaughtRejectionListener);
if (this.uncaughtRejectionListener) {
process.off('unhandledRejection', this.uncaughtRejectionListener);
}
this.uncaughtExceptionListener = null;
this.uncaughtRejectionListener = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export default class BrowserFrameNavigator {
windowClass: new (
browserFrame: IBrowserFrame,
options?: { url?: string; width?: number; height?: number }
) => BrowserWindow;
) => BrowserWindow | null;
frame: IBrowserFrame;
url: string;
goToOptions?: IGoToOptions;
method?: string;
formData?: FormData;
formData?: FormData | null;
disableHistory?: boolean;
}): Promise<Response | null> {
const { windowClass, frame, url, formData, method, goToOptions, disableHistory } = options;
Expand Down Expand Up @@ -235,7 +235,7 @@ export default class BrowserFrameNavigator {

// The frame may be destroyed during teardown.
if (!frame.window) {
return;
return null;
}

// Fixes issue where evaluating the response can throw an error.
Expand Down Expand Up @@ -265,7 +265,7 @@ export default class BrowserFrameNavigator {
windowClass: new (
browserFrame: IBrowserFrame,
options?: { url?: string; width?: number; height?: number }
) => BrowserWindow;
) => BrowserWindow | null;
frame: IBrowserFrame;
goToOptions?: IGoToOptions;
}): Promise<Response | null> {
Expand Down Expand Up @@ -324,7 +324,7 @@ export default class BrowserFrameNavigator {
windowClass: new (
browserFrame: IBrowserFrame,
options?: { url?: string; width?: number; height?: number }
) => BrowserWindow;
) => BrowserWindow | null;
frame: IBrowserFrame;
goToOptions?: IGoToOptions;
}): Promise<Response | null> {
Expand Down Expand Up @@ -384,7 +384,7 @@ export default class BrowserFrameNavigator {
windowClass: new (
browserFrame: IBrowserFrame,
options?: { url?: string; width?: number; height?: number }
) => BrowserWindow;
) => BrowserWindow | null;
frame: IBrowserFrame;
goToOptions?: IGoToOptions;
steps?: number;
Expand Down Expand Up @@ -448,7 +448,7 @@ export default class BrowserFrameNavigator {
windowClass: new (
browserFrame: IBrowserFrame,
options?: { url?: string; width?: number; height?: number }
) => BrowserWindow;
) => BrowserWindow | null;
frame: IBrowserFrame;
goToOptions?: IGoToOptions;
}): Promise<Response | null> {
Expand Down
45 changes: 28 additions & 17 deletions packages/happy-dom/src/console/VirtualConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ export default class VirtualConsole implements Console {
* Writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.
*
* @param assertion Assertion.
* @param message Message.
* @param args Arguments.
*/
public assert(assertion: boolean, ...args: Array<object | string>): void {
public assert(assertion: boolean, message?: any, ...args: Array<object | string>): void {
if (!assertion) {
this.#printer.print({
type: VirtualConsoleLogTypeEnum.assert,
level: VirtualConsoleLogLevelEnum.error,
message: ['Assertion failed:', ...args],
message: ['Assertion failed:', ...(message ? [message, ...args] : args)],
group: this.#groups[this.#groups.length - 1] || null
});
}
Expand Down Expand Up @@ -92,13 +93,14 @@ export default class VirtualConsole implements Console {
/**
* Outputs a message to the web console at the "debug" log level.
*
* @param message Message.
* @param args Arguments.
*/
public debug(...args: Array<object | string>): void {
public debug(message?: any, ...args: Array<object | string>): void {
this.#printer.print({
type: VirtualConsoleLogTypeEnum.debug,
level: VirtualConsoleLogLevelEnum.log,
message: args,
message: message ? [message, ...args] : args,
group: this.#groups[this.#groups.length - 1] || null
});
}
Expand All @@ -108,7 +110,7 @@ export default class VirtualConsole implements Console {
*
* @param data Data.
*/
public dir(data: object): void {
public dir(data: any): void {
this.#printer.print({
type: VirtualConsoleLogTypeEnum.dir,
level: VirtualConsoleLogLevelEnum.log,
Expand All @@ -122,7 +124,7 @@ export default class VirtualConsole implements Console {
*
* @param data Data.
*/
public dirxml(data: object): void {
public dirxml(data: any[]): void {
this.#printer.print({
type: VirtualConsoleLogTypeEnum.dirxml,
level: VirtualConsoleLogLevelEnum.log,
Expand All @@ -134,13 +136,14 @@ export default class VirtualConsole implements Console {
/**
* Outputs an error message to the console.
*
* @param message Message.
* @param args Arguments.
*/
public error(...args: Array<object | string>): void {
public error(message?: any, ...args: Array<object | string>): void {
this.#printer.print({
type: VirtualConsoleLogTypeEnum.error,
level: VirtualConsoleLogLevelEnum.error,
message: args,
message: message ? [message, ...args] : args,
group: this.#groups[this.#groups.length - 1] || null
});
}
Expand Down Expand Up @@ -211,28 +214,31 @@ export default class VirtualConsole implements Console {
}

/**
* Outputs an informational message to the console.
*
* @param args
* @param message Message.
* @param args Arguments.
*/
public info(...args: Array<object | string>): void {
public info(message?: any, ...args: Array<object | string>): void {
this.#printer.print({
type: VirtualConsoleLogTypeEnum.info,
level: VirtualConsoleLogLevelEnum.info,
message: args,
message: message ? [message, ...args] : args,
group: this.#groups[this.#groups.length - 1] || null
});
}

/**
* Outputs a message to the console.
*
* @param message Message.
* @param args Arguments.
*/
public log(...args: Array<object | string>): void {
public log(message?: any, ...args: Array<object | string>): void {
this.#printer.print({
type: VirtualConsoleLogTypeEnum.log,
level: VirtualConsoleLogLevelEnum.log,
message: args,
message: message ? [message, ...args] : args,
group: this.#groups[this.#groups.length - 1] || null
});
}
Expand Down Expand Up @@ -329,27 +335,32 @@ export default class VirtualConsole implements Console {
/**
* Outputs a stack trace to the console.
*
* @param message Message.
* @param args Arguments.
*/
public trace(...args: Array<object | string>): void {
public trace(message?: any, ...args: Array<object | string>): void {
this.#printer.print({
type: VirtualConsoleLogTypeEnum.trace,
level: VirtualConsoleLogLevelEnum.log,
message: [...args, new Error('stack').stack.replace('Error: stack', '')],
message: [
...(message ? [message, ...args] : args),
new Error('stack').stack!.replace('Error: stack', '')
],
group: this.#groups[this.#groups.length - 1] || null
});
}

/**
* Outputs a warning message to the console.
*
* @param message Message.
* @param args Arguments.
*/
public warn(...args: Array<object | string>): void {
public warn(message?: any, ...args: Array<object | string>): void {
this.#printer.print({
type: VirtualConsoleLogTypeEnum.warn,
level: VirtualConsoleLogLevelEnum.warn,
message: args,
message: message ? [message, ...args] : args,
group: this.#groups[this.#groups.length - 1] || null
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class CookieExpireUtility {
* @param cookie Cookie.
* @returns "true" if cookie has expired.
*/
public static hasExpired(cookie: ICookie): boolean {
public static hasExpired(cookie: ICookie): boolean | null {
return cookie.expires && cookie.expires.getTime() < Date.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ export default class CSSStyleDeclarationComputedStyle {
private parseCSSRules(options: {
cssRules: CSSRule[];
elements: IStyleAndElement[];
rootElement?: IStyleAndElement;
hostElement?: IStyleAndElement;
rootElement?: IStyleAndElement | null;
hostElement?: IStyleAndElement | null;
}): void {
if (!options.elements.length) {
return;
Expand Down
Loading

0 comments on commit 3218bf9

Please sign in to comment.