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.
64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { FunctionCall } from './Call/FunctionCall';
|
|
|
|
import {
|
|
FunctionBodyType, IFunctionCode, ISharedFunction, SharedFunctionBody,
|
|
} from './ISharedFunction';
|
|
import { IReadOnlyFunctionParameterCollection } from './Parameter/IFunctionParameterCollection';
|
|
|
|
export function createCallerFunction(
|
|
name: string,
|
|
parameters: IReadOnlyFunctionParameterCollection,
|
|
callSequence: readonly FunctionCall[],
|
|
): ISharedFunction {
|
|
if (!callSequence.length) {
|
|
throw new Error(`missing call sequence in function "${name}"`);
|
|
}
|
|
return new SharedFunction(name, parameters, callSequence, FunctionBodyType.Calls);
|
|
}
|
|
|
|
export function createFunctionWithInlineCode(
|
|
name: string,
|
|
parameters: IReadOnlyFunctionParameterCollection,
|
|
code: string,
|
|
revertCode?: string,
|
|
): ISharedFunction {
|
|
if (!code) {
|
|
throw new Error(`undefined code in function "${name}"`);
|
|
}
|
|
const content: IFunctionCode = {
|
|
execute: code,
|
|
revert: revertCode,
|
|
};
|
|
return new SharedFunction(name, parameters, content, FunctionBodyType.Code);
|
|
}
|
|
|
|
class SharedFunction implements ISharedFunction {
|
|
public readonly body: SharedFunctionBody;
|
|
|
|
constructor(
|
|
public readonly name: string,
|
|
public readonly parameters: IReadOnlyFunctionParameterCollection,
|
|
content: IFunctionCode | readonly FunctionCall[],
|
|
bodyType: FunctionBodyType,
|
|
) {
|
|
if (!name) { throw new Error('missing function name'); }
|
|
|
|
switch (bodyType) {
|
|
case FunctionBodyType.Code:
|
|
this.body = {
|
|
type: FunctionBodyType.Code,
|
|
code: content as IFunctionCode,
|
|
};
|
|
break;
|
|
case FunctionBodyType.Calls:
|
|
this.body = {
|
|
type: FunctionBodyType.Calls,
|
|
calls: content as readonly FunctionCall[],
|
|
};
|
|
break;
|
|
default:
|
|
throw new Error(`unknown body type: ${FunctionBodyType[bodyType]}`);
|
|
}
|
|
}
|
|
}
|