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

93.1% Statements 27/29
100% Branches 9/9
80% Functions 4/5
93.1% Lines 27/29

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 37  1x     1x 1x       1x 95x 4x 95x 95x   1x 1x 1x 1x 1x   1x 6x 6x   1x 1x 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 ArrayAccessor implements PropertyAccessor {
  public getSpecificTargetClasses(): null {
    return null;
  }
 
  public canRead(_context: EvaluationContext, target: unknown, name: string): boolean {
    if (!Array.isArray(target)) return false;
    const index = parseInt(name, 10);
    return !isNaN(index) && index >= 0 && index < (target as unknown[]).length;
  }
 
  public read(_context: EvaluationContext, target: unknown, name: string): TypedValue {
    const arr = target as unknown[];
    const index = parseInt(name, 10);
    return new TypedValue(arr[index]);
  }
 
  public canWrite(_context: EvaluationContext, target: unknown, _name: string): boolean {
    return Array.isArray(target);
  }
 
  public write(
    _context: EvaluationContext,
    target: unknown,
    name: string,
    newValue: unknown,
  ): void {
    const arr = target as unknown[];
    const index = parseInt(name, 10);
    arr[index] = newValue;
  }
}