Add support for nested templates
Add support for expressions inside expressions.
Add support for templating where the output of one expression results in
another template part with expressions.
E.g., this did not work before, but compilation will now evaluate both
with expression with `$condition` and parameter substitution with
`$text`:
```
{{ with $condition }}
echo '{{ $text }}'
{{ end }}
```
Add also more sanity checks (validation logic) when compiling
expressions to reveal problems quickly.
This commit is contained in:
@@ -4,15 +4,25 @@ import { IExpressionParser } from '@/application/Parser/Script/Compiler/Expressi
|
||||
export class ExpressionParserStub implements IExpressionParser {
|
||||
public callHistory = new Array<string>();
|
||||
|
||||
private result: IExpression[] = [];
|
||||
private results = new Map<string, readonly IExpression[]>();
|
||||
|
||||
public withResult(result: IExpression[]) {
|
||||
this.result = result;
|
||||
public withResult(code: string, result: readonly IExpression[]) {
|
||||
if (this.results.has(code)) {
|
||||
throw new Error(
|
||||
'Result for code is already registered.'
|
||||
+ `\nCode: ${code}`
|
||||
+ `\nResult: ${JSON.stringify(result)}`,
|
||||
);
|
||||
}
|
||||
this.results.set(code, result);
|
||||
return this;
|
||||
}
|
||||
|
||||
public findExpressions(code: string): IExpression[] {
|
||||
this.callHistory.push(code);
|
||||
return this.result;
|
||||
if (this.results.has(code)) {
|
||||
return [...this.results.get(code)];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ export class ExpressionStub implements IExpression {
|
||||
|
||||
public parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollectionStub();
|
||||
|
||||
private result: string;
|
||||
private result: string = undefined;
|
||||
|
||||
public withParameters(parameters: IReadOnlyFunctionParameterCollection) {
|
||||
this.parameters = parameters;
|
||||
@@ -35,9 +35,11 @@ export class ExpressionStub implements IExpression {
|
||||
}
|
||||
|
||||
public evaluate(context: IExpressionEvaluationContext): string {
|
||||
const { args } = context;
|
||||
this.callHistory.push(context);
|
||||
const result = this.result || `[expression-stub] args: ${args ? Object.keys(args).map((key) => `${key}: ${args[key]}`).join('", "') : 'none'}`;
|
||||
return result;
|
||||
if (this.result === undefined /* not empty string */) {
|
||||
const { args } = context;
|
||||
return `[expression-stub] args: ${args ? Object.keys(args).map((key) => `${key}: ${args[key]}`).join('", "') : 'none'}`;
|
||||
}
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user