Skip to content

Commit 27b9554

Browse files
afonsojramossetchy
andauthored
feat: enable TypeScript strict mode (#2761)
* feat: enable strict mode in TypeScript config * fix: resolve strict mode errors in main process * fix: resolve strict mode errors in renderer process * refactor: use early return guard for mb.window across src/main * fix: restore null semantics for native notification url parameter * refactor: replace err as Error casts with toError utility * address sonar feedback Signed-off-by: Adam Setch <adam.setch@outlook.com> * address sonar feedback Signed-off-by: Adam Setch <adam.setch@outlook.com> --------- Signed-off-by: Adam Setch <adam.setch@outlook.com> Co-authored-by: Adam Setch <adam.setch@outlook.com>
1 parent c53ee8f commit 27b9554

File tree

98 files changed

+432
-318
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+432
-318
lines changed

src/main/config.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ describe('main/config.ts', () => {
3333
expect(WindowConfig.resizable).toBe(false);
3434
expect(WindowConfig.skipTaskbar).toBe(true);
3535
expect(WindowConfig.webPreferences).toBeDefined();
36-
expect(WindowConfig.webPreferences.contextIsolation).toBe(true);
37-
expect(WindowConfig.webPreferences.nodeIntegration).toBe(false);
38-
expect(WindowConfig.webPreferences.backgroundThrottling).toBe(false);
36+
expect(WindowConfig.webPreferences?.contextIsolation).toBe(true);
37+
expect(WindowConfig.webPreferences?.nodeIntegration).toBe(false);
38+
expect(WindowConfig.webPreferences?.backgroundThrottling).toBe(false);
3939
});
4040
});

src/main/events.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import type { EventData, EventType } from '../shared/events';
1010
* @param event - The IPC channel/event name to listen on.
1111
* @param listener - Callback invoked when the event is received.
1212
*/
13-
export function onMainEvent(
13+
export function onMainEvent<T = EventData>(
1414
event: EventType,
15-
listener: (event: Electron.IpcMainEvent, args: EventData) => void,
15+
listener: (event: Electron.IpcMainEvent, args: T) => void,
1616
) {
17-
ipcMain.on(event, listener);
17+
ipcMain.on(event, listener as Parameters<typeof ipcMain.on>[1]);
1818
}
1919

2020
/**
@@ -24,14 +24,14 @@ export function onMainEvent(
2424
* @param event - The IPC channel/event name to handle.
2525
* @param listener - Callback whose return value is sent back to the renderer.
2626
*/
27-
export function handleMainEvent(
27+
export function handleMainEvent<T = EventData>(
2828
event: EventType,
2929
listener: (
3030
event: Electron.IpcMainInvokeEvent,
31-
data: EventData,
31+
data: T,
3232
) => unknown | Promise<unknown>,
3333
) {
34-
ipcMain.handle(event, listener);
34+
ipcMain.handle(event, listener as Parameters<typeof ipcMain.handle>[1]);
3535
}
3636

3737
/**
@@ -46,5 +46,9 @@ export function sendRendererEvent(
4646
event: EventType,
4747
data?: string,
4848
) {
49+
if (!mb.window) {
50+
return;
51+
}
52+
4953
mb.window.webContents.send(event, data);
5054
}

src/main/handlers/app.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ describe('main/handlers/app.ts', () => {
4444
registerAppHandlers(menubar);
4545

4646
const registeredHandlers = handleMock.mock.calls.map(
47-
(call: [string]) => call[0],
47+
(call: unknown[]) => call[0],
4848
);
4949
const registeredEvents = onMock.mock.calls.map(
50-
(call: [string]) => call[0],
50+
(call: unknown[]) => call[0],
5151
);
5252

5353
expect(registeredHandlers).toContain(EVENTS.VERSION);

src/main/handlers/storage.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ vi.mock('electron', () => ({
1515
}));
1616

1717
const logErrorMock = vi.fn();
18-
vi.mock('../../shared/logger', () => ({
19-
logError: (...args: unknown[]) => logErrorMock(...args),
20-
}));
18+
vi.mock('../../shared/logger', async (importOriginal) => {
19+
const actual = await importOriginal<typeof import('../../shared/logger')>();
20+
return {
21+
...actual,
22+
logError: (...args: unknown[]) => logErrorMock(...args),
23+
};
24+
});
2125

2226
describe('main/handlers/storage.ts', () => {
2327
describe('registerStorageHandlers', () => {
@@ -29,7 +33,7 @@ describe('main/handlers/storage.ts', () => {
2933
registerStorageHandlers();
3034

3135
const registeredHandlers = handleMock.mock.calls.map(
32-
(call: [string]) => call[0],
36+
(call: unknown[]) => call[0],
3337
);
3438

3539
expect(registeredHandlers).toContain(EVENTS.SAFE_STORAGE_ENCRYPT);

src/main/handlers/storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { safeStorage } from 'electron';
22

33
import { EVENTS } from '../../shared/events';
4-
import { logError } from '../../shared/logger';
4+
import { logError, toError } from '../../shared/logger';
55

66
import { handleMainEvent } from '../events';
77

@@ -26,7 +26,7 @@ export function registerStorageHandlers(): void {
2626
logError(
2727
'main:safe-storage-decrypt',
2828
'Failed to decrypt value - data may be from old build',
29-
err,
29+
toError(err),
3030
);
3131
throw err;
3232
}

src/main/handlers/system.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ describe('main/handlers/system.ts', () => {
6363
it('registers expected system IPC event handlers', () => {
6464
registerSystemHandlers(menubar);
6565

66-
const onEvents = onMock.mock.calls.map((call: [string]) => call[0]);
66+
const onEvents = onMock.mock.calls.map((call: unknown[]) => call[0]);
6767
const handleEvents = handleMock.mock.calls.map(
68-
(call: [string]) => call[0],
68+
(call: unknown[]) => call[0],
6969
);
7070

7171
expect(onEvents).toContain(EVENTS.OPEN_EXTERNAL);

src/main/handlers/system.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export function registerSystemHandlers(mb: Menubar): void {
2424
let lastRegisteredAccelerator: string | null = null;
2525

2626
const toggleWindow = () => {
27+
if (!mb.window) {
28+
return;
29+
}
30+
2731
if (mb.window.isVisible()) {
2832
mb.hideWindow();
2933
} else {

src/main/handlers/tray.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('main/handlers/tray.ts', () => {
3838
registerTrayHandlers(menubar);
3939

4040
const registeredEvents = onMock.mock.calls.map(
41-
(call: [string]) => call[0],
41+
(call: unknown[]) => call[0],
4242
);
4343

4444
expect(registeredEvents).toContain(EVENTS.USE_ALTERNATE_IDLE_ICON);
@@ -55,7 +55,7 @@ describe('main/handlers/tray.ts', () => {
5555
registerTrayHandlers(menubar);
5656

5757
const updateColorHandler = onMock.mock.calls.find(
58-
(call: [string]) => call[0] === EVENTS.UPDATE_ICON_COLOR,
58+
(call: unknown[]) => call[0] === EVENTS.UPDATE_ICON_COLOR,
5959
)?.[1];
6060
updateColorHandler?.({}, 5);
6161

@@ -66,7 +66,7 @@ describe('main/handlers/tray.ts', () => {
6666
registerTrayHandlers(menubar);
6767

6868
const updateColorHandler = onMock.mock.calls.find(
69-
(call: [string]) => call[0] === EVENTS.UPDATE_ICON_COLOR,
69+
(call: unknown[]) => call[0] === EVENTS.UPDATE_ICON_COLOR,
7070
)?.[1];
7171
updateColorHandler?.({}, 0);
7272

@@ -77,7 +77,7 @@ describe('main/handlers/tray.ts', () => {
7777
registerTrayHandlers(menubar);
7878

7979
const updateColorHandler = onMock.mock.calls.find(
80-
(call: [string]) => call[0] === EVENTS.UPDATE_ICON_COLOR,
80+
(call: unknown[]) => call[0] === EVENTS.UPDATE_ICON_COLOR,
8181
)?.[1];
8282
updateColorHandler?.({}, 3);
8383

@@ -88,7 +88,7 @@ describe('main/handlers/tray.ts', () => {
8888
registerTrayHandlers(menubar);
8989

9090
const updateColorHandler = onMock.mock.calls.find(
91-
(call: [string]) => call[0] === EVENTS.UPDATE_ICON_COLOR,
91+
(call: unknown[]) => call[0] === EVENTS.UPDATE_ICON_COLOR,
9292
)?.[1];
9393
updateColorHandler?.({}, -1);
9494

@@ -99,7 +99,7 @@ describe('main/handlers/tray.ts', () => {
9999
registerTrayHandlers(menubar);
100100

101101
const updateTitleHandler = onMock.mock.calls.find(
102-
(call: [string]) => call[0] === EVENTS.UPDATE_ICON_TITLE,
102+
(call: unknown[]) => call[0] === EVENTS.UPDATE_ICON_TITLE,
103103
)?.[1];
104104
updateTitleHandler?.({}, '5');
105105

src/main/lifecycle/first-run.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ vi.mock('electron', () => ({
3535
vi.mock('../utils', () => ({ isDevMode: () => false }));
3636

3737
const logErrorMock = vi.fn();
38-
vi.mock('../../shared/logger', () => ({
39-
logError: (...a: unknown[]) => logErrorMock(...a),
40-
}));
38+
vi.mock('../../shared/logger', async (importOriginal) => {
39+
const actual = await importOriginal<typeof import('../../shared/logger')>();
40+
return {
41+
...actual,
42+
logError: (...a: unknown[]) => logErrorMock(...a),
43+
};
44+
});
4145

4246
let mac = true;
4347
vi.mock('../../shared/platform', () => ({ isMacOS: () => mac }));

src/main/lifecycle/first-run.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'node:path';
44
import { app, dialog } from 'electron';
55

66
import { APPLICATION } from '../../shared/constants';
7-
import { logError } from '../../shared/logger';
7+
import { logError, toError } from '../../shared/logger';
88
import { isMacOS } from '../../shared/platform';
99

1010
import { isDevMode } from '../utils';
@@ -71,7 +71,11 @@ function checkAndMarkFirstRun(): boolean {
7171

7272
fs.writeFileSync(configPath, '');
7373
} catch (err) {
74-
logError('checkAndMarkFirstRun', 'Unable to write firstRun file', err);
74+
logError(
75+
'checkAndMarkFirstRun',
76+
'Unable to write firstRun file',
77+
toError(err),
78+
);
7579
}
7680

7781
return true;

0 commit comments

Comments
 (0)