From 28415a82d903fc191d1571beb90e8c01b2aacf82 Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Wed, 6 Apr 2022 22:28:19 +0200 Subject: [PATCH] Removed private fields using new # syntax due to issues --- CHANGELOG.md | 3 +- src/openApi/v2/parser/getEnum.ts | 2 +- src/openApi/v3/parser/getEnum.ts | 2 +- src/templates/core/CancelablePromise.hbs | 70 ++++++------ test/__snapshots__/index.spec.ts.snap | 140 +++++++++++------------ 5 files changed, 109 insertions(+), 108 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index decee2b1..4bb88bfd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,11 @@ # Changelog All notable changes to this project will be documented in this file. -## [0.20.2] - 2022-02-25 +## [0.21.0] - 2022-04-06 ### Fixed - Return `undefined` to match `noImplicitReturns` rule - Made `BaseHttpRequest` class abstract +- Removed private fields using `#` inside `CancelablePromise` - Filter out wrong enum values ## [0.20.1] - 2022-02-25 diff --git a/src/openApi/v2/parser/getEnum.ts b/src/openApi/v2/parser/getEnum.ts index 5569afeb..64c7ca8b 100644 --- a/src/openApi/v2/parser/getEnum.ts +++ b/src/openApi/v2/parser/getEnum.ts @@ -24,7 +24,7 @@ export const getEnum = (values?: (string | number)[]): Enum[] => { .replace(/^(\d+)/g, '_$1') .replace(/([a-z])([A-Z]+)/g, '$1_$2') .toUpperCase(), - value: `'${value.replace(/'/g, '\\\'')}'`, + value: `'${value.replace(/'/g, "\\'")}'`, type: 'string', description: null, }; diff --git a/src/openApi/v3/parser/getEnum.ts b/src/openApi/v3/parser/getEnum.ts index 5569afeb..64c7ca8b 100644 --- a/src/openApi/v3/parser/getEnum.ts +++ b/src/openApi/v3/parser/getEnum.ts @@ -24,7 +24,7 @@ export const getEnum = (values?: (string | number)[]): Enum[] => { .replace(/^(\d+)/g, '_$1') .replace(/([a-z])([A-Z]+)/g, '$1_$2') .toUpperCase(), - value: `'${value.replace(/'/g, '\\\'')}'`, + value: `'${value.replace(/'/g, "\\'")}'`, type: 'string', description: null, }; diff --git a/src/templates/core/CancelablePromise.hbs b/src/templates/core/CancelablePromise.hbs index 67a92a67..09765790 100644 --- a/src/templates/core/CancelablePromise.hbs +++ b/src/templates/core/CancelablePromise.hbs @@ -23,13 +23,13 @@ export interface OnCancel { export class CancelablePromise implements Promise { readonly [Symbol.toStringTag]: string; - #isResolved: boolean; - #isRejected: boolean; - #isCancelled: boolean; - readonly #cancelHandlers: (() => void)[]; - readonly #promise: Promise; - #resolve?: (value: T | PromiseLike) => void; - #reject?: (reason?: any) => void; + private _isResolved: boolean; + private _isRejected: boolean; + private _isCancelled: boolean; + private readonly _cancelHandlers: (() => void)[]; + private readonly _promise: Promise; + private _resolve?: (value: T | PromiseLike) => void; + private _reject?: (reason?: any) => void; constructor( executor: ( @@ -38,47 +38,47 @@ export class CancelablePromise implements Promise { onCancel: OnCancel ) => void ) { - this.#isResolved = false; - this.#isRejected = false; - this.#isCancelled = false; - this.#cancelHandlers = []; - this.#promise = new Promise((resolve, reject) => { - this.#resolve = resolve; - this.#reject = reject; + this._isResolved = false; + this._isRejected = false; + this._isCancelled = false; + this._cancelHandlers = []; + this._promise = new Promise((resolve, reject) => { + this._resolve = resolve; + this._reject = reject; const onResolve = (value: T | PromiseLike): void => { - if (this.#isResolved || this.#isRejected || this.#isCancelled) { + if (this._isResolved || this._isRejected || this._isCancelled) { return; } - this.#isResolved = true; - this.#resolve?.(value); + this._isResolved = true; + this._resolve?.(value); }; const onReject = (reason?: any): void => { - if (this.#isResolved || this.#isRejected || this.#isCancelled) { + if (this._isResolved || this._isRejected || this._isCancelled) { return; } - this.#isRejected = true; - this.#reject?.(reason); + this._isRejected = true; + this._reject?.(reason); }; const onCancel = (cancelHandler: () => void): void => { - if (this.#isResolved || this.#isRejected || this.#isCancelled) { + if (this._isResolved || this._isRejected || this._isCancelled) { return; } - this.#cancelHandlers.push(cancelHandler); + this._cancelHandlers.push(cancelHandler); }; Object.defineProperty(onCancel, 'isResolved', { - get: (): boolean => this.#isResolved, + get: (): boolean => this._isResolved, }); Object.defineProperty(onCancel, 'isRejected', { - get: (): boolean => this.#isRejected, + get: (): boolean => this._isRejected, }); Object.defineProperty(onCancel, 'isCancelled', { - get: (): boolean => this.#isCancelled, + get: (): boolean => this._isCancelled, }); return executor(onResolve, onReject, onCancel as OnCancel); @@ -89,27 +89,27 @@ export class CancelablePromise implements Promise { onFulfilled?: ((value: T) => TResult1 | PromiseLike) | null, onRejected?: ((reason: any) => TResult2 | PromiseLike) | null ): Promise { - return this.#promise.then(onFulfilled, onRejected); + return this._promise.then(onFulfilled, onRejected); } public catch( onRejected?: ((reason: any) => TResult | PromiseLike) | null ): Promise { - return this.#promise.catch(onRejected); + return this._promise.catch(onRejected); } public finally(onFinally?: (() => void) | null): Promise { - return this.#promise.finally(onFinally); + return this._promise.finally(onFinally); } public cancel(): void { - if (this.#isResolved || this.#isRejected || this.#isCancelled) { + if (this._isResolved || this._isRejected || this._isCancelled) { return; } - this.#isCancelled = true; - if (this.#cancelHandlers.length) { + this._isCancelled = true; + if (this._cancelHandlers.length) { try { - for (const cancelHandler of this.#cancelHandlers) { + for (const cancelHandler of this._cancelHandlers) { cancelHandler(); } } catch (error) { @@ -117,11 +117,11 @@ export class CancelablePromise implements Promise { return; } } - this.#cancelHandlers.length = 0; - this.#reject?.(new CancelError('Request aborted')); + this._cancelHandlers.length = 0; + this._reject?.(new CancelError('Request aborted')); } public get isCancelled(): boolean { - return this.#isCancelled; + return this._isCancelled; } } diff --git a/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap index 227b8db5..497f5c25 100644 --- a/test/__snapshots__/index.spec.ts.snap +++ b/test/__snapshots__/index.spec.ts.snap @@ -83,13 +83,13 @@ export interface OnCancel { export class CancelablePromise implements Promise { readonly [Symbol.toStringTag]: string; - #isResolved: boolean; - #isRejected: boolean; - #isCancelled: boolean; - readonly #cancelHandlers: (() => void)[]; - readonly #promise: Promise; - #resolve?: (value: T | PromiseLike) => void; - #reject?: (reason?: any) => void; + private _isResolved: boolean; + private _isRejected: boolean; + private _isCancelled: boolean; + private readonly _cancelHandlers: (() => void)[]; + private readonly _promise: Promise; + private _resolve?: (value: T | PromiseLike) => void; + private _reject?: (reason?: any) => void; constructor( executor: ( @@ -98,47 +98,47 @@ export class CancelablePromise implements Promise { onCancel: OnCancel ) => void ) { - this.#isResolved = false; - this.#isRejected = false; - this.#isCancelled = false; - this.#cancelHandlers = []; - this.#promise = new Promise((resolve, reject) => { - this.#resolve = resolve; - this.#reject = reject; + this._isResolved = false; + this._isRejected = false; + this._isCancelled = false; + this._cancelHandlers = []; + this._promise = new Promise((resolve, reject) => { + this._resolve = resolve; + this._reject = reject; const onResolve = (value: T | PromiseLike): void => { - if (this.#isResolved || this.#isRejected || this.#isCancelled) { + if (this._isResolved || this._isRejected || this._isCancelled) { return; } - this.#isResolved = true; - this.#resolve?.(value); + this._isResolved = true; + this._resolve?.(value); }; const onReject = (reason?: any): void => { - if (this.#isResolved || this.#isRejected || this.#isCancelled) { + if (this._isResolved || this._isRejected || this._isCancelled) { return; } - this.#isRejected = true; - this.#reject?.(reason); + this._isRejected = true; + this._reject?.(reason); }; const onCancel = (cancelHandler: () => void): void => { - if (this.#isResolved || this.#isRejected || this.#isCancelled) { + if (this._isResolved || this._isRejected || this._isCancelled) { return; } - this.#cancelHandlers.push(cancelHandler); + this._cancelHandlers.push(cancelHandler); }; Object.defineProperty(onCancel, 'isResolved', { - get: (): boolean => this.#isResolved, + get: (): boolean => this._isResolved, }); Object.defineProperty(onCancel, 'isRejected', { - get: (): boolean => this.#isRejected, + get: (): boolean => this._isRejected, }); Object.defineProperty(onCancel, 'isCancelled', { - get: (): boolean => this.#isCancelled, + get: (): boolean => this._isCancelled, }); return executor(onResolve, onReject, onCancel as OnCancel); @@ -149,27 +149,27 @@ export class CancelablePromise implements Promise { onFulfilled?: ((value: T) => TResult1 | PromiseLike) | null, onRejected?: ((reason: any) => TResult2 | PromiseLike) | null ): Promise { - return this.#promise.then(onFulfilled, onRejected); + return this._promise.then(onFulfilled, onRejected); } public catch( onRejected?: ((reason: any) => TResult | PromiseLike) | null ): Promise { - return this.#promise.catch(onRejected); + return this._promise.catch(onRejected); } public finally(onFinally?: (() => void) | null): Promise { - return this.#promise.finally(onFinally); + return this._promise.finally(onFinally); } public cancel(): void { - if (this.#isResolved || this.#isRejected || this.#isCancelled) { + if (this._isResolved || this._isRejected || this._isCancelled) { return; } - this.#isCancelled = true; - if (this.#cancelHandlers.length) { + this._isCancelled = true; + if (this._cancelHandlers.length) { try { - for (const cancelHandler of this.#cancelHandlers) { + for (const cancelHandler of this._cancelHandlers) { cancelHandler(); } } catch (error) { @@ -177,12 +177,12 @@ export class CancelablePromise implements Promise { return; } } - this.#cancelHandlers.length = 0; - this.#reject?.(new CancelError('Request aborted')); + this._cancelHandlers.length = 0; + this._reject?.(new CancelError('Request aborted')); } public get isCancelled(): boolean { - return this.#isCancelled; + return this._isCancelled; } }" `; @@ -3094,13 +3094,13 @@ export interface OnCancel { export class CancelablePromise implements Promise { readonly [Symbol.toStringTag]: string; - #isResolved: boolean; - #isRejected: boolean; - #isCancelled: boolean; - readonly #cancelHandlers: (() => void)[]; - readonly #promise: Promise; - #resolve?: (value: T | PromiseLike) => void; - #reject?: (reason?: any) => void; + private _isResolved: boolean; + private _isRejected: boolean; + private _isCancelled: boolean; + private readonly _cancelHandlers: (() => void)[]; + private readonly _promise: Promise; + private _resolve?: (value: T | PromiseLike) => void; + private _reject?: (reason?: any) => void; constructor( executor: ( @@ -3109,47 +3109,47 @@ export class CancelablePromise implements Promise { onCancel: OnCancel ) => void ) { - this.#isResolved = false; - this.#isRejected = false; - this.#isCancelled = false; - this.#cancelHandlers = []; - this.#promise = new Promise((resolve, reject) => { - this.#resolve = resolve; - this.#reject = reject; + this._isResolved = false; + this._isRejected = false; + this._isCancelled = false; + this._cancelHandlers = []; + this._promise = new Promise((resolve, reject) => { + this._resolve = resolve; + this._reject = reject; const onResolve = (value: T | PromiseLike): void => { - if (this.#isResolved || this.#isRejected || this.#isCancelled) { + if (this._isResolved || this._isRejected || this._isCancelled) { return; } - this.#isResolved = true; - this.#resolve?.(value); + this._isResolved = true; + this._resolve?.(value); }; const onReject = (reason?: any): void => { - if (this.#isResolved || this.#isRejected || this.#isCancelled) { + if (this._isResolved || this._isRejected || this._isCancelled) { return; } - this.#isRejected = true; - this.#reject?.(reason); + this._isRejected = true; + this._reject?.(reason); }; const onCancel = (cancelHandler: () => void): void => { - if (this.#isResolved || this.#isRejected || this.#isCancelled) { + if (this._isResolved || this._isRejected || this._isCancelled) { return; } - this.#cancelHandlers.push(cancelHandler); + this._cancelHandlers.push(cancelHandler); }; Object.defineProperty(onCancel, 'isResolved', { - get: (): boolean => this.#isResolved, + get: (): boolean => this._isResolved, }); Object.defineProperty(onCancel, 'isRejected', { - get: (): boolean => this.#isRejected, + get: (): boolean => this._isRejected, }); Object.defineProperty(onCancel, 'isCancelled', { - get: (): boolean => this.#isCancelled, + get: (): boolean => this._isCancelled, }); return executor(onResolve, onReject, onCancel as OnCancel); @@ -3160,27 +3160,27 @@ export class CancelablePromise implements Promise { onFulfilled?: ((value: T) => TResult1 | PromiseLike) | null, onRejected?: ((reason: any) => TResult2 | PromiseLike) | null ): Promise { - return this.#promise.then(onFulfilled, onRejected); + return this._promise.then(onFulfilled, onRejected); } public catch( onRejected?: ((reason: any) => TResult | PromiseLike) | null ): Promise { - return this.#promise.catch(onRejected); + return this._promise.catch(onRejected); } public finally(onFinally?: (() => void) | null): Promise { - return this.#promise.finally(onFinally); + return this._promise.finally(onFinally); } public cancel(): void { - if (this.#isResolved || this.#isRejected || this.#isCancelled) { + if (this._isResolved || this._isRejected || this._isCancelled) { return; } - this.#isCancelled = true; - if (this.#cancelHandlers.length) { + this._isCancelled = true; + if (this._cancelHandlers.length) { try { - for (const cancelHandler of this.#cancelHandlers) { + for (const cancelHandler of this._cancelHandlers) { cancelHandler(); } } catch (error) { @@ -3188,12 +3188,12 @@ export class CancelablePromise implements Promise { return; } } - this.#cancelHandlers.length = 0; - this.#reject?.(new CancelError('Request aborted')); + this._cancelHandlers.length = 0; + this._reject?.(new CancelError('Request aborted')); } public get isCancelled(): boolean { - return this.#isCancelled; + return this._isCancelled; } }" `;