This commit fixes compiler bug where it fails when optional values are compiled into absent values in nested calls. - Throw exception with more context for easier future debugging. - Add better validation of argument values for nested calls. - Refactor `FunctionCallCompiler` for better clarity and modularize it to make it more maintainable and testable. - Refactor related interface to not have `I` prefix, and function/variable names for better clarity. Context: Discovered this issue while attempting to call `RunInlineCodeAsTrustedInstaller` which in turn invokes `RunPowerShell` for issue #246. This led to the realization that despite parameters flagged as optional, the nested argument compilation didn't support them.
55 lines
1.9 KiB
TypeScript
55 lines
1.9 KiB
TypeScript
import { IFunctionCallArgument } from '@/application/Parser/Script/Compiler/Function/Call/Argument/IFunctionCallArgument';
|
|
import { IFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/Function/Call/Argument/IFunctionCallArgumentCollection';
|
|
import { FunctionCallArgumentStub } from './FunctionCallArgumentStub';
|
|
|
|
export class FunctionCallArgumentCollectionStub implements IFunctionCallArgumentCollection {
|
|
private args = new Array<IFunctionCallArgument>();
|
|
|
|
public withEmptyArguments(): this {
|
|
this.args.length = 0;
|
|
return this;
|
|
}
|
|
|
|
public withSomeArguments(): this {
|
|
return this
|
|
.withArgument('firstTestParameterName', 'first-parameter-argument-value')
|
|
.withArgument('secondTestParameterName', 'second-parameter-argument-value')
|
|
.withArgument('thirdTestParameterName', 'third-parameter-argument-value');
|
|
}
|
|
|
|
public withArgument(parameterName: string, argumentValue: string): this {
|
|
const arg = new FunctionCallArgumentStub()
|
|
.withParameterName(parameterName)
|
|
.withArgumentValue(argumentValue);
|
|
this.addArgument(arg);
|
|
return this;
|
|
}
|
|
|
|
public withArguments(args: { readonly [index: string]: string }): this {
|
|
for (const [name, value] of Object.entries(args)) {
|
|
this.withArgument(name, value);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public hasArgument(parameterName: string): boolean {
|
|
return this.args.some((a) => a.parameterName === parameterName);
|
|
}
|
|
|
|
public addArgument(argument: IFunctionCallArgument): void {
|
|
this.args.push(argument);
|
|
}
|
|
|
|
public getAllParameterNames(): string[] {
|
|
return this.args.map((a) => a.parameterName);
|
|
}
|
|
|
|
public getArgument(parameterName: string): IFunctionCallArgument {
|
|
const arg = this.args.find((a) => a.parameterName === parameterName);
|
|
if (!arg) {
|
|
throw new Error(`no argument exists for parameter "${parameterName}"`);
|
|
}
|
|
return arg;
|
|
}
|
|
}
|