All files / src/evaluation-context map-accessor.ts

90% Statements 9/10
100% Branches 6/6
80% Functions 4/5
88.88% Lines 8/9

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                    207x 205x       2x 2x 1x   1x       5x                 1x      
import type { EvaluationContext } from './evaluation-context.js';
import { TypedValue } from '../typed-value.js';
import type { PropertyAccessor } from './property-accessor.js';
 
export class MapAccessor implements PropertyAccessor {
  public getSpecificTargetClasses(): null {
    return null;
  }
 
  public canRead(_context: EvaluationContext, target: unknown, _name: string): boolean {
    if (target === null || target === undefined) return false;
    return target instanceof Map;
  }
 
  public read(_context: EvaluationContext, target: unknown, name: string): TypedValue {
    const map = target as Map<string, unknown>;
    if (!map.has(name)) {
      return TypedValue.NULL;
    }
    return new TypedValue(map.get(name));
  }
 
  public canWrite(_context: EvaluationContext, target: unknown, _name: string): boolean {
    return target instanceof Map;
  }
 
  public write(
    _context: EvaluationContext,
    target: unknown,
    name: string,
    newValue: unknown,
  ): void {
    (target as Map<string, unknown>).set(name, newValue);
  }
}