From f6326cffe7c08ee5755c3c4f46a27458165b1553 Mon Sep 17 00:00:00 2001 From: Vooord Date: Tue, 7 Mar 2023 09:50:14 +0530 Subject: [PATCH 1/3] Changed `.prettierrc.json`: trailingComma: es5 -> all; printWidth: 80 -> 100; --- .prettierrc.json | 3 +- src/TestUtils.ts | 3 +- src/impl/FormulaCell.test-d.ts | 29 +++++-------- src/impl/FormulaCell.ts | 73 +++++++++++++------------------- src/impl/Formulas.test-d.ts | 4 +- src/impl/Formulas.ts | 22 ++++------ src/impl/SourceCell.ts | 2 +- src/index.ts | 14 +----- src/interfaces/Deref.ts | 4 +- src/interfaces/Subscribe.test.ts | 11 +---- src/interfaces/Subscribe.ts | 10 +---- tsconfig.json | 4 +- 12 files changed, 61 insertions(+), 118 deletions(-) diff --git a/.prettierrc.json b/.prettierrc.json index 9922d53..e552d64 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -2,6 +2,7 @@ "bracketSpacing": true, "semi": false, "singleQuote": true, - "trailingComma": "es5", + "printWidth": 100, + "trailingComma": "all", "tabWidth": 4 } diff --git a/src/TestUtils.ts b/src/TestUtils.ts index 445f741..6e86458 100644 --- a/src/TestUtils.ts +++ b/src/TestUtils.ts @@ -1,6 +1,5 @@ export const inc = (x: number) => x + 1 -export const sum = (...nums: number[]) => - nums.reduce((x: number, y: number) => x + y, 0) +export const sum = (...nums: number[]) => nums.reduce((x: number, y: number) => x + y, 0) export const identity = (x: T) => x export const negate = (x: boolean) => !x diff --git a/src/impl/FormulaCell.test-d.ts b/src/impl/FormulaCell.test-d.ts index 954b463..384d1a8 100644 --- a/src/impl/FormulaCell.test-d.ts +++ b/src/impl/FormulaCell.test-d.ts @@ -31,14 +31,7 @@ import { FormulaCell, Cell, SourceCell, cell, map } from '../index' const justString = 'a' // functions passed to map - const mapFn = ( - n1: number, - n2: number, - n3: number, - s1: string, - s2: string, - s3: string - ) => + const mapFn = (n1: number, n2: number, n3: number, s1: string, s2: string, s3: string) => [ Math.round(n1) + Math.round(n2) + Math.round(n3), s1.toUpperCase() + s2.toUpperCase() + s3.toUpperCase(), @@ -49,7 +42,7 @@ import { FormulaCell, Cell, SourceCell, cell, map } from '../index' n3: boolean, s1: string, s2: string, - s3: string + s3: string, ) => [ Math.round(n1) + Math.round(n2) + (n3 ? 1 : 2), @@ -61,7 +54,7 @@ import { FormulaCell, Cell, SourceCell, cell, map } from '../index' n3: number, s1: string, s2: string, - s3: boolean + s3: boolean, ) => [ Math.round(n1) + Math.round(n2) + Math.round(n3), @@ -76,7 +69,7 @@ import { FormulaCell, Cell, SourceCell, cell, map } from '../index' justNumber, stringCell, formulaStringCell, - justString + justString, ) const allFormulaInput = map( mapFn, @@ -85,7 +78,7 @@ import { FormulaCell, Cell, SourceCell, cell, map } from '../index' formulaNumberCell, formulaStringCell, formulaStringCell, - formulaStringCell + formulaStringCell, ) const allCellInput = map( mapFn, @@ -94,7 +87,7 @@ import { FormulaCell, Cell, SourceCell, cell, map } from '../index' numberCell, stringCell, stringCell, - stringCell + stringCell, ) const allRawInput = map( mapFn, @@ -103,7 +96,7 @@ import { FormulaCell, Cell, SourceCell, cell, map } from '../index' justNumber, justString, justString, - justString + justString, ) // tests @@ -125,8 +118,8 @@ import { FormulaCell, Cell, SourceCell, cell, map } from '../index' justNumber, stringCell, formulaStringCell, - justString - ) + justString, + ), ) expectError( map( @@ -136,7 +129,7 @@ import { FormulaCell, Cell, SourceCell, cell, map } from '../index' justNumber, stringCell, formulaStringCell, - justString - ) + justString, + ), ) } diff --git a/src/impl/FormulaCell.ts b/src/impl/FormulaCell.ts index 3994eb9..0b8c2e9 100644 --- a/src/impl/FormulaCell.ts +++ b/src/impl/FormulaCell.ts @@ -22,9 +22,7 @@ export class FormulaCell implements Cell, Recalculable { public deref(): T { if (this.destroyed) { - throw new OperationOnDestroyedCellError( - 'Impossible to deref a destroyed cell' - ) + throw new OperationOnDestroyedCellError('Impossible to deref a destroyed cell') } return this.val } @@ -41,7 +39,7 @@ export class FormulaCell implements Cell, Recalculable { '\noccurred while recalculating\n', this, '\nwith the following arguments\n', - args + args, ) return this.val } @@ -53,9 +51,7 @@ export class FormulaCell implements Cell, Recalculable { public addSubscriber(subscriber: Recalculable & Destroyable): void { if (this.destroyed) { - throw new OperationOnDestroyedCellError( - 'Impossible to subscribe a destroyed cell' - ) + throw new OperationOnDestroyedCellError('Impossible to subscribe a destroyed cell') } this.subs.add(subscriber) } @@ -96,37 +92,29 @@ type InferReturn = T extends (...args: any[]) => infer U ? U : never export function formula< T1 extends Value, FN extends (val1: UnwrapCell) => unknown, - R = InferReturn + R = InferReturn, >(fn: FN, input1: T1): T1 extends Cell ? FormulaCell : R export function formula< T1 extends Value, T2 extends Value, FN extends (val1: UnwrapCell, val2: UnwrapCell) => unknown, - R = InferReturn + R = InferReturn, >( fn: FN, input1: T1, - input2: T2 -): T1 extends Cell - ? FormulaCell - : T2 extends Cell - ? FormulaCell - : R + input2: T2, +): T1 extends Cell ? FormulaCell : T2 extends Cell ? FormulaCell : R export function formula< T1 extends Value, T2 extends Value, T3 extends Value, - FN extends ( - val1: UnwrapCell, - val2: UnwrapCell, - val3: UnwrapCell - ) => unknown, - R = InferReturn + FN extends (val1: UnwrapCell, val2: UnwrapCell, val3: UnwrapCell) => unknown, + R = InferReturn, >( fn: FN, input1: T1, input2: T2, - input3: T3 + input3: T3, ): T1 extends Cell ? FormulaCell : T2 extends Cell @@ -143,15 +131,15 @@ export function formula< val1: UnwrapCell, val2: UnwrapCell, val3: UnwrapCell, - val4: UnwrapCell + val4: UnwrapCell, ) => unknown, - R = InferReturn + R = InferReturn, >( fn: FN, input1: T1, input2: T2, input3: T3, - input4: T4 + input4: T4, ): T1 extends Cell ? FormulaCell : T2 extends Cell @@ -172,16 +160,16 @@ export function formula< val2: UnwrapCell, val3: UnwrapCell, val4: UnwrapCell, - val5: UnwrapCell + val5: UnwrapCell, ) => unknown, - R = InferReturn + R = InferReturn, >( fn: FN, input1: T1, input2: T2, input3: T3, input4: T4, - input5: T5 + input5: T5, ): T1 extends Cell ? FormulaCell : T2 extends Cell @@ -206,9 +194,9 @@ export function formula< val3: UnwrapCell, val4: UnwrapCell, val5: UnwrapCell, - val6: UnwrapCell + val6: UnwrapCell, ) => unknown, - R = InferReturn + R = InferReturn, >( fn: FN, input1: T1, @@ -216,7 +204,7 @@ export function formula< input3: T3, input4: T4, input5: T5, - input6: T6 + input6: T6, ): T1 extends Cell ? FormulaCell : T2 extends Cell @@ -245,9 +233,9 @@ export function formula< val4: UnwrapCell, val5: UnwrapCell, val6: UnwrapCell, - val7: UnwrapCell + val7: UnwrapCell, ) => unknown, - R = InferReturn + R = InferReturn, >( fn: FN, input1: T1, @@ -256,7 +244,7 @@ export function formula< input4: T4, input5: T5, input6: T6, - input7: T7 + input7: T7, ): T1 extends Cell ? FormulaCell : T2 extends Cell @@ -289,9 +277,9 @@ export function formula< val5: UnwrapCell, val6: UnwrapCell, val7: UnwrapCell, - val8: UnwrapCell + val8: UnwrapCell, ) => unknown, - R = InferReturn + R = InferReturn, >( fn: FN, input1: T1, @@ -301,7 +289,7 @@ export function formula< input5: T5, input6: T6, input7: T7, - input8: T8 + input8: T8, ): T1 extends Cell ? FormulaCell : T2 extends Cell @@ -338,9 +326,9 @@ export function formula< val6: UnwrapCell, val7: UnwrapCell, val8: UnwrapCell, - val9: UnwrapCell + val9: UnwrapCell, ) => unknown, - R = InferReturn + R = InferReturn, >( fn: FN, input1: T1, @@ -351,7 +339,7 @@ export function formula< input6: T6, input7: T7, input8: T8, - input9: T9 + input9: T9, ): T1 extends Cell ? FormulaCell : T2 extends Cell @@ -371,10 +359,7 @@ export function formula< : T9 extends Cell ? FormulaCell : R -export function formula( - fn: Function, - ...sources: Value[] -): FormulaCell | T { +export function formula(fn: Function, ...sources: Value[]): FormulaCell | T { if (sources.some(isCell)) { return new FormulaCell(fn, ...sources) } else { diff --git a/src/impl/Formulas.test-d.ts b/src/impl/Formulas.test-d.ts index 1e5ba8f..4c31164 100644 --- a/src/impl/Formulas.test-d.ts +++ b/src/impl/Formulas.test-d.ts @@ -158,9 +158,7 @@ import { expectType>(field(s1, recCell)) expectType>(field(s2, recCell)) // Notice: non-existing key Symbol('c') - expectType>( - field(Symbol('c'), recCell) - ) + expectType>(field(Symbol('c'), recCell)) // cannot pass key of wrong type expectError(field(1, recCell)) diff --git a/src/impl/Formulas.ts b/src/impl/Formulas.ts index cac25ed..3b57596 100644 --- a/src/impl/Formulas.ts +++ b/src/impl/Formulas.ts @@ -7,10 +7,8 @@ type HistoryResult = [T, T | undefined] * initially oldValue is undefined */ export function history>( - cell: T -): T extends Cell - ? FormulaCell>> - : HistoryResult> { + cell: T, +): T extends Cell ? FormulaCell>> : HistoryResult> { let oldVal: UnwrapCell | undefined = undefined return formula((newVal: UnwrapCell) => { const result: HistoryResult> = [newVal, oldVal] @@ -36,12 +34,10 @@ type IsRecord = string extends keyof T */ export function field, K extends keyof UnwrapCell>( fieldName: K, - fromCell: T + fromCell: T, ): T extends Cell ? FormulaCell< - IsRecord> extends true - ? UnwrapCell[K] | undefined - : UnwrapCell[K] + IsRecord> extends true ? UnwrapCell[K] | undefined : UnwrapCell[K] > : IsRecord> extends true ? UnwrapCell[K] | undefined @@ -51,21 +47,19 @@ export function field, K extends keyof UnwrapCell>( export function byIndex, U extends number>( index: U, - source: T -): T extends Cell - ? FormulaCell[U] | undefined> - : UnwrapCell[U] | undefined { + source: T, +): T extends Cell ? FormulaCell[U] | undefined> : UnwrapCell[U] | undefined { return formula((fromVal) => fromVal[index], source) } export function toBool>( - source: T + source: T, ): T extends Cell ? FormulaCell : boolean { return formula((fromVal) => Boolean(fromVal), source) } export function not>( - source: T + source: T, ): T extends Cell ? FormulaCell : boolean { return formula((fromVal) => !Boolean(fromVal), source) } diff --git a/src/impl/SourceCell.ts b/src/impl/SourceCell.ts index 1b21cf5..7e673db 100644 --- a/src/impl/SourceCell.ts +++ b/src/impl/SourceCell.ts @@ -82,7 +82,7 @@ export class SourceCell implements Cell, Updatable { private verifyIfDestroyed(): void { if (this.destroyed) { throw new OperationOnDestroyedCellError( - 'Operation is not supported on a destroyed cell' + 'Operation is not supported on a destroyed cell', ) } } diff --git a/src/index.ts b/src/index.ts index 432f5da..bc7800c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,17 +1,7 @@ export { Dereferencable, isDereferencable, deref } from './interfaces/Deref' -export { - Destroyable, - isDestroyable, - destroy, - isDestroyed, -} from './interfaces/Destroy' +export { Destroyable, isDestroyable, destroy, isDestroyed } from './interfaces/Destroy' export { Recalculable } from './interfaces/Recalculate' -export { - Subscribable, - isSubscribable, - subscribe, - unsubscribe, -} from './interfaces/Subscribe' +export { Subscribable, isSubscribable, subscribe, unsubscribe } from './interfaces/Subscribe' export { Updatable, reset, swap } from './interfaces/Update' export { Cell, Value, isCell } from './interfaces/Cell' export { OperationOnDestroyedCellError } from './impl/OperationOnDestroyedCellError' diff --git a/src/interfaces/Deref.ts b/src/interfaces/Deref.ts index e896848..ec6a1bd 100644 --- a/src/interfaces/Deref.ts +++ b/src/interfaces/Deref.ts @@ -4,9 +4,7 @@ export interface Dereferencable { deref(): T } -export function isDereferencable( - val: Dereferencable | T -): val is Dereferencable { +export function isDereferencable(val: Dereferencable | T): val is Dereferencable { return hasMethods>(val, ['deref']) } diff --git a/src/interfaces/Subscribe.test.ts b/src/interfaces/Subscribe.test.ts index 92763dd..bad481c 100644 --- a/src/interfaces/Subscribe.test.ts +++ b/src/interfaces/Subscribe.test.ts @@ -1,11 +1,6 @@ import { Destroyable } from './Destroy' import { Recalculable } from './Recalculate' -import { - Subscribable, - isSubscribable, - subscribe, - unsubscribe, -} from './Subscribe' +import { Subscribable, isSubscribable, subscribe, unsubscribe } from './Subscribe' const subscriber: Recalculable & Destroyable = { recalculate: () => {}, @@ -46,9 +41,7 @@ describe('Subscribe', () => { test('should trigger unsubscribe method if applied to a subscribable object', () => { const x: Subscribable = { addSubscriber: (_: Recalculable & Destroyable) => {}, - removeSubscriber: jest.fn( - (_: Recalculable & Destroyable) => {} - ), + removeSubscriber: jest.fn((_: Recalculable & Destroyable) => {}), } unsubscribe(subscriber, x) expect(x.removeSubscriber).toBeCalledTimes(1) diff --git a/src/interfaces/Subscribe.ts b/src/interfaces/Subscribe.ts index 5c03ba1..5da6d03 100644 --- a/src/interfaces/Subscribe.ts +++ b/src/interfaces/Subscribe.ts @@ -11,16 +11,10 @@ export function isSubscribable(val: any): val is Subscribable { return hasMethods(val, ['addSubscriber', 'removeSubscriber']) } -export function subscribe( - subscriber: Recalculable & Destroyable, - val: any -): void { +export function subscribe(subscriber: Recalculable & Destroyable, val: any): void { if (isSubscribable(val)) val.addSubscriber(subscriber) } -export function unsubscribe( - subscriber: Recalculable & Destroyable, - val: any -): void { +export function unsubscribe(subscriber: Recalculable & Destroyable, val: any): void { if (isSubscribable(val)) val.removeSubscriber(subscriber) } diff --git a/tsconfig.json b/tsconfig.json index 17d3968..f4dbe0c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,9 +6,7 @@ // "incremental": true, /* Enable incremental compilation */ "target": "es2015" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "module": "esnext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, - "lib": [ - "es2015" - ] /* Specify library files to be included in the compilation. */, + "lib": ["es2015"] /* Specify library files to be included in the compilation. */, // "allowJs": true, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ From e48d82a1a947ad9862588d87f670b1a148c04947 Mon Sep 17 00:00:00 2001 From: Vooord Date: Tue, 7 Mar 2023 09:53:16 +0530 Subject: [PATCH 2/3] Removed `bracketSpacing` option from `.prettierrc.json` as it uses default value --- .prettierrc.json | 1 - 1 file changed, 1 deletion(-) diff --git a/.prettierrc.json b/.prettierrc.json index e552d64..3406770 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -1,5 +1,4 @@ { - "bracketSpacing": true, "semi": false, "singleQuote": true, "printWidth": 100, From 4f7d4b017c829759e414e72d1fcaa3639f420228 Mon Sep 17 00:00:00 2001 From: Vooord Date: Tue, 7 Mar 2023 09:54:33 +0530 Subject: [PATCH 3/3] Introduced `.git-blame-ignore-revs` file and docs --- .git-blame-ignore-revs | 6 ++++++ README.dev.md | 14 ++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 .git-blame-ignore-revs diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..c526cf5 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,6 @@ +# see https://git-scm.com/docs/git-blame#Documentation/git-blame.txt---ignore-revs-fileltfilegt + +# Prettier change: +# "trailingComma": "es5" -> "all"; +# "printWidth": 80 -> 100; +f6326cffe7c08ee5755c3c4f46a27458165b1553 diff --git a/README.dev.md b/README.dev.md index 255be78..a2e3104 100644 --- a/README.dev.md +++ b/README.dev.md @@ -35,3 +35,17 @@ Here is how you do this: * [new tag] v0.0.9 -> v0.0.9 ``` + +## Format the code + +### ignore-revs-file + +Sometimes (after long and bloody discussions) we decide to change `.prettierrc.json`. It leads to a huge commit, +as `Prettier` automatically reformats all the project files in a pre-commit hook. This commit has no use for git-blame, +so we'd like to ignore it, i.e. see the blame of previous commit for strings affected by reformatting. +Here comes [.git-blame-ignore-revs](./.git-blame-ignore-revs) file where we list such commits. +To make it work in your local repo, run + +```shell +git config blame.ignoreRevsFile .git-blame-ignore-revs +```