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

92.85% Statements 26/28
100% Branches 8/8
80% Functions 4/5
92.85% Lines 26/28

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  1x     1x 1x       1x 94x 92x 94x   1x 2x 2x 1x 1x 1x 2x   1x 5x 5x   1x 1x 1x 1x 1x 1x 1x 1x 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);
  }
}