Skip to content

Commit

Permalink
♻️ (typescript) fixing strictNullChecks=true issues (#2228)
Browse files Browse the repository at this point in the history
* ♻️ (typescript) fixing strictNullChecks=true issues

* Patch test-helpers

* Strict patch
  • Loading branch information
MatissJanis authored Jan 18, 2024
1 parent 363c9e4 commit ff11399
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 38 deletions.
21 changes: 11 additions & 10 deletions packages/loot-core/src/shared/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import { type HandlerFunctions } from '../types/handlers';
export function sequential<T extends HandlerFunctions>(
fn: T,
): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>> {
const sequenceState = {
const sequenceState: {
running: Promise<Awaited<ReturnType<T>>> | null;
queue: Array<{ args: Parameters<T>; resolve; reject }>;
} = {
running: null,
queue: [],
};

function pump() {
if (sequenceState.queue.length > 0) {
const next = sequenceState.queue.shift();
const next = sequenceState.queue.shift();
if (next !== undefined) {
run(next.args, next.resolve, next.reject);
} else {
sequenceState.running = null;
}
}

function run(args, resolve, reject) {
sequenceState.running = fn.apply(null, args);

sequenceState.running.then(
function run(args: Parameters<T>, resolve, reject) {
sequenceState.running = fn.apply(null, args).then(
val => {
pump();
resolve(val);
Expand All @@ -48,9 +49,9 @@ export function sequential<T extends HandlerFunctions>(

export function once<T extends HandlerFunctions>(
fn: T,
): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>> {
let promise = null;
return (...args) => {
): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>> | null {
let promise: Promise<Awaited<ReturnType<T>>> | null = null;
return (...args: Parameters<T>) => {
if (!promise) {
promise = fn.apply(null, args).finally(() => {
promise = null;
Expand Down
8 changes: 4 additions & 4 deletions packages/loot-core/src/shared/months.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export function _range(
end: DateLike,
inclusive = false,
): string[] {
const months = [];
const months: string[] = [];
let month = monthFromDate(start);
while (d.isBefore(_parse(month), _parse(end))) {
months.push(month);
Expand All @@ -210,15 +210,15 @@ export function _dayRange(
end: DateLike,
inclusive = false,
): string[] {
const days = [];
const days: string[] = [];
let day = start;
while (d.isBefore(_parse(day), _parse(end))) {
days.push(day);
days.push(dayFromDate(day));
day = addDays(day, 1);
}

if (inclusive) {
days.push(day);
days.push(dayFromDate(day));
}

return days;
Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/shared/query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-strict-ignore
export type QueryState = {
filterExpressions: Array<unknown>;
filterExpressions: Array<string>;
selectExpressions: Array<unknown>;
groupExpressions: Array<unknown>;
orderExpressions: Array<unknown>;
Expand Down
10 changes: 7 additions & 3 deletions packages/loot-core/src/shared/schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import type { IRuleOptions } from '@rschedule/core';
import * as monthUtils from './months';
import { q } from './query';

export function getStatus(nextDate, completed, hasTrans) {
export function getStatus(
nextDate: string,
completed: boolean,
hasTrans: boolean,
) {
const today = monthUtils.currentDay();

if (completed) {
Expand Down Expand Up @@ -45,7 +49,7 @@ export function getHasTransactionsQuery(schedules) {
.select(['schedule', 'date']);
}

function makeNumberSuffix(num) {
function makeNumberSuffix(num: number) {
// Slight abuse of date-fns to turn a number like "1" into the full
// form "1st" but formatting a date with that number
return monthUtils.format(new Date(2020, 0, num, 12), 'do');
Expand Down Expand Up @@ -128,7 +132,7 @@ export function getRecurringDescription(config, dateFormat) {

desc += ' on the ';

const strs = [];
const strs: string[] = [];

const uniqueDays = new Set(patterns.map(p => p.type));
const isSameDay = uniqueDays.size === 1 && !uniqueDays.has('day');
Expand Down
47 changes: 27 additions & 20 deletions packages/loot-core/src/shared/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// @ts-strict-ignore
export let tracer = null;
export let tracer: null | ReturnType<typeof execTracer> = null;

function timeout(promise, n) {
let resolve;
const timeoutPromise = new Promise(_ => (resolve = _));
function timeout<T extends Promise<unknown>>(promise: T, n: number) {
let resolve: (response: string) => void;
const timeoutPromise = new Promise<string>(_ => (resolve = _));
const timer = setTimeout(() => resolve(`timeout(${n})`), n);

return Promise.race([
Expand All @@ -19,16 +18,20 @@ export function resetTracer() {
tracer = execTracer();
}

export function execTracer() {
const queue = [];
export function execTracer<T>() {
const queue: Array<{ name: string; data?: T }> = [];
let hasStarted = false;
let waitingFor = null;
let waitingFor: null | {
name: string;
reject: (error: Error) => void;
resolve: (data?: T) => void;
} = null;
let ended = false;

const log = false;

return {
event(name: string, data?: unknown) {
event(name: string, data?: T) {
if (!hasStarted) {
return;
} else if (log) {
Expand Down Expand Up @@ -57,7 +60,7 @@ export function execTracer() {
}
},

wait(name) {
wait(name: string) {
if (waitingFor) {
throw new Error(
`Already waiting for ${waitingFor.name}, cannot wait for multiple events`,
Expand All @@ -69,18 +72,22 @@ export function execTracer() {
});
},

expectWait(name, data) {
expectWait(name: string, data?: T) {
if (!hasStarted) {
throw new Error(`Expected “${name}” but tracer hasn’t started yet`);
} else if (log) {
console.log(`--- expectWait(${name}) ---`);
}

let promise = this.wait(name);
const promise = this.wait(name);
if (data === undefined) {
// We want to ignore the result
promise = promise.then(() => true);
data = true;
return expect(
timeout(
promise.then(() => true),
1000,
),
).resolves.toEqual(true);
}

if (typeof data === 'function') {
Expand All @@ -97,20 +104,20 @@ export function execTracer() {
}
},

expectNow(name, data) {
expectNow(name: string, data?: T) {
if (!hasStarted) {
throw new Error(`Expected “${name}” but tracer hasn’t started yet`);
} else if (log) {
console.log(`--- expectNow(${name}) ---`);
}

if (queue.length === 0) {
const entry = queue.shift();

if (!entry) {
throw new Error(
`Expected event “${name}” but none found - has it happened yet?`,
);
} else if (queue[0].name === name) {
const entry = queue.shift();

} else if (entry.name === name) {
if (typeof data === 'function') {
data(entry.data);
} else {
Expand All @@ -123,7 +130,7 @@ export function execTracer() {
}
},

expect(name: string, data?: unknown) {
expect(name: string, data?: T) {
if (queue.length === 0) {
return this.expectWait(name, data);
}
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/2228.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [MatissJanis]
---

TypeScript: fix some `strictNullChecks: true` issues (pt.2)

0 comments on commit ff11399

Please sign in to comment.