Refactor to enforce strictNullChecks

This commit applies `strictNullChecks` to the entire codebase to improve
maintainability and type safety. Key changes include:

- Remove some explicit null-checks where unnecessary.
- Add necessary null-checks.
- Refactor static factory functions for a more functional approach.
- Improve some test names and contexts for better debugging.
- Add unit tests for any additional logic introduced.
- Refactor `createPositionFromRegexFullMatch` to its own function as the
  logic is reused.
- Prefer `find` prefix on functions that may return `undefined` and
  `get` prefix for those that always return a value.
This commit is contained in:
undergroundwires
2023-11-12 22:54:00 +01:00
parent 7ab16ecccb
commit 949fac1a7c
294 changed files with 2477 additions and 2738 deletions

View File

@@ -104,10 +104,14 @@ async function downloadFileWithProgress(
if (!response.ok) {
throw Error(`Unable to download, server returned ${response.status} ${response.statusText}`);
}
const contentLength = +response.headers.get('content-length');
const contentLengthString = response.headers.get('content-length');
if (contentLengthString === null || contentLengthString === undefined) {
log.error('Content-Length header is missing');
}
const contentLength = +(contentLengthString ?? 0);
const writer = fs.createWriteStream(filePath);
log.info(`Writing to ${filePath}, content length: ${contentLength}`);
if (!contentLength) {
if (Number.isNaN(contentLength) || contentLength <= 0) {
log.error('Unknown content-length', Array.from(response.headers.entries()));
progressHandler = () => { /* do nothing */ };
}
@@ -138,7 +142,7 @@ async function streamWithProgress(
log.info('Downloaded successfully');
}
function getReader(response: Response): NodeJS.ReadableStream {
function getReader(response: Response): NodeJS.ReadableStream | undefined {
// On browser, we could use browser API response.body.getReader()
// But here, we use cross-fetch that gets node-fetch on a node application
// This API is node-fetch specific, see https://github.com/node-fetch/node-fetch#streams

View File

@@ -4,7 +4,7 @@ import { app, BrowserWindow } from 'electron';
import log from 'electron-log';
export class UpdateProgressBar {
private progressBar: ProgressBar;
private progressBar: ProgressBar | undefined;
private get innerProgressBarWindow(): BrowserWindow {
// eslint-disable-next-line no-underscore-dangle
@@ -39,16 +39,16 @@ export class UpdateProgressBar {
+ `\n${e && e.message ? e.message : e}`;
this.innerProgressBarWindow.setClosable(true);
};
if (this.progressBar.innerProgressBarWindow) {
if (this.progressBar?.innerProgressBarWindow) {
reportUpdateError();
} else {
this.progressBar.on('ready', () => reportUpdateError());
this.progressBar?.on('ready', () => reportUpdateError());
}
}
public close() {
if (!this.progressBar.isCompleted()) {
this.progressBar.close();
if (!this.progressBar?.isCompleted()) {
this.progressBar?.close();
}
}
}