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

@@ -1,29 +1,16 @@
import { describe, it, expect } from 'vitest';
import { SharedFunctionCollection } from '@/application/Parser/Script/Compiler/Function/SharedFunctionCollection';
import { SharedFunctionStub } from '@tests/unit/shared/Stubs/SharedFunctionStub';
import { FunctionBodyType } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
import { createSharedFunctionStubWithCode, createSharedFunctionStubWithCalls } from '@tests/unit/shared/Stubs/SharedFunctionStub';
import { FunctionCallStub } from '@tests/unit/shared/Stubs/FunctionCallStub';
import { itEachAbsentObjectValue, itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
import { itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('SharedFunctionCollection', () => {
describe('addFunction', () => {
describe('throws if function is missing', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing function';
const func = absentValue;
const sut = new SharedFunctionCollection();
// act
const act = () => sut.addFunction(func);
// assert
expect(act).to.throw(expectedError);
});
});
it('throws if function with same name already exists', () => {
// arrange
const functionName = 'duplicate-function';
const expectedError = `function with name ${functionName} already exists`;
const func = new SharedFunctionStub(FunctionBodyType.Code)
const func = createSharedFunctionStubWithCode()
.withName('duplicate-function');
const sut = new SharedFunctionCollection();
sut.addFunction(func);
@@ -43,13 +30,13 @@ describe('SharedFunctionCollection', () => {
const act = () => sut.getFunctionByName(absentValue);
// assert
expect(act).to.throw(expectedError);
});
}, { excludeNull: true, excludeUndefined: true });
});
it('throws if function does not exist', () => {
// arrange
const name = 'unique-name';
const expectedError = `called function is not defined "${name}"`;
const func = new SharedFunctionStub(FunctionBodyType.Code)
const func = createSharedFunctionStubWithCode()
.withName('unexpected-name');
const sut = new SharedFunctionCollection();
sut.addFunction(func);
@@ -61,7 +48,7 @@ describe('SharedFunctionCollection', () => {
describe('returns existing function', () => {
it('when function with inline code is added', () => {
// arrange
const expected = new SharedFunctionStub(FunctionBodyType.Code)
const expected = createSharedFunctionStubWithCode()
.withName('expected-function-name');
const sut = new SharedFunctionCollection();
// act
@@ -72,9 +59,9 @@ describe('SharedFunctionCollection', () => {
});
it('when calling function is added', () => {
// arrange
const callee = new SharedFunctionStub(FunctionBodyType.Code)
const callee = createSharedFunctionStubWithCode()
.withName('calleeFunction');
const caller = new SharedFunctionStub(FunctionBodyType.Calls)
const caller = createSharedFunctionStubWithCalls()
.withName('callerFunction')
.withCalls(new FunctionCallStub().withFunctionName(callee.name));
const sut = new SharedFunctionCollection();