All files / json-path/src ast.ts

96.87% Statements 62/64
100% Branches 0/0
94.59% Functions 35/37
96.82% Lines 61/63

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156    6x 423x 6x 647x   6x 401x 69x   30x 41x   50x 137x     6x         90x         10x 17x       96x   3x   1x     6x 6x     126x 145x       96x         401x 401x       69x 69x       30x   30x 30x 30x         41x       50x 50x       137x 137x         647x 647x         423x       90x   90x 90x 90x         10x   10x 10x 10x         17x 17x       192x   192x 192x         3x 3x       1x 1x       6x               126x 126x       145x 145x    
import type * as types from './types';
 
export class Ast {
  public static path = (segments: types.IPathSegment[]): types.IJSONPath => new JSONPath(segments);
  public static segment = (selectors: types.IAnySelector[], recursive?: boolean): types.IPathSegment =>
    new PathSegment(selectors, recursive);
 
  public static selector = class selector {
    public static named = (name: string): types.INamedSelector => new Named(name);
    public static index = (index: number): types.IIndexSelector => new Index(index);
    public static slice = (start?: number, end?: number, step?: number): types.ISliceSelector =>
      new Slice(start, end, step);
    public static wildcard = (): types.IWildcardSelector => new Wildcard();
    public static recursiveDescent = (selector: types.IAnySelector): types.IRecursiveDescentSelector =>
      new RecursiveDescent(selector);
    public static filter = (expression: types.IFilterExpression): types.IFilterSelector => new Filter(expression);
  };
 
  public static expression = class expression {
    public static comparison = (
      operator: types.IComparisonExpression['operator'],
      left: types.IValueExpression,
      right: types.IValueExpression,
    ): types.IComparisonExpression => new ComparisonExpression(operator, left, right);
    public static logical = (
      operator: types.ILogicalExpression['operator'],
      left: types.IFilterExpression,
      right: types.IFilterExpression,
    ): types.ILogicalExpression => new LogicalExpression(operator, left, right);
    public static existence = (path: types.IJSONPath): types.IExistenceExpression => new ExistenceExpression(path);
    public static function = (
      name: string,
      args: (types.IValueExpression | types.IFilterExpression | types.IJSONPath)[],
    ): types.IFunctionExpression => new FunctionExpression(name, args);
    public static paren = (expression: types.IFilterExpression): types.IParenExpression =>
      new ParenthesizedExpression(expression);
    public static negation = (expression: types.IFilterExpression): types.INegationExpression =>
      new NegationExpression(expression);
  };
 
  public static value = class value {
    public static current = (): types.ICurrentNodeExpression => new CurrentNodeExpression();
    public static root = (): types.IRootNodeExpression => new RootNodeExpression();
    public static literal = (value: string | number | boolean | null): types.ILiteralExpression =>
      new LiteralExpression(value);
    public static path = (path: types.IJSONPath): types.IPathExpression => new PathExpression(path);
    public static function = (
      name: string,
      args: (types.IValueExpression | types.IFilterExpression | types.IJSONPath)[],
    ): types.IFunctionExpression => new FunctionExpression(name, args);
  };
}
 
class Named implements types.INamedSelector {
  public readonly type = 'name' as const;
  constructor(public readonly name: string) {}
}
 
class Index implements types.IIndexSelector {
  public readonly type = 'index' as const;
  constructor(public readonly index: number) {}
}
 
class Slice implements types.ISliceSelector {
  public readonly type = 'slice' as const;
  constructor(
    public readonly start?: number,
    public readonly end?: number,
    public readonly step?: number,
  ) {}
}
 
class Wildcard implements types.IWildcardSelector {
  public readonly type = 'wildcard' as const;
}
 
class RecursiveDescent implements types.IRecursiveDescentSelector {
  public readonly type = 'recursive-descent' as const;
  constructor(public readonly selector: types.IAnySelector) {}
}
 
class Filter implements types.IFilterSelector {
  public readonly type = 'filter' as const;
  constructor(public readonly expression: types.IFilterExpression) {}
}
 
class PathSegment implements types.IPathSegment {
  constructor(
    public readonly selectors: types.IAnySelector[],
    public readonly recursive?: boolean,
  ) {}
}
 
class JSONPath implements types.IJSONPath {
  constructor(public readonly segments: types.IPathSegment[]) {}
}
 
class ComparisonExpression implements types.IComparisonExpression {
  public readonly type = 'comparison' as const;
  constructor(
    public readonly operator: types.IComparisonExpression['operator'],
    public readonly left: types.IValueExpression,
    public readonly right: types.IValueExpression,
  ) {}
}
 
class LogicalExpression implements types.ILogicalExpression {
  public readonly type = 'logical' as const;
  constructor(
    public readonly operator: types.ILogicalExpression['operator'],
    public readonly left: types.IFilterExpression,
    public readonly right: types.IFilterExpression,
  ) {}
}
 
class ExistenceExpression implements types.IExistenceExpression {
  public readonly type = 'existence' as const;
  constructor(public readonly path: types.IJSONPath) {}
}
 
class FunctionExpression implements types.IFunctionExpression {
  public readonly type = 'function' as const;
  constructor(
    public readonly name: string,
    public readonly args: (types.IValueExpression | types.IFilterExpression | types.IJSONPath)[],
  ) {}
}
 
class ParenthesizedExpression implements types.IParenExpression {
  public readonly type = 'paren' as const;
  constructor(public readonly expression: types.IFilterExpression) {}
}
 
class NegationExpression implements types.INegationExpression {
  public readonly type = 'negation' as const;
  constructor(public readonly expression: types.IFilterExpression) {}
}
 
class CurrentNodeExpression implements types.ICurrentNodeExpression {
  public readonly type = 'current' as const;
}
 
class RootNodeExpression implements types.IRootNodeExpression {
  public readonly type = 'root' as const;
}
 
class LiteralExpression implements types.ILiteralExpression {
  public readonly type = 'literal' as const;
  constructor(public readonly value: string | number | boolean | null) {}
}
 
class PathExpression implements types.IPathExpression {
  public readonly type = 'path' as const;
  constructor(public readonly path: types.IJSONPath) {}
}