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

@@ -3,40 +3,21 @@ import { CodeSubstituter } from '@/application/Parser/ScriptingDefinition/CodeSu
import { IExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler';
import { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformationStub';
import { ExpressionsCompilerStub } from '@tests/unit/shared/Stubs/ExpressionsCompilerStub';
import { getAbsentObjectTestCases, getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
import { itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('CodeSubstituter', () => {
describe('throws with invalid parameters', () => {
// arrange
const testCases = [
...getAbsentStringTestCases().map((testCase) => ({
name: `given code: ${testCase.valueName}`,
expectedError: 'missing code',
parameters: {
code: testCase.absentValue,
info: new ProjectInformationStub(),
},
})),
...getAbsentObjectTestCases().map((testCase) => ({
name: `given info: ${testCase.valueName}`,
expectedError: 'missing info',
parameters: {
code: 'non empty code',
info: testCase.absentValue,
},
})),
];
for (const testCase of testCases) {
it(`${testCase.name} throws "${testCase.expectedError}"`, () => {
// arrange
const sut = new CodeSubstituterBuilder().build();
const { code, info } = testCase.parameters;
// act
const act = () => sut.substitute(code, info);
// assert
expect(act).to.throw(testCase.expectedError);
});
}
describe('throws if code is empty', () => {
itEachAbsentStringValue((emptyCode) => {
// arrange
const expectedError = 'missing code';
const code = emptyCode;
const info = new ProjectInformationStub();
const sut = new CodeSubstituterBuilder().build();
// act
const act = () => sut.substitute(code, info);
// assert
expect(act).to.throw(expectedError);
}, { excludeNull: true, excludeUndefined: true });
});
describe('substitutes parameters as expected values', () => {
// arrange

View File

@@ -8,38 +8,9 @@ import { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformat
import { EnumParserStub } from '@tests/unit/shared/Stubs/EnumParserStub';
import { ScriptingDefinitionDataStub } from '@tests/unit/shared/Stubs/ScriptingDefinitionDataStub';
import { CodeSubstituterStub } from '@tests/unit/shared/Stubs/CodeSubstituterStub';
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('ScriptingDefinitionParser', () => {
describe('parseScriptingDefinition', () => {
describe('throws when info is missing', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing info';
const info = absentValue;
const definition = new ScriptingDefinitionDataStub();
const sut = new ScriptingDefinitionParserBuilder()
.build();
// act
const act = () => sut.parse(definition, info);
// assert
expect(act).to.throw(expectedError);
});
});
describe('throws when definition is missing', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing definition';
const info = new ProjectInformationStub();
const definition = absentValue;
const sut = new ScriptingDefinitionParserBuilder()
.build();
// act
const act = () => sut.parse(definition, info);
// assert
expect(act).to.throw(expectedError);
});
});
describe('language', () => {
it('parses as expected', () => {
// arrange