Skip to content

Commit

Permalink
More polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 9, 2024
1 parent 78a6b65 commit 31e5670
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
6 changes: 3 additions & 3 deletions openrewrite/src/core/markers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {random_id, UUID} from "./tree";

export interface Marker {
id(): UUID;
get id(): UUID;

withId(id: UUID): Marker;
}
Expand All @@ -17,15 +17,15 @@ export class Markers {
this._markers = markers;
}

public id(): UUID {
public get id(): UUID {
return this._id;
}

public withId(id: UUID): Markers {
return this._id === id ? this : new Markers(id, this._markers);
}

public markers(): Marker[] {
public get markers(): Marker[] {
return this._markers;
}

Expand Down
43 changes: 36 additions & 7 deletions openrewrite/src/core/tree.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
import {v4 as uuidv4} from 'uuid';
import {Markers} from "./markers";

export type UUID = string;

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<P>(v: TreeVisitor<Tree, P>, p: P): boolean;

accept<R extends Tree, P>(v: TreeVisitor<R, P>, p: P): R | null;
}

export abstract class TreeVisitor<T extends Tree, P> {
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<string, Object>;
Expand All @@ -17,7 +52,7 @@ export class Cursor {
this._messages = new Map<string, Object>();
}

public parent(): Cursor | null {
public get parent(): Cursor | null {
return this._parent;
}

Expand All @@ -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;
}

0 comments on commit 31e5670

Please sign in to comment.