All files / src/ast/operator operand-guards.ts

100% Statements 4/4
100% Branches 2/2
100% Functions 1/1
100% Lines 4/4

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                            2636x 2636x 2623x   13x    
import { SpelEvaluationException } from '../../error/spel-evaluation-exception.js';
import { SpelMessage } from '../../error/spel-message.js';
import type { ExpressionState } from '../../expression-state.js';
import type { SpelNodeImpl } from '../spel-node.js';
 
/**
 * Evaluate an operand as a boolean.
 *
 * Spring's OperatorAnd, OperatorOr and OperatorNot coerce their operands to
 * Boolean and raise a type-conversion error when a value cannot be converted.
 * They therefore reject truthiness: `true and 5` is an error rather than `5`,
 * and `null and true` is an error rather than `null`.
 */
export function requireBoolean(operand: SpelNodeImpl, state: ExpressionState): boolean {
  const value = operand.getValue(state).getValue();
  if (typeof value === 'boolean') {
    return value;
  }
  throw new SpelEvaluationException(operand.startPos, SpelMessage.TYPE_CONVERSION_ERROR);
}