All files / src/ast/control-flow ternary.ts

100% Statements 25/25
100% Branches 5/5
100% Functions 3/3
100% Lines 25/25

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  1x 1x 1x   1x 1x 20x 20x 20x 20x 20x 20x 20x 20x   1x 17x 17x 11x 17x 6x 6x 17x   1x 1x 1x 1x  
import type { ExpressionState } from '../../expression-state.js';
import { TypedValue } from '../../typed-value.js';
import { SpelNodeImpl } from '../spel-node.js';
import { NodeType } from '../../language/node-type.js';
 
export class Ternary extends SpelNodeImpl {
  constructor(
    startPos: number,
    endPos: number,
    condition: SpelNodeImpl,
    trueExpr: SpelNodeImpl,
    falseExpr: SpelNodeImpl,
  ) {
    super(NodeType.TERNARY, startPos, endPos, condition, trueExpr, falseExpr);
  }
 
  public getValueInternal(state: ExpressionState): TypedValue {
    const conditionVal = this.children[0]!.getValue(state).getValue();
    if (conditionVal) {
      return this.children[1]!.getValue(state);
    } else {
      return this.children[2]!.getValue(state);
    }
  }
 
  public toStringAST(): string {
    return `(${this.children[0]!.toStringAST()} ? ${this.children[1]!.toStringAST()} : ${this.children[2]!.toStringAST()})`;
  }
}