|
| 1 | +import {LstType, Markers, Tree, TreeVisitor, UUID} from "../../core"; |
| 2 | +import {JsonVisitor} from "../visitor"; |
| 3 | + |
| 4 | +export interface Json extends Tree { |
| 5 | + get prefix(): Space; |
| 6 | + |
| 7 | + withPrefix(prefix: Space): Json; |
| 8 | + |
| 9 | + get id(): UUID; |
| 10 | + |
| 11 | + withId(id: UUID): Json; |
| 12 | + |
| 13 | + get markers(): Markers; |
| 14 | + |
| 15 | + withMarkers(markers: Markers): Json; |
| 16 | + |
| 17 | + isAcceptable<P>(v: TreeVisitor<Tree, P>, p: P): boolean; |
| 18 | + |
| 19 | + accept<R extends Tree, P>(v: TreeVisitor<R, P>, p: P): R | null; |
| 20 | + |
| 21 | + acceptJson<P>(v: JsonVisitor<P>, p: P): Json | null; |
| 22 | +} |
| 23 | + |
| 24 | +type Constructor<T = {}> = new (...args: any[]) => T; |
| 25 | + |
| 26 | +export function isJson(tree: any): tree is Json { |
| 27 | + return !!tree.constructor.isJson || !!tree.isJson; |
| 28 | +} |
| 29 | + |
| 30 | +export function JsonMixin<TBase extends Constructor<Object>>(Base: TBase) { |
| 31 | + abstract class JsonMixed extends Base implements Json { |
| 32 | + static isTree = true; |
| 33 | + static isJson = true; |
| 34 | + |
| 35 | + abstract get prefix(): Space; |
| 36 | + |
| 37 | + abstract withPrefix(prefix: Space): Json; |
| 38 | + |
| 39 | + abstract get id(): UUID; |
| 40 | + |
| 41 | + abstract withId(id: UUID): Json; |
| 42 | + |
| 43 | + abstract get markers(): Markers; |
| 44 | + |
| 45 | + abstract withMarkers(markers: Markers): Json; |
| 46 | + |
| 47 | + public isAcceptable<P>(v: TreeVisitor<Tree, P>, p: P): boolean { |
| 48 | + return v.isAdaptableTo(JsonVisitor); |
| 49 | + } |
| 50 | + |
| 51 | + public accept<R extends Tree, P>(v: TreeVisitor<R, P>, p: P): R | null { |
| 52 | + return this.acceptJson(v.adapt(JsonVisitor), p) as unknown as R | null; |
| 53 | + } |
| 54 | + |
| 55 | + public acceptJson<P>(v: JsonVisitor<P>, p: P): Json | null { |
| 56 | + return v.defaultValue(this, p) as Json | null; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return JsonMixed; |
| 61 | +} |
| 62 | + |
| 63 | +export interface JsonKey extends Tree { |
| 64 | +} |
| 65 | + |
| 66 | +export interface JsonValue extends Tree { |
| 67 | +} |
| 68 | + |
| 69 | +@LstType("org.openrewrite.json.tree.Space") |
| 70 | +export class Space { |
| 71 | +} |
| 72 | + |
| 73 | +@LstType("org.openrewrite.json.tree.Comment") |
| 74 | +export class Comment { |
| 75 | +} |
| 76 | + |
| 77 | +@LstType("org.openrewrite.json.tree.JsonRightPadded") |
| 78 | +export class JsonRightPadded<T extends Tree> { |
| 79 | + constructor(element: T) { |
| 80 | + this._element = element; |
| 81 | + } |
| 82 | + |
| 83 | + private readonly _element: T; |
| 84 | + |
| 85 | + get element(): T { |
| 86 | + return this._element; |
| 87 | + } |
| 88 | + |
| 89 | + static getElements<T extends Tree>(padded: JsonRightPadded<T>[]) { |
| 90 | + return []; |
| 91 | + } |
| 92 | + |
| 93 | + static withElements<T extends Tree>(padded: JsonRightPadded<T>[], elements: T[]) { |
| 94 | + return []; |
| 95 | + } |
| 96 | + |
| 97 | + static withElement<T extends Tree>(padded: JsonRightPadded<T>, element: T): JsonRightPadded<T> { |
| 98 | + return padded; |
| 99 | + } |
| 100 | + |
| 101 | + withElement(element: T) : JsonRightPadded<T> { |
| 102 | + return undefined!; |
| 103 | + } |
| 104 | +} |
0 commit comments