Skip to content

Commit

Permalink
Generate YamlVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 10, 2024
1 parent 14b421c commit 46e3669
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 75 deletions.
57 changes: 42 additions & 15 deletions openrewrite/src/core/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ export abstract class TreeVisitor<T extends Tree, P> {
this._cursor = cursor;
}

abstract visit(tree: Tree | null, p: P): T | null;
visit(tree: Tree | null, p: P): T | null {
// FIXME
return tree as T;
}

protected visitAndCast<T extends Tree>(t: T | null, p: P): T | null {
return this.visit(t, p) as T | null;
}

visitMarkers(markers: Markers | undefined, p: P): Markers {
if (markers == undefined || markers === Markers.EMPTY) {
Expand Down Expand Up @@ -80,7 +87,12 @@ export class Cursor {
}

export class Checksum {
constructor(private readonly _algorithm: string, private readonly _value: ArrayBuffer) {
private readonly _algorithm: string;
private readonly _value: ArrayBuffer;

constructor(algorithm: string, value: ArrayBuffer) {
this._algorithm = algorithm;
this._value = value;
}

get algorithm(): string {
Expand All @@ -93,15 +105,30 @@ export class Checksum {
}

export class FileAttributes {
private readonly _creationTime: Date | undefined;
private readonly _lastModifiedTime: Date | undefined;
private readonly _lastAccessTime: Date | undefined;
private readonly _isReadable: boolean;
private readonly _isWritable: boolean;
private readonly _isExecutable: boolean;
private readonly _size: number;

public constructor(
private readonly _creationTime: Date | undefined,
private readonly _lastModifiedTime: Date | undefined,
private readonly _lastAccessTime: Date | undefined,
private readonly _isReadable: boolean,
private readonly _isWritable: boolean,
private readonly _isExecutable: boolean,
private readonly _size: number,
creationTime: Date | undefined,
lastModifiedTime: Date | undefined,
lastAccessTime: Date | undefined,
isReadable: boolean,
isWritable: boolean,
isExecutable: boolean,
size: number,
) {
this._creationTime = creationTime;
this._lastModifiedTime = lastModifiedTime;
this._lastAccessTime = lastAccessTime;
this._isReadable = isReadable;
this._isWritable = isWritable;
this._isExecutable = isExecutable;
this._size = size;
}

get creationTime(): Date | undefined {
Expand Down Expand Up @@ -167,21 +194,21 @@ export interface SourceFile extends Tree {

withSourcePath(sourcePath: string): SourceFile;

get charsetName(): string | undefined;
get charsetName(): string | null;

withCharsetName(charset: string | undefined): SourceFile;
withCharsetName(charset: string | null): SourceFile;

get charsetBomMarked(): boolean;

withCharsetBomMarked(isCharsetBomMarked: boolean): SourceFile;

get checksum(): Checksum | undefined;
get checksum(): Checksum | null;

withChecksum(checksum: Checksum | undefined): SourceFile;
withChecksum(checksum: Checksum | null): SourceFile;

get fileAttributes(): FileAttributes | undefined;
get fileAttributes(): FileAttributes | null;

withFileAttributes(fileAttributes: FileAttributes | undefined): SourceFile;
withFileAttributes(fileAttributes: FileAttributes | null): SourceFile;
}

type CommentWrapper = (input: string) => string;
Expand Down
4 changes: 2 additions & 2 deletions openrewrite/src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export class ListUtils {
static map<T>(ls: T[] | undefined, map: (item: T) => T): T[] | undefined {
static map<T>(ls: T[] | undefined, map: (item: T) => T): T[] {
if (ls === undefined || ls.length === 0) {
return ls;
return ls as T[];
}

let newLs: T[] = ls;
Expand Down
4 changes: 4 additions & 0 deletions openrewrite/src/yaml/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './markers';
export * from './support_types';
export * from './tree';
export * from './visitor';
1 change: 1 addition & 0 deletions openrewrite/src/yaml/markers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
2 changes: 1 addition & 1 deletion openrewrite/src/yaml/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export abstract class Yaml implements Tree {
abstract withMarkers(markers: Markers): Tree;
}

export interface YamlKey {
export interface YamlKey extends Tree {
// get value(): string;

withPrefix(prefix: string): YamlKey;
Expand Down
Loading

0 comments on commit 46e3669

Please sign in to comment.