A local-first personal finance app
:recycle: (crdt) adding more strict typings (#1437)
Matiss Janis Aboltinsfac3af63602c
diff --git a/packages/crdt/src/crdt/merkle.test.ts b/packages/crdt/src/crdt/merkle.test.tsindex a0b7d98a68..21f02d5710 100644--- a/packages/crdt/src/crdt/merkle.test.ts+++ b/packages/crdt/src/crdt/merkle.test.ts@@ -1,13 +1,17 @@+/* eslint-disable @typescript-eslint/no-non-null-assertion */import * as merkle from './merkle';import { Timestamp } from './timestamp';-function message(timestamp, hash) {- timestamp = Timestamp.parse(timestamp);+function message(timestampStr: string, hash: number) {+ const timestamp = Timestamp.parse(timestampStr)!;timestamp.hash = () => hash;return { timestamp };}-function insertMessages(trie, messages) {+function insertMessages(+ trie: merkle.TrieNode,+ messages: ReturnType<typeof message>[],+) {messages.forEach(msg => {trie = merkle.insert(trie, msg.timestamp);});@@ -18,11 +22,11 @@ describe('merkle trie', () => {test('adding an item works', () => {let trie = merkle.insert(merkle.emptyTrie(),- Timestamp.parse('2018-11-12T13:21:40.122Z-0000-0123456789ABCDEF'),+ Timestamp.parse('2018-11-12T13:21:40.122Z-0000-0123456789ABCDEF')!,);trie = merkle.insert(trie,- Timestamp.parse('2018-11-13T13:21:40.122Z-0000-0123456789ABCDEF'),+ Timestamp.parse('2018-11-13T13:21:40.122Z-0000-0123456789ABCDEF')!,);expect(trie).toMatchSnapshot();});@@ -68,7 +72,7 @@ describe('merkle trie', () => {let trie1 = merkle.emptyTrie();let trie2 = merkle.insert(merkle.emptyTrie(),- Timestamp.parse('2009-01-02T10:17:37.789Z-0000-0000testinguuid1'),+ Timestamp.parse('2009-01-02T10:17:37.789Z-0000-0000testinguuid1')!,);expect(merkle.diff(trie1, trie2)).toBe(0);diff --git a/packages/crdt/src/crdt/merkle.ts b/packages/crdt/src/crdt/merkle.tsindex 20899838ab..ca987bf811 100644--- a/packages/crdt/src/crdt/merkle.ts+++ b/packages/crdt/src/crdt/merkle.ts@@ -47,7 +47,7 @@ export function insert(trie: TrieNode, timestamp: Timestamp) {return insertKey(trie, key, hash);}-function insertKey(trie: TrieNode, key: string, hash: number) {+function insertKey(trie: TrieNode, key: string, hash: number): TrieNode {if (key.length === 0) {return trie;}@@ -105,12 +105,13 @@ export function diff(trie1: TrieNode, trie2: TrieNode): number {for (let i = 0; i < keys.length; i++) {let key = keys[i];- if (!node1[key] || !node2[key]) {+ let next1 = node1[key];+ let next2 = node2[key];++ if (!next1 || !next2) {break;}- let next1 = node1[key];- let next2 = node2[key];if (next1.hash !== next2.hash) {diffkey = key;break;@@ -136,7 +137,7 @@ export function prune(trie: TrieNode, n = 2): TrieNode {let keys = getKeys(trie);keys.sort();- let next = { hash: trie.hash };+ let next: TrieNode = { hash: trie.hash };// Prune child nodes.for (let k of keys.slice(-n)) {@@ -146,7 +147,7 @@ export function prune(trie: TrieNode, n = 2): TrieNode {return next;}-export function debug(trie: TrieNode, k = '', indent = 0) {+export function debug(trie: TrieNode, k = '', indent = 0): string {const str =' '.repeat(indent) +(k !== '' ? `k: ${k} ` : '') +diff --git a/packages/crdt/src/crdt/timestamp.test.ts b/packages/crdt/src/crdt/timestamp.test.tsindex c1fb77f217..cea2a94a74 100644--- a/packages/crdt/src/crdt/timestamp.test.ts+++ b/packages/crdt/src/crdt/timestamp.test.ts@@ -1,8 +1,9 @@+/* eslint-disable @typescript-eslint/no-non-null-assertion */import { Timestamp } from './timestamp';describe('Timestamp', function () {let now = 0;- let prevNow;+ let prevNow: typeof Date.now;beforeEach(function () {prevNow = Date.now;@@ -16,10 +17,12 @@ describe('Timestamp', function () {describe('comparison', function () {it('should be in order', function () {+ const sendTimestamp = Timestamp.send();+expect(Timestamp.zero).toBe(Timestamp.zero);expect(Timestamp.max > Timestamp.zero).toBeTruthy();- expect(Timestamp.send() > Timestamp.zero).toBeTruthy();- expect(Timestamp.send() < Timestamp.max).toBeTruthy();+ expect(sendTimestamp && sendTimestamp > Timestamp.zero).toBeTruthy();+ expect(sendTimestamp && sendTimestamp < Timestamp.max).toBeTruthy();});});@@ -53,7 +56,7 @@ describe('Timestamp', function () {'9999-12-31T23:59:59.999Z-FFFF-FFFFFFFFFFFFFFFF',];for (let validInput of validInputs) {- let parsed = Timestamp.parse(validInput);+ let parsed = Timestamp.parse(validInput)!;expect(typeof parsed).toBe('object');expect(parsed.millis() >= 0).toBeTruthy();expect(parsed.millis() < 253402300800000).toBeTruthy();@@ -133,7 +136,7 @@ describe('Timestamp', function () {now = 52;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.051Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.051Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.052Z-0000-0000000000000001'),@@ -141,7 +144,7 @@ describe('Timestamp', function () {now = 54;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.053Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.053Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.054Z-0000-0000000000000001'),@@ -149,7 +152,7 @@ describe('Timestamp', function () {now = 56;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.055Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.055Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.056Z-0000-0000000000000001'),@@ -160,7 +163,7 @@ describe('Timestamp', function () {now = 61;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.062Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.062Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.062Z-0001-0000000000000001'),@@ -168,7 +171,7 @@ describe('Timestamp', function () {now = 62;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.062Z-0001-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.062Z-0001-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.062Z-0002-0000000000000001'),@@ -176,7 +179,7 @@ describe('Timestamp', function () {now = 62;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.062Z-0002-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.062Z-0002-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.062Z-0003-0000000000000001'),@@ -184,7 +187,7 @@ describe('Timestamp', function () {now = 63;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.062Z-0004-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.062Z-0004-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.063Z-0000-0000000000000001'),@@ -195,7 +198,7 @@ describe('Timestamp', function () {now = 73;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.071Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.071Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.073Z-0000-0000000000000001'),@@ -203,7 +206,7 @@ describe('Timestamp', function () {now = 73;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.072Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.072Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.073Z-0001-0000000000000001'),@@ -211,7 +214,7 @@ describe('Timestamp', function () {now = 74;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.073Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.073Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.074Z-0000-0000000000000001'),@@ -222,7 +225,7 @@ describe('Timestamp', function () {now = 81;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.083Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.083Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.083Z-0001-0000000000000001'),@@ -230,7 +233,7 @@ describe('Timestamp', function () {now = 82;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.083Z-0001-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.083Z-0001-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.083Z-0002-0000000000000001'),@@ -238,7 +241,7 @@ describe('Timestamp', function () {now = 83;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.083Z-0002-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.083Z-0002-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.083Z-0003-0000000000000001'),@@ -246,7 +249,7 @@ describe('Timestamp', function () {now = 84;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.083Z-0003-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.083Z-0003-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.084Z-0000-0000000000000001'),@@ -256,7 +259,7 @@ describe('Timestamp', function () {now = 93;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.091Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.091Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.093Z-0000-0000000000000001'),@@ -264,7 +267,7 @@ describe('Timestamp', function () {now = 92;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.092Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.092Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.093Z-0001-0000000000000001'),@@ -272,7 +275,7 @@ describe('Timestamp', function () {now = 91;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.093Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.093Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.093Z-0002-0000000000000001'),@@ -282,7 +285,7 @@ describe('Timestamp', function () {now = 101;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.103Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.103Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.103Z-0001-0000000000000001'),@@ -290,7 +293,7 @@ describe('Timestamp', function () {now = 102;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.102Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.102Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.103Z-0002-0000000000000001'),@@ -298,7 +301,7 @@ describe('Timestamp', function () {now = 103;expect(Timestamp.recv(- Timestamp.parse('1970-01-01T00:00:00.101Z-0000-0000000000000002'),+ Timestamp.parse('1970-01-01T00:00:00.101Z-0000-0000000000000002')!,),).toEqual(Timestamp.parse('1970-01-01T00:00:00.103Z-0003-0000000000000001'),@@ -316,7 +319,7 @@ describe('Timestamp', function () {it('should fail with clock drift', function () {expect(function () {Timestamp.recv(- Timestamp.parse('1980-01-01T00:00:00.101Z-0000-0000000000000002'),+ Timestamp.parse('1980-01-01T00:00:00.101Z-0000-0000000000000002')!,);}).toThrow(Timestamp.ClockDriftError);});diff --git a/packages/crdt/src/crdt/timestamp.ts b/packages/crdt/src/crdt/timestamp.tsindex 1fe7d31f41..1ff88aeaa8 100644--- a/packages/crdt/src/crdt/timestamp.ts+++ b/packages/crdt/src/crdt/timestamp.ts@@ -32,7 +32,7 @@ export type Clock = {};// A mutable global clock-let clock: Clock = null;+let clock: Clock;export function setClock(clock_: Clock): void {clock = clock_;@@ -148,9 +148,10 @@ export class Timestamp {/*** maximum timestamp*/+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertionstatic max = Timestamp.parse('9999-12-31T23:59:59.999Z-FFFF-FFFFFFFFFFFFFFFF',- );+ )!;/*** timestamp parsing@@ -287,11 +288,12 @@ export class Timestamp {/*** zero/minimum timestamp*/+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertionstatic zero = Timestamp.parse('1970-01-01T00:00:00.000Z-0000-0000000000000000',- );+ )!;- static since = isoString => isoString + '-0000-0000000000000000';+ static since = (isoString: string) => isoString + '-0000-0000000000000000';/*** error classes@@ -321,7 +323,7 @@ export class Timestamp {}class MutableTimestamp extends Timestamp {- static from(timestamp) {+ static from(timestamp: Timestamp) {return new MutableTimestamp(timestamp.millis(),timestamp.counter(),@@ -329,15 +331,15 @@ class MutableTimestamp extends Timestamp {);}- setMillis(n) {+ setMillis(n: number) {this._state.millis = n;}- setCounter(n) {+ setCounter(n: number) {this._state.counter = n;}- setNode(n) {+ setNode(n: string) {this._state.node = n;}}diff --git a/packages/crdt/tsconfig.dist.json b/packages/crdt/tsconfig.dist.jsonindex 81e315be11..25b6118e0b 100644--- a/packages/crdt/tsconfig.dist.json+++ b/packages/crdt/tsconfig.dist.json@@ -7,6 +7,8 @@"module": "CommonJS","noEmit": false,"declaration": true,+ // TODO: enable+ // "strict": true,"outDir": "dist"},"include": ["."],diff --git a/upcoming-release-notes/1437.md b/upcoming-release-notes/1437.mdnew file mode 100644index 0000000000..1b2d1f3bd8--- /dev/null+++ b/upcoming-release-notes/1437.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [MatissJanis]+---++`crdt`: make typings more strict