Files
privacy.sexy/tests/unit/shared/Stubs/ArgumentCompilerStub.ts
undergroundwires 53222fd83c Fix compiler bug with nested optional arguments
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.
2023-09-16 16:11:41 +02:00

38 lines
1.5 KiB
TypeScript

import { FunctionCallCompilationContext } from '@/application/Parser/Script/Compiler/Function/Call/Compiler/FunctionCallCompilationContext';
import { ArgumentCompiler } from '@/application/Parser/Script/Compiler/Function/Call/Compiler/SingleCall/Strategies/Argument/ArgumentCompiler';
import { FunctionCall } from '@/application/Parser/Script/Compiler/Function/Call/FunctionCall';
import { FunctionCallStub } from './FunctionCallStub';
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';
export class ArgumentCompilerStub
extends StubWithObservableMethodCalls<ArgumentCompiler>
implements ArgumentCompiler {
private readonly scenarios = new Array<ArgumentCompilationScenario>();
public createCompiledNestedCall(
nestedFunctionCall: FunctionCall,
parentFunctionCall: FunctionCall,
context: FunctionCallCompilationContext,
): FunctionCall {
this.registerMethodCall({
methodName: 'createCompiledNestedCall',
args: [nestedFunctionCall, parentFunctionCall, context],
});
const scenario = this.scenarios.find((s) => s.givenNestedFunctionCall === nestedFunctionCall);
if (scenario) {
return scenario.result;
}
return new FunctionCallStub();
}
public withScenario(scenario: ArgumentCompilationScenario): this {
this.scenarios.push(scenario);
return this;
}
}
interface ArgumentCompilationScenario {
readonly givenNestedFunctionCall: FunctionCall;
readonly result: FunctionCall;
}