All files / src spel-expression.ts

100% Statements 48/48
100% Branches 12/12
100% Functions 12/12
100% Lines 48/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65  1x   1x     1x   1x 5119x 5119x   5119x 5119x 5119x 5119x   5119x 4x 4x   5119x 1x 1x   5119x 5086x 5086x 5086x 5086x   5119x 4944x 4944x 4944x   5119x 1x 1x 1x   5119x 1x 1x 1x   5119x 2x 2x 2x   5119x 4x 4x 4x   5119x 1x 1x   5119x 15x 15x 5119x  
import type { SpelNodeImpl } from './ast/spel-node.js';
import { ExpressionState } from './expression-state.js';
import type { EvaluationContext } from './evaluation-context/evaluation-context.js';
import { StandardEvaluationContext } from './standard-evaluation-context.js';
import type { TypedValue } from './typed-value.js';
import type { SpelReference } from './language/reference-extractor.js';
import { SpelReferenceExtractor } from './language/reference-extractor.js';
 
export class SpelExpression {
  public readonly expressionString: string;
  private readonly ast: SpelNodeImpl;
 
  constructor(expressionString: string, ast: SpelNodeImpl) {
    this.expressionString = expressionString;
    this.ast = ast;
  }
 
  public getAST(): SpelNodeImpl {
    return this.ast;
  }
 
  public getReferences(): SpelReference[] {
    return SpelReferenceExtractor.extractFromAst(this.ast);
  }
 
  public getValueWithContext(context: EvaluationContext): unknown {
    const state = new ExpressionState(context);
    const result = this.ast.getValue(state);
    return result.getValue();
  }
 
  public getValue(rootObject?: unknown): unknown {
    const context = new StandardEvaluationContext(rootObject);
    return this.getValueWithContext(context);
  }
 
  public getTypedValue(context: EvaluationContext): TypedValue {
    const state = new ExpressionState(context);
    return this.ast.getValue(state);
  }
 
  public getValueType(context: EvaluationContext): unknown {
    const state = new ExpressionState(context);
    return this.ast.getValueType(state);
  }
 
  public isWritable(context: EvaluationContext): boolean {
    const state = new ExpressionState(context);
    return this.ast.isWritable(state);
  }
 
  public setValue(context: EvaluationContext, newValue: unknown): void {
    const state = new ExpressionState(context);
    this.ast.setValue(state, newValue);
  }
 
  public getExpressionString(): string {
    return this.expressionString;
  }
 
  public toStringAST(): string {
    return this.ast.toStringAST();
  }
}