Skip to content

Commit

Permalink
Add an isMarker() type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 17, 2024
1 parent 4e35a2d commit 5e0d501
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions openrewrite/src/core/markers.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import {randomId, UUID} from "./tree";
import {randomId, Tree, UUID} from "./tree";
import {Parser} from "./parser";

// This allows `isMarker()` to identify any `Marker` implementations
export const MarkerSymbol = Symbol('Marker');

export interface Marker {
[MarkerSymbol]: boolean;
get id(): UUID;

withId(id: UUID): Marker;
}

interface MarkerConstructor {
new (value?: any): Marker;
<T>(value?: T): Marker;
readonly prototype: Marker;
export function isMarker(tree: any): tree is Marker {
// return 'sourcePath' in tree && 'printer' in tree;
return tree && tree[MarkerSymbol] === true;
}

// pretend like we have a class called `Marker` which we can pass around
export declare var Marker: MarkerConstructor;


export class Markers {
public static readonly EMPTY: Markers = new Markers(randomId(), []);

Expand Down Expand Up @@ -50,6 +49,7 @@ export class Markers {
}

export class ParseExceptionResult implements Marker {
[MarkerSymbol] = true;
private readonly _id: UUID;
private readonly _parserType: string;
private readonly _exceptionType: string;
Expand Down

0 comments on commit 5e0d501

Please sign in to comment.