From 17a79870957670f4d09785244b1d2a9cc6782c0c Mon Sep 17 00:00:00 2001 From: Jesse Himmelstein Date: Thu, 12 Sep 2024 12:14:06 +0200 Subject: [PATCH] Fixed Prettier version --- .prettierrc | 1 + package.json | 3 +- src/chip.ts | 95 ++++++++++++++++++++++++++------------------------ src/running.ts | 16 ++++----- yarn.lock | 5 +++ 5 files changed, 65 insertions(+), 55 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/.prettierrc @@ -0,0 +1 @@ +{} diff --git a/package.json b/package.json index 79e0c0a..3de053d 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "checkLinting": "yarn eslint", "checkFormatting": "yarn prettier --check ./src", "checkAll": "yarn checkTypes && yarn checkLinting && yarn checkFormatting", - "prettier": "npx prettier ./src --write", + "format": "yarn prettier --write ./src ", "docs": "yarn typedoc" }, "license": "MIT", @@ -30,6 +30,7 @@ "eslint-config-prettier": "^9.0.0", "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", + "prettier": "3.3.3", "ts-jest": "^29.1.1", "typedoc": "^0.25.7" } diff --git a/src/chip.ts b/src/chip.ts index fdd106d..002fbad 100644 --- a/src/chip.ts +++ b/src/chip.ts @@ -9,7 +9,7 @@ import * as _ from "underscore"; // @es-li export function fillInOptions( options: Partial | unknown, - defaults: T + defaults: T, ): T { if (options) return { ...defaults, ...(options as object) }; else return defaults; @@ -41,7 +41,7 @@ export function isEventTarget(emitter: object): emitter is EventTarget { export type UnsubscribeFunction = ( emitter: object, event: string, - cb: () => void + cb: () => void, ) => void; export interface SubscriptionHandler { @@ -110,7 +110,7 @@ export type SignalResolvable = Signal | SignalParams | string; export function makeSignal( name = "default", - params: SignalParams = {} + params: SignalParams = {}, ): Signal { return { name, params }; } @@ -169,7 +169,7 @@ export interface TickInfo { */ export type ChipFactory = ( context: ChipContext, - signal: Signal + signal: Signal, ) => Chip | undefined; export type ChipResolvable = Chip | ChipFactory; @@ -192,7 +192,7 @@ export function isChip(e: any): e is Chip { } export function isChipResolvable( - e: ChipResolvable | ActivateChildChipOptions + e: ChipResolvable | ActivateChildChipOptions, ): e is ChipResolvable { return typeof e === "function" || isChip(e); } @@ -236,7 +236,7 @@ export interface Chip extends NodeEventSource { tickInfo: TickInfo, chipContext: ChipContext, inputSignal: Signal, - reloadMemento?: ReloadMemento + reloadMemento?: ReloadMemento, ): void; /** Update the chip, provided a new time */ @@ -287,7 +287,7 @@ export abstract class ChipBase extends EventEmitter implements Chip { tickInfo: TickInfo, chipContext: ChipContext, inputSignal: Signal, - reloadMemento?: ReloadMemento + reloadMemento?: ReloadMemento, ): void { if (this._chipState !== "inactive") throw new Error(`activate() called from state ${this._chipState}`); @@ -318,7 +318,7 @@ export abstract class ChipBase extends EventEmitter implements Chip { public terminate( tickInfo: TickInfo, - outputSignal: Signal = makeSignal() + outputSignal: Signal = makeSignal(), ): void { if (!this.isInChipState("active", "paused", "requestedTermination")) throw new Error(`terminate() called from state ${this._chipState}`); @@ -341,7 +341,7 @@ export abstract class ChipBase extends EventEmitter implements Chip { protected _terminateSelf(signal?: SignalResolvable) { if (this._chipState !== "active" && this._chipState !== "paused") { console.warn( - `_terminateSelf() called from state ${this._chipState}. Ignoring...` + `_terminateSelf() called from state ${this._chipState}. Ignoring...`, ); return; } @@ -380,7 +380,7 @@ export abstract class ChipBase extends EventEmitter implements Chip { event: string, // eslint-disable-next-line @typescript-eslint/no-explicit-any cb: (...args: any[]) => void, - subscriptionHandler?: SubscriptionHandler + subscriptionHandler?: SubscriptionHandler, ): void { if (!subscriptionHandler) { if (isNodeEventSource(emitter)) { @@ -389,7 +389,7 @@ export abstract class ChipBase extends EventEmitter implements Chip { subscriptionHandler = new EventTargetSubscriptionHandler(); } else { throw new Error( - `Emitter is of unknown type "${typeof emitter}", requires custom SubscriptionHandler` + `Emitter is of unknown type "${typeof emitter}", requires custom SubscriptionHandler`, ); } } @@ -419,7 +419,7 @@ export abstract class ChipBase extends EventEmitter implements Chip { event: string, // eslint-disable-next-line @typescript-eslint/no-explicit-any cb: (...args: any[]) => void, - subscriptionHandler?: SubscriptionHandler + subscriptionHandler?: SubscriptionHandler, ): void { if (!subscriptionHandler) { if (isNodeEventSource(emitter)) { @@ -428,7 +428,7 @@ export abstract class ChipBase extends EventEmitter implements Chip { subscriptionHandler = new EventTargetSubscriptionHandler(); } else { throw new Error( - `Emitter is of unknown type "${typeof emitter}", requires custom SubscriptionHandler` + `Emitter is of unknown type "${typeof emitter}", requires custom SubscriptionHandler`, ); } } @@ -452,7 +452,7 @@ export abstract class ChipBase extends EventEmitter implements Chip { protected _unsubscribe( emitter?: object, event?: string, - cb?: (...args: unknown[]) => void + cb?: (...args: unknown[]) => void, ): void { // props should only contain defined arguments const props = _.pick( @@ -461,18 +461,18 @@ export abstract class ChipBase extends EventEmitter implements Chip { event, cb, }, - (v) => !!v + (v) => !!v, ); const [listenersToRemove, listenersToKeep] = _.partition( this._eventListeners, - props + props, ); for (const listener of listenersToRemove) listener.subscriptionHandler.unsubscribe( listener.emitter, listener.event, - listener.boundCb + listener.boundCb, ); this._eventListeners = listenersToKeep; @@ -493,7 +493,7 @@ export abstract class ChipBase extends EventEmitter implements Chip { public makeReloadMemento(): ReloadMemento { if (this._chipState !== "active" && this._chipState !== "paused") throw new Error( - `makeReloadMemento() called from state ${this._chipState}` + `makeReloadMemento() called from state ${this._chipState}`, ); const childMementos: Record = {}; @@ -638,7 +638,7 @@ export abstract class Composite extends ChipBase { tickInfo: TickInfo, chipContext: ChipContext, inputSignal: Signal, - reloadMemento?: ReloadMemento + reloadMemento?: ReloadMemento, ): void { this._childChipInfos = {}; this._childChipContext = {}; @@ -715,7 +715,7 @@ export abstract class Composite extends ChipBase { */ protected _activateChildChip( chipResolvable: ChipResolvable, - options?: Omit + options?: Omit, ): CompositeChildChipInfo; /** * Activate a child chip @@ -723,11 +723,11 @@ export abstract class Composite extends ChipBase { * @returns The activated chip */ protected _activateChildChip( - options: ActivateChildChipOptions + options: ActivateChildChipOptions, ): CompositeChildChipInfo; protected _activateChildChip( chipOrOptions: ChipResolvable | ActivateChildChipOptions, - options?: Omit + options?: Omit, ): CompositeChildChipInfo { if (this.chipState !== "active") throw new Error("Composite is not active"); @@ -760,7 +760,7 @@ export abstract class Composite extends ChipBase { options.attribute } would replace a non-chip. Current attribute value = ${ thisAsAny[options.attribute] - }` + }`, ); const existingChip = thisAsAny[options.attribute] as Chip; @@ -787,21 +787,21 @@ export abstract class Composite extends ChipBase { const childContextExtensions = Object.values(this._childChipInfos).map( (childChipInfo) => childChipInfo.extendChildContext && - childChipInfo.chip.contextModification + childChipInfo.chip.contextModification, ); childChipInfo.context = processChipContext( this._chipContext, this._childChipContext, ...childContextExtensions, this.contextModification, - options.context + options.context, ); } if (_.isFunction(chipResolvable)) { childChipInfo.chip = chipResolvable( childChipInfo.context, - childChipInfo.inputSignal + childChipInfo.inputSignal, ); } else { childChipInfo.chip = chipResolvable; @@ -847,7 +847,7 @@ export abstract class Composite extends ChipBase { if (childChipInfo.includeInChildContext) { if (!providedId) throw new Error( - "To include a child chip in the context, provide an attribute name or ID" + "To include a child chip in the context, provide an attribute name or ID", ); this._childChipContext[providedId] = childChipInfo.chip; @@ -861,7 +861,7 @@ export abstract class Composite extends ChipBase { this._lastTickInfo, childChipInfo.context, childChipInfo.inputSignal, - childChipInfo.reloadMemento + childChipInfo.reloadMemento, ); this.emit("activatedChildChip", childChipInfo); @@ -872,7 +872,7 @@ export abstract class Composite extends ChipBase { /** Terminate the child with the given signal */ protected _terminateChildChip( chipOrId: Chip | string, - outputSignal?: SignalResolvable + outputSignal?: SignalResolvable, ): void { if (this.chipState === "inactive") throw new Error("Composite is inactive"); @@ -888,7 +888,7 @@ export abstract class Composite extends ChipBase { childChipInfo.chip.terminate( this._lastTickInfo, - resolveSignal(outputSignal) + resolveSignal(outputSignal), ); delete this._childChipInfos[childChipInfo.id]; @@ -913,7 +913,7 @@ export abstract class Composite extends ChipBase { this.emit( "terminatedChildChip", childChipInfo, - childChipInfo.chip.outputSignal + childChipInfo.chip.outputSignal, ); } @@ -923,7 +923,7 @@ export abstract class Composite extends ChipBase { */ protected _tickChildChips(): void { for (const [childId, childChipInfo] of Object.entries( - this._childChipInfos + this._childChipInfos, )) { if (childChipInfo.chip.chipState === "requestedTermination") { this._terminateChildChip(childId); @@ -981,7 +981,7 @@ export abstract class Composite extends ChipBase { /** Terminate any child chips that requested it */ protected _terminateRequestedChildChips() { for (const [childId, childChipInfo] of Object.entries( - this._childChipInfos + this._childChipInfos, )) { if (childChipInfo.chip.chipState === "requestedTermination") { this._terminateChildChip(childId); @@ -1007,7 +1007,7 @@ export class Parallel extends Composite { constructor( childChipOptions: Array, - options?: Partial + options?: Partial, ) { super(); @@ -1102,7 +1102,7 @@ export class Parallel extends Composite { } indexOfChipActivationInfo( - chip: ActivateChildChipOptions | ChipResolvable + chip: ActivateChildChipOptions | ChipResolvable, ): number { if (isChipResolvable(chip)) { return this._childChipOptions.findIndex((x) => x.chip === chip); @@ -1115,7 +1115,7 @@ export class Parallel extends Composite { export class ContextProvider extends Composite { constructor( private readonly _context: Record, - private readonly _child: ChipResolvable + private readonly _child: ChipResolvable, ) { super(); } @@ -1158,7 +1158,7 @@ export class Sequence extends Composite { constructor( childChipOptions: Array, - options?: Partial + options?: Partial, ) { super(); @@ -1201,7 +1201,7 @@ export class Sequence extends Composite { // Copy chip activation info and optionally extend it const info = Object.assign( {}, - this._childChipOptions[this._currentChipIndex] + this._childChipOptions[this._currentChipIndex], ); if (signal) info.inputSignal = signal; @@ -1300,7 +1300,7 @@ export type StateTableDescriptor = { export type SignalFunction = ( context: ChipContext, - signal: Signal + signal: Signal, ) => Signal | string; export type SignalDescriptor = Signal | SignalFunction; export type SignalTable = { [name: string]: SignalDescriptor }; @@ -1338,7 +1338,7 @@ export class StateMachine extends Composite { constructor( states: StateTableDescriptor, - options?: Partial + options?: Partial, ) { super(); @@ -1373,7 +1373,7 @@ export class StateMachine extends Composite { tickInfo: TickInfo, chipContext: ChipContext, inputSignal?: Signal, - reloadMemento?: ReloadMemento + reloadMemento?: ReloadMemento, ) { super.activate(tickInfo, chipContext, inputSignal, reloadMemento); @@ -1411,7 +1411,7 @@ export class StateMachine extends Composite { this._transitions[this._lastSignal.name]; if (_.isFunction(signalDescriptor)) { nextStateDescriptor = resolveSignal( - signalDescriptor(this._chipContext, signal) + signalDescriptor(this._chipContext, signal), ); } else { nextStateDescriptor = signalDescriptor; @@ -1612,7 +1612,10 @@ export class Functional extends Composite { Optionally takes a @that parameter, which is set as _this_ during the call. */ export class Lambda extends ChipBase { - constructor(public f: (arg: unknown) => unknown, public that?: unknown) { + constructor( + public f: (arg: unknown) => unknown, + public that?: unknown, + ) { super(); this.that = that || this; } @@ -1689,7 +1692,7 @@ export class WaitForEvent extends ChipBase { constructor( public emitter: NodeEventSource, public eventName: string, - public handler: (...args: unknown[]) => Signal | boolean = _.constant(true) + public handler: (...args: unknown[]) => Signal | boolean = _.constant(true), ) { super(); } @@ -1729,7 +1732,7 @@ export class Alternative extends Composite { constructor( childChipOptions: Array< ChipResolvable | AlternativeActivateChildChipOptions - > + >, ) { super(); @@ -1758,7 +1761,7 @@ export class Alternative extends Composite { if (childChipInfo !== chipInfo) return; this._onChildTerminated(i, outputSignal); - } + }, ); } } diff --git a/src/running.ts b/src/running.ts index 72a6c93..55e6ab4 100644 --- a/src/running.ts +++ b/src/running.ts @@ -43,7 +43,7 @@ export class Runner { */ constructor( private readonly _rootChipResolvable: chip.ChipResolvable, - options?: Partial + options?: Partial, ) { this._options = chip.fillInOptions(options, new RunnerOptions()); } @@ -91,7 +91,7 @@ export class Runner { }; document.addEventListener( "visibilitychange", - this._visibilityChangeHandler + this._visibilityChangeHandler, ); this._runningStatus = "running"; @@ -108,14 +108,14 @@ export class Runner { this._rootChip.activate( tickInfo, this._rootContext, - this._options.inputSignal + this._options.inputSignal, ); this._requestUpdate(); this._backupTimerId = window.setInterval( this._onBackupTimer.bind(this), - this._options.backupTimerInterval + this._options.backupTimerInterval, ); if (this._options.hmr) this._enableHotReloading(); @@ -126,7 +126,7 @@ export class Runner { const timeStamp = performance.now(); const timeSinceLastTick = this._clampTimeSinceLastTick( - timeStamp - this._lastTimeStamp + timeStamp - this._lastTimeStamp, ); const tickInfo: chip.TickInfo = { @@ -138,7 +138,7 @@ export class Runner { document.removeEventListener( "visibilitychange", - this._visibilityChangeHandler + this._visibilityChangeHandler, ); delete this._visibilityChangeHandler; @@ -191,7 +191,7 @@ export class Runner { tickInfo, this._rootContext, chip.makeSignal("afterReload"), - reloadMemento + reloadMemento, ); }); } @@ -211,7 +211,7 @@ export class Runner { if (this._options.minFps >= 0) { timeSinceLastTick = Math.min( timeSinceLastTick, - 1000 / this._options.minFps + 1000 / this._options.minFps, ); } diff --git a/yarn.lock b/yarn.lock index fb47d6c..e703adf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2516,6 +2516,11 @@ prelude-ls@^1.2.1: resolved "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz" integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g== +prettier@3.3.3: + version "3.3.3" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.3.tgz#30c54fe0be0d8d12e6ae61dbb10109ea00d53105" + integrity sha512-i2tDNA0O5IrMO757lfrdQZCc2jPNDVntV0m/+4whiDfWaTKfMNgR7Qz0NAeGz/nRqF4m5/6CLzbP4/liHt12Ew== + pretty-format@^29.7.0: version "29.7.0" resolved "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz"