Add optionality for parameters
This commit allows for parameters that does not require any arguments to be provided in function calls. It changes collection syntax where parameters are list of objects instead of primitive strings. A parameter has now 'name' and 'optional' properties. 'name' is required and used in same way as older strings as parameter definitions. 'Optional' property is optional, 'false' is the default behavior if undefined. It also adds additional validation to restrict parameter names to alphanumeric strings to have a clear syntax in expressions.
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition';
|
||||
import { ExpressionArguments, IExpression } from '@/application/Parser/Script/Compiler/Expressions/Expression/IExpression';
|
||||
import { IExpression } from '@/application/Parser/Script/Compiler/Expressions/Expression/IExpression';
|
||||
import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection';
|
||||
import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
|
||||
import { FunctionParameterCollectionStub } from './FunctionParameterCollectionStub';
|
||||
|
||||
export class ExpressionStub implements IExpression {
|
||||
public callHistory = new Array<ExpressionArguments>();
|
||||
public callHistory = new Array<IReadOnlyFunctionCallArgumentCollection>();
|
||||
public position = new ExpressionPosition(0, 5);
|
||||
public parameters = [];
|
||||
public parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollectionStub();
|
||||
private result: string;
|
||||
public withParameters(...parameters: string[]) {
|
||||
public withParameters(parameters: IReadOnlyFunctionParameterCollection) {
|
||||
this.parameters = parameters;
|
||||
return this;
|
||||
}
|
||||
public withParameterNames(parameterNames: readonly string[], isOptional = false) {
|
||||
const collection = new FunctionParameterCollectionStub()
|
||||
.withParameterNames(parameterNames, isOptional);
|
||||
return this.withParameters(collection);
|
||||
}
|
||||
public withPosition(start: number, end: number) {
|
||||
this.position = new ExpressionPosition(start, end);
|
||||
return this;
|
||||
@@ -18,7 +26,7 @@ export class ExpressionStub implements IExpression {
|
||||
this.result = result;
|
||||
return this;
|
||||
}
|
||||
public evaluate(args?: ExpressionArguments): string {
|
||||
public evaluate(args: IReadOnlyFunctionCallArgumentCollection): string {
|
||||
this.callHistory.push(args);
|
||||
const result = this.result || `[expression-stub] args: ${args ? Object.keys(args).map((key) => `${key}: ${args[key]}`).join('", "') : 'none'}`;
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user