All files / src/ast/collection inline-map.ts

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

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   7x 7x   1x 5x 5x 7x 7x 7x 7x 5x 5x   1x 2x 2x 5x 5x 2x 2x 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 InlineMap extends SpelNodeImpl {
  constructor(startPos: number, endPos: number, ...entries: SpelNodeImpl[]) {
    // Entries come in pairs: [key0, value0, key1, value1, ...]
    super(NodeType.INLINE_MAP, startPos, endPos, ...entries);
  }
 
  public getValueInternal(state: ExpressionState): TypedValue {
    const result = new Map<string, unknown>();
    for (let i = 0; i < this.children.length; i += 2) {
      const key = String(this.children[i]!.getValue(state).getValue());
      const value = this.children[i + 1]!.getValue(state).getValue();
      result.set(key, value);
    }
    return new TypedValue(result);
  }
 
  public toStringAST(): string {
    const parts: string[] = [];
    for (let i = 0; i < this.children.length; i += 2) {
      parts.push(`${this.children[i]!.toStringAST()} : ${this.children[i + 1]!.toStringAST()}`);
    }
    return '{' + parts.join(', ') + '}';
  }
}