All files / json-pack/src/ion Import.ts

90.62% Statements 29/32
75% Branches 6/8
100% Functions 5/5
92.59% Lines 25/27

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 38 39 40 41 42 43 44 45 46 47 48 49 506x     6x     6481x     6481x 6481x   6481x 6481x 6481x 46779x 46779x         5x 5x           53232x 51201x       49196x 49196x 46748x 46748x 46748x 46748x 46748x 46748x       2035x 2035x 2035x      
import {type AstNode, ObjAstNode, toAst} from './ast';
import type {SymbolTable} from './types';
 
export class Import {
  public readonly offset: number;
  public length: number;
  protected readonly byText = new Map<string, number>();
 
  constructor(
    public readonly parent: Import | null,
    public readonly symbols: SymbolTable,
  ) {
    this.offset = parent ? parent.offset + parent.length : 1;
    this.length = symbols.length;
    for (let i = 0; i < symbols.length; i++) {
      const symbol = symbols[i];
      this.byText.set(symbol, this.offset + i);
    }
  }
 
  public getId(symbol: string): number | undefined {
    const id = this.byText.get(symbol);
    if (id !== undefined) return id;
    Iif (this.parent) this.parent.getId(symbol);
    return undefined;
  }
 
  public getText(id: number): string | undefined {
    if (id < this.offset) return this.parent ? this.parent.getText(id) : undefined;
    return this.symbols[id - this.offset];
  }
 
  public add(symbol: string): number {
    let id = this.byText.get(symbol);
    if (id !== undefined) return id;
    const length = this.symbols.length;
    id = this.offset + length;
    this.symbols.push(symbol);
    this.length++;
    this.byText.set(symbol, id);
    return id;
  }
 
  public toAst(): ObjAstNode {
    const map = new Map<number, AstNode<unknown>>();
    map.set(7, toAst(this.symbols, this));
    return new ObjAstNode(map);
  }
}