diff --git a/openrewrite/src/core/markers.ts b/openrewrite/src/core/markers.ts index c5e76741..022ed8d3 100644 --- a/openrewrite/src/core/markers.ts +++ b/openrewrite/src/core/markers.ts @@ -1,7 +1,7 @@ import {random_id, UUID} from "./tree"; export interface Marker { - id(): UUID; + get id(): UUID; withId(id: UUID): Marker; } @@ -17,7 +17,7 @@ export class Markers { this._markers = markers; } - public id(): UUID { + public get id(): UUID { return this._id; } @@ -25,7 +25,7 @@ export class Markers { return this._id === id ? this : new Markers(id, this._markers); } - public markers(): Marker[] { + public get markers(): Marker[] { return this._markers; } diff --git a/openrewrite/src/core/tree.ts b/openrewrite/src/core/tree.ts index 88679ed6..53f298a7 100644 --- a/openrewrite/src/core/tree.ts +++ b/openrewrite/src/core/tree.ts @@ -1,4 +1,5 @@ import {v4 as uuidv4} from 'uuid'; +import {Markers} from "./markers"; export type UUID = string; @@ -6,7 +7,41 @@ export const random_id = (): UUID => { return uuidv4(); } +export interface Tree { + get id(): UUID; + + withId(id: UUID): Tree; + + get markers(): Markers; + + withMarkers(markers: Markers): Tree; + + isAcceptable

(v: TreeVisitor, p: P): boolean; + + accept(v: TreeVisitor, p: P): R | null; +} + +export abstract class TreeVisitor { + private _cursor: Cursor; + + protected constructor() { + this._cursor = new Cursor(null, Cursor.ROOT_VALUE); + } + + public get cursor(): Cursor { + return this._cursor; + } + + public set cursor(cursor: Cursor) { + this._cursor = cursor; + } + + abstract visit(tree: Tree | null, p: P): T | null; +} + export class Cursor { + public static ROOT_VALUE: String = "root"; + private readonly _parent: Cursor | null; private readonly _value: Object; private _messages: Map; @@ -17,7 +52,7 @@ export class Cursor { this._messages = new Map(); } - public parent(): Cursor | null { + public get parent(): Cursor | null { return this._parent; } @@ -29,9 +64,3 @@ export class Cursor { return new Cursor(this._parent === null ? null : this._parent.fork(), this.value); } } - -export interface Tree { - id(): UUID; - - withId(id: UUID): Tree; -}