All files / jit-router/src codegen.ts

90.9% Statements 10/11
100% Branches 1/1
75% Functions 3/4
100% Lines 10/10

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 316x 6x         6x       7x               6x   151x 151x 151x       144x 144x      
import {Codegen} from '@jsonjoy.com/codegen/lib/Codegen';
import {JsExpression} from '@jsonjoy.com/codegen/lib/util/JsExpression';
import type {Match} from './router';
 
export type RouteMatcher<Data = unknown> = (route: string) => undefined | Match<Data>;
 
export class RouterCodegenCtx {
  public readonly codegen: Codegen<RouteMatcher>;
 
  constructor() {
    this.codegen = new Codegen<RouteMatcher>({
      args: ['str'],
      prologue: 'str = "" + str; var len = str.length|0;',
      epilogue: 'return undefined;',
    });
  }
}
 
export class RouterCodegenOpts {
  constructor(
    public readonly slice: JsExpression,
    public readonly offset: string,
    public readonly depth: number = 0,
  ) {}
 
  public create(offset: string): RouterCodegenOpts {
    const slice = new JsExpression(() => `str.slice(${offset})`);
    return new RouterCodegenOpts(slice, offset, this.depth + 1);
  }
}