diff --git a/openrewrite/src/json/extensions.ts b/openrewrite/src/json/extensions.ts index 1a1b0b47..8cf0b045 100644 --- a/openrewrite/src/json/extensions.ts +++ b/openrewrite/src/json/extensions.ts @@ -1,5 +1,5 @@ import {JsonVisitor} from "./visitor"; -import {JsonRightPadded, Space} from "./support_types"; +import {JsonRightPadded, Space} from "./tree"; import {Tree} from "../core"; export function visitRightPadded(v: JsonVisitor

, right: JsonRightPadded | null, p: P): JsonRightPadded { diff --git a/openrewrite/src/json/index.ts b/openrewrite/src/json/index.ts index 432d1988..fc538818 100644 --- a/openrewrite/src/json/index.ts +++ b/openrewrite/src/json/index.ts @@ -1,4 +1,3 @@ export * from './markers'; -export * from './support_types'; export * from './tree'; export * from './visitor'; diff --git a/openrewrite/src/json/support_types.ts b/openrewrite/src/json/support_types.ts deleted file mode 100644 index 12014e7e..00000000 --- a/openrewrite/src/json/support_types.ts +++ /dev/null @@ -1,44 +0,0 @@ -import {LstType, Tree} from "../core"; - -export interface JsonKey extends Tree { -} - -export interface JsonValue extends Tree { -} - -@LstType("org.openrewrite.json.tree.Space") -export class Space { -} - -@LstType("org.openrewrite.json.tree.Comment") -export class Comment { -} - -@LstType("org.openrewrite.json.tree.JsonRightPadded") -export class JsonRightPadded { - constructor(element: T) { - this._element = element; - } - - private readonly _element: T; - - get element(): T { - return this._element; - } - - static getElements(padded: JsonRightPadded[]) { - return []; - } - - static withElements(padded: JsonRightPadded[], elements: T[]) { - return []; - } - - static withElement(padded: JsonRightPadded, element: T): JsonRightPadded { - return padded; - } - - withElement(element: T) : JsonRightPadded { - return undefined!; - } -} \ No newline at end of file diff --git a/openrewrite/src/json/tree/extensions.ts b/openrewrite/src/json/tree/extensions.ts new file mode 100644 index 00000000..336ce12b --- /dev/null +++ b/openrewrite/src/json/tree/extensions.ts @@ -0,0 +1 @@ +export {} diff --git a/openrewrite/src/json/tree/index.ts b/openrewrite/src/json/tree/index.ts new file mode 100644 index 00000000..cede2203 --- /dev/null +++ b/openrewrite/src/json/tree/index.ts @@ -0,0 +1,2 @@ +export * from './support_types'; +export * from './tree'; diff --git a/openrewrite/src/json/tree/support_types.ts b/openrewrite/src/json/tree/support_types.ts new file mode 100644 index 00000000..1cd94e4e --- /dev/null +++ b/openrewrite/src/json/tree/support_types.ts @@ -0,0 +1,104 @@ +import {LstType, Markers, Tree, TreeVisitor, UUID} from "../../core"; +import {JsonVisitor} from "../visitor"; + +export interface Json extends Tree { + get prefix(): Space; + + withPrefix(prefix: Space): Json; + + get id(): UUID; + + withId(id: UUID): Json; + + get markers(): Markers; + + withMarkers(markers: Markers): Json; + + isAcceptable

(v: TreeVisitor, p: P): boolean; + + accept(v: TreeVisitor, p: P): R | null; + + acceptJson

(v: JsonVisitor

, p: P): Json | null; +} + +type Constructor = new (...args: any[]) => T; + +export function isJson(tree: any): tree is Json { + return !!tree.constructor.isJson || !!tree.isJson; +} + +export function JsonMixin>(Base: TBase) { + abstract class JsonMixed extends Base implements Json { + static isTree = true; + static isJson = true; + + abstract get prefix(): Space; + + abstract withPrefix(prefix: Space): Json; + + abstract get id(): UUID; + + abstract withId(id: UUID): Json; + + abstract get markers(): Markers; + + abstract withMarkers(markers: Markers): Json; + + public isAcceptable

(v: TreeVisitor, p: P): boolean { + return v.isAdaptableTo(JsonVisitor); + } + + public accept(v: TreeVisitor, p: P): R | null { + return this.acceptJson(v.adapt(JsonVisitor), p) as unknown as R | null; + } + + public acceptJson

(v: JsonVisitor

, p: P): Json | null { + return v.defaultValue(this, p) as Json | null; + } + } + + return JsonMixed; +} + +export interface JsonKey extends Tree { +} + +export interface JsonValue extends Tree { +} + +@LstType("org.openrewrite.json.tree.Space") +export class Space { +} + +@LstType("org.openrewrite.json.tree.Comment") +export class Comment { +} + +@LstType("org.openrewrite.json.tree.JsonRightPadded") +export class JsonRightPadded { + constructor(element: T) { + this._element = element; + } + + private readonly _element: T; + + get element(): T { + return this._element; + } + + static getElements(padded: JsonRightPadded[]) { + return []; + } + + static withElements(padded: JsonRightPadded[], elements: T[]) { + return []; + } + + static withElement(padded: JsonRightPadded, element: T): JsonRightPadded { + return padded; + } + + withElement(element: T) : JsonRightPadded { + return undefined!; + } +} diff --git a/openrewrite/src/json/tree.ts b/openrewrite/src/json/tree/tree.ts similarity index 66% rename from openrewrite/src/json/tree.ts rename to openrewrite/src/json/tree/tree.ts index 82b9c796..c78417bb 100644 --- a/openrewrite/src/json/tree.ts +++ b/openrewrite/src/json/tree/tree.ts @@ -1,44 +1,20 @@ // noinspection JSUnusedGlobalSymbols import * as extensions from "./extensions"; -import {Comment, JsonKey, JsonRightPadded, JsonValue, Space} from "./support_types"; -import {JsonVisitor} from "./visitor"; -import {Checksum, Cursor, FileAttributes, LstType, Markers, PrintOutputCapture, PrinterFactory, SourceFile, SourceFileMixin, Tree, TreeVisitor, UUID} from "../core"; - -export abstract class Json implements Tree { - abstract get id(): UUID; - - abstract withId(id: UUID): Tree; - - abstract get markers(): Markers; - - abstract withMarkers(markers: Markers): Tree; - - public isAcceptable

(v: TreeVisitor, p: P): boolean { - return v.isAdaptableTo(JsonVisitor); +import {Json, JsonMixin, Comment, JsonKey, JsonRightPadded, JsonValue, Space} from "./support_types"; +import {JsonVisitor} from "../visitor"; +import {Checksum, Cursor, FileAttributes, LstType, Markers, PrintOutputCapture, PrinterFactory, SourceFile, SourceFileMixin, Tree, TreeVisitor, UUID} from "../../core"; + +@LstType("org.openrewrite.json.tree.Json$Array") +export class Array extends JsonMixin(Object) implements JsonValue { + public constructor(id: UUID, prefix: Space, markers: Markers, values: JsonRightPadded[]) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._values = values; } - public accept(v: TreeVisitor, p: P): R | null { - return this.acceptJson(v.adapt(JsonVisitor), p) as unknown as R | null; - } - - public acceptJson

(v: JsonVisitor

, p: P): Json | null { - return v.defaultValue(this, p) as Json | null; - } - -} - -export namespace Json { - @LstType("org.openrewrite.json.tree.Json$Array") - export class Array extends Json implements JsonValue { - public constructor(id: UUID, prefix: Space, markers: Markers, values: JsonRightPadded[]) { - super(); - this._id = id; - this._prefix = prefix; - this._markers = markers; - this._values = values; - } - private readonly _id: UUID; public get id(): UUID { @@ -79,39 +55,39 @@ export namespace Json { return this.padding.withValues(JsonRightPadded.withElements(this._values, values)); } - public acceptJson

(v: JsonVisitor

, p: P): Json | null { - return v.visitArray(this, p); - } + public acceptJson

(v: JsonVisitor

, p: P): Json | null { + return v.visitArray(this, p); + } - get padding() { - const t = this; - return new class { - public get values(): JsonRightPadded[] { - return t._values; - } - public withValues(values: JsonRightPadded[]): Array { - return t._values === values ? t : new Json.Array(t._id, t._prefix, t._markers, values); - } + get padding() { + const t = this; + return new class { + public get values(): JsonRightPadded[] { + return t._values; + } + public withValues(values: JsonRightPadded[]): Array { + return t._values === values ? t : new Array(t._id, t._prefix, t._markers, values); } } - } - @LstType("org.openrewrite.json.tree.Json$Document") - export class Document extends SourceFileMixin(Json) { - public constructor(id: UUID, sourcePath: string, prefix: Space, markers: Markers, charsetName: string | null, charsetBomMarked: boolean, checksum: Checksum | null, fileAttributes: FileAttributes | null, value: JsonValue, eof: Space) { - super(); - this._id = id; - this._sourcePath = sourcePath; - this._prefix = prefix; - this._markers = markers; - this._charsetName = charsetName; - this._charsetBomMarked = charsetBomMarked; - this._checksum = checksum; - this._fileAttributes = fileAttributes; - this._value = value; - this._eof = eof; - } +} + +@LstType("org.openrewrite.json.tree.Json$Document") +export class Document extends SourceFileMixin(JsonMixin(Object)) { + public constructor(id: UUID, sourcePath: string, prefix: Space, markers: Markers, charsetName: string | null, charsetBomMarked: boolean, checksum: Checksum | null, fileAttributes: FileAttributes | null, value: JsonValue, eof: Space) { + super(); + this._id = id; + this._sourcePath = sourcePath; + this._prefix = prefix; + this._markers = markers; + this._charsetName = charsetName; + this._charsetBomMarked = charsetBomMarked; + this._checksum = checksum; + this._fileAttributes = fileAttributes; + this._value = value; + this._eof = eof; + } private readonly _id: UUID; @@ -213,24 +189,24 @@ export namespace Json { return eof === this._eof ? this : new Document(this._id, this._sourcePath, this._prefix, this._markers, this._charsetName, this._charsetBomMarked, this._checksum, this._fileAttributes, this._value, eof); } - public printer

(cursor: Cursor): TreeVisitor> { - return PrinterFactory.current.createPrinter(cursor); - } - - public acceptJson

(v: JsonVisitor

, p: P): Json | null { - return v.visitDocument(this, p); - } + public printer

(cursor: Cursor): TreeVisitor> { + return PrinterFactory.current.createPrinter(cursor); + } + public acceptJson

(v: JsonVisitor

, p: P): Json | null { + return v.visitDocument(this, p); } - @LstType("org.openrewrite.json.tree.Json$Empty") - export class Empty extends Json implements JsonValue { - public constructor(id: UUID, prefix: Space, markers: Markers) { - super(); - this._id = id; - this._prefix = prefix; - this._markers = markers; - } +} + +@LstType("org.openrewrite.json.tree.Json$Empty") +export class Empty extends JsonMixin(Object) implements JsonValue { + public constructor(id: UUID, prefix: Space, markers: Markers) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + } private readonly _id: UUID; @@ -262,21 +238,21 @@ export namespace Json { return markers === this._markers ? this : new Empty(this._id, this._prefix, markers); } - public acceptJson

(v: JsonVisitor

, p: P): Json | null { - return v.visitEmpty(this, p); - } - + public acceptJson

(v: JsonVisitor

, p: P): Json | null { + return v.visitEmpty(this, p); } - @LstType("org.openrewrite.json.tree.Json$Identifier") - export class Identifier extends Json implements JsonKey { - public constructor(id: UUID, prefix: Space, markers: Markers, name: string) { - super(); - this._id = id; - this._prefix = prefix; - this._markers = markers; - this._name = name; - } +} + +@LstType("org.openrewrite.json.tree.Json$Identifier") +export class Identifier extends JsonMixin(Object) implements JsonKey { + public constructor(id: UUID, prefix: Space, markers: Markers, name: string) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._name = name; + } private readonly _id: UUID; @@ -318,22 +294,22 @@ export namespace Json { return name === this._name ? this : new Identifier(this._id, this._prefix, this._markers, name); } - public acceptJson

(v: JsonVisitor

, p: P): Json | null { - return v.visitIdentifier(this, p); - } - + public acceptJson

(v: JsonVisitor

, p: P): Json | null { + return v.visitIdentifier(this, p); } - @LstType("org.openrewrite.json.tree.Json$Literal") - export class Literal extends Json implements JsonValue, JsonKey { - public constructor(id: UUID, prefix: Space, markers: Markers, source: string, value: Object) { - super(); - this._id = id; - this._prefix = prefix; - this._markers = markers; - this._source = source; - this._value = value; - } +} + +@LstType("org.openrewrite.json.tree.Json$Literal") +export class Literal extends JsonMixin(Object) implements JsonValue, JsonKey { + public constructor(id: UUID, prefix: Space, markers: Markers, source: string, value: Object | null) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._source = source; + this._value = value; + } private readonly _id: UUID; @@ -375,32 +351,32 @@ export namespace Json { return source === this._source ? this : new Literal(this._id, this._prefix, this._markers, source, this._value); } - private readonly _value: Object; + private readonly _value: Object | null; - public get value(): Object { + public get value(): Object | null { return this._value; } - public withValue(value: Object): Literal { + public withValue(value: Object | null): Literal { return value === this._value ? this : new Literal(this._id, this._prefix, this._markers, this._source, value); } - public acceptJson

(v: JsonVisitor

, p: P): Json | null { - return v.visitLiteral(this, p); - } - + public acceptJson

(v: JsonVisitor

, p: P): Json | null { + return v.visitLiteral(this, p); } - @LstType("org.openrewrite.json.tree.Json$Member") - export class Member extends Json { - public constructor(id: UUID, prefix: Space, markers: Markers, key: JsonRightPadded, value: JsonValue) { - super(); - this._id = id; - this._prefix = prefix; - this._markers = markers; - this._key = key; - this._value = value; - } +} + +@LstType("org.openrewrite.json.tree.Json$Member") +export class Member extends JsonMixin(Object) { + public constructor(id: UUID, prefix: Space, markers: Markers, key: JsonRightPadded, value: JsonValue) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._key = key; + this._value = value; + } private readonly _id: UUID; @@ -452,33 +428,33 @@ export namespace Json { return value === this._value ? this : new Member(this._id, this._prefix, this._markers, this._key, value); } - public acceptJson

(v: JsonVisitor

, p: P): Json | null { - return v.visitMember(this, p); - } + public acceptJson

(v: JsonVisitor

, p: P): Json | null { + return v.visitMember(this, p); + } - get padding() { - const t = this; - return new class { - public get key(): JsonRightPadded { - return t._key; - } - public withKey(key: JsonRightPadded): Member { - return t._key === key ? t : new Json.Member(t._id, t._prefix, t._markers, key, t._value); - } + get padding() { + const t = this; + return new class { + public get key(): JsonRightPadded { + return t._key; + } + public withKey(key: JsonRightPadded): Member { + return t._key === key ? t : new Member(t._id, t._prefix, t._markers, key, t._value); } } - } - @LstType("org.openrewrite.json.tree.Json$JsonObject") - export class JsonObject extends Json implements JsonValue { - public constructor(id: UUID, prefix: Space, markers: Markers, members: JsonRightPadded[]) { - super(); - this._id = id; - this._prefix = prefix; - this._markers = markers; - this._members = members; - } +} + +@LstType("org.openrewrite.json.tree.Json$JsonObject") +export class JsonObject extends JsonMixin(Object) implements JsonValue { + public constructor(id: UUID, prefix: Space, markers: Markers, members: JsonRightPadded[]) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._members = members; + } private readonly _id: UUID; @@ -520,22 +496,20 @@ export namespace Json { return this.padding.withMembers(JsonRightPadded.withElements(this._members, members)); } - public acceptJson

(v: JsonVisitor

, p: P): Json | null { - return v.visitObject(this, p); - } + public acceptJson

(v: JsonVisitor

, p: P): Json | null { + return v.visitObject(this, p); + } - get padding() { - const t = this; - return new class { - public get members(): JsonRightPadded[] { - return t._members; - } - public withMembers(members: JsonRightPadded[]): JsonObject { - return t._members === members ? t : new Json.JsonObject(t._id, t._prefix, t._markers, members); - } + get padding() { + const t = this; + return new class { + public get members(): JsonRightPadded[] { + return t._members; + } + public withMembers(members: JsonRightPadded[]): JsonObject { + return t._members === members ? t : new JsonObject(t._id, t._prefix, t._markers, members); } } - } } diff --git a/openrewrite/src/json/visitor.ts b/openrewrite/src/json/visitor.ts index efa23d3f..eb5a612a 100644 --- a/openrewrite/src/json/visitor.ts +++ b/openrewrite/src/json/visitor.ts @@ -1,21 +1,21 @@ import * as extensions from "./extensions"; import {ListUtils, SourceFile, Tree, TreeVisitor} from "../core"; -import {Comment, JsonKey, JsonRightPadded, JsonValue, Space} from "./support_types"; -import {Json} from "./tree"; +import {Json, isJson, Comment, JsonKey, JsonRightPadded, JsonValue, Space} from "./tree"; +import {Array, Document, Empty, Identifier, Literal, Member, JsonObject} from "./tree"; export class JsonVisitor

extends TreeVisitor { isAcceptable(sourceFile: SourceFile, p: P): boolean { - return sourceFile instanceof Json; + return isJson(sourceFile); } - public visitArray(array: Json.Array, p: P): Json | null { + public visitArray(array: Array, p: P): Json | null { array = array.withPrefix(this.visitSpace(array.prefix, p)!); array = array.withMarkers(this.visitMarkers(array.markers, p)); array = array.padding.withValues(ListUtils.map(array.padding.values, el => this.visitRightPadded(el, p))); return array; } - public visitDocument(document: Json.Document, p: P): Json | null { + public visitDocument(document: Document, p: P): Json | null { document = document.withPrefix(this.visitSpace(document.prefix, p)!); document = document.withMarkers(this.visitMarkers(document.markers, p)); document = document.withValue(this.visitAndCast(document.value, p)!); @@ -23,25 +23,25 @@ export class JsonVisitor

extends TreeVisitor { return document; } - public visitEmpty(empty: Json.Empty, p: P): Json | null { + public visitEmpty(empty: Empty, p: P): Json | null { empty = empty.withPrefix(this.visitSpace(empty.prefix, p)!); empty = empty.withMarkers(this.visitMarkers(empty.markers, p)); return empty; } - public visitIdentifier(identifier: Json.Identifier, p: P): Json | null { + public visitIdentifier(identifier: Identifier, p: P): Json | null { identifier = identifier.withPrefix(this.visitSpace(identifier.prefix, p)!); identifier = identifier.withMarkers(this.visitMarkers(identifier.markers, p)); return identifier; } - public visitLiteral(literal: Json.Literal, p: P): Json | null { + public visitLiteral(literal: Literal, p: P): Json | null { literal = literal.withPrefix(this.visitSpace(literal.prefix, p)!); literal = literal.withMarkers(this.visitMarkers(literal.markers, p)); return literal; } - public visitMember(member: Json.Member, p: P): Json | null { + public visitMember(member: Member, p: P): Json | null { member = member.withPrefix(this.visitSpace(member.prefix, p)!); member = member.withMarkers(this.visitMarkers(member.markers, p)); member = member.padding.withKey(this.visitRightPadded(member.padding.key, p)!); @@ -49,7 +49,7 @@ export class JsonVisitor

extends TreeVisitor { return member; } - public visitObject(jsonObject: Json.JsonObject, p: P): Json | null { + public visitObject(jsonObject: JsonObject, p: P): Json | null { jsonObject = jsonObject.withPrefix(this.visitSpace(jsonObject.prefix, p)!); jsonObject = jsonObject.withMarkers(this.visitMarkers(jsonObject.markers, p)); jsonObject = jsonObject.padding.withMembers(ListUtils.map(jsonObject.padding.members, el => this.visitRightPadded(el, p))); diff --git a/openrewrite/src/yaml/extensions.ts b/openrewrite/src/yaml/extensions.ts new file mode 100644 index 00000000..336ce12b --- /dev/null +++ b/openrewrite/src/yaml/extensions.ts @@ -0,0 +1 @@ +export {} diff --git a/openrewrite/src/yaml/remote/receiver.ts b/openrewrite/src/yaml/remote/receiver.ts index 3b793ffd..6cc5932f 100644 --- a/openrewrite/src/yaml/remote/receiver.ts +++ b/openrewrite/src/yaml/remote/receiver.ts @@ -1,8 +1,8 @@ import * as extensions from "./remote_extensions"; import {Checksum, Cursor, FileAttributes, ListUtils, Tree} from '../../core'; import {DetailsReceiver, Receiver, ReceiverContext, ReceiverFactory, ValueType} from '@openrewrite/rewrite-remote'; -import {YamlVisitor} from '../visitor'; -import {Yaml, YamlKey, Documents, Document, Block, Scalar, Mapping, Sequence, Alias, Anchor} from '../tree'; +import {YamlVisitor} from '..'; +import {Yaml, YamlKey, Documents, Document, Block, Scalar, Mapping, Sequence, Alias, Anchor, Tag} from '../tree'; export class YamlReceiver implements Receiver { public fork(ctx: ReceiverContext): ReceiverContext { @@ -61,6 +61,7 @@ class Visitor extends YamlVisitor { scalar = scalar.withMarkers(ctx.receiveNode(scalar.markers, ctx.receiveMarkers)!); scalar = scalar.withStyle(ctx.receiveValue(scalar.style, ValueType.Enum)!); scalar = scalar.withAnchor(ctx.receiveNode(scalar.anchor, ctx.receiveTree)); + scalar = scalar.withTag(ctx.receiveNode(scalar.tag, ctx.receiveTree)); scalar = scalar.withValue(ctx.receiveValue(scalar.value, ValueType.Primitive)!); return scalar; } @@ -72,6 +73,7 @@ class Visitor extends YamlVisitor { mapping = mapping.withEntries(ctx.receiveNodes(mapping.entries, ctx.receiveTree)!); mapping = mapping.withClosingBracePrefix(ctx.receiveValue(mapping.closingBracePrefix, ValueType.Primitive)); mapping = mapping.withAnchor(ctx.receiveNode(mapping.anchor, ctx.receiveTree)); + mapping = mapping.withTag(ctx.receiveNode(mapping.tag, ctx.receiveTree)); return mapping; } @@ -92,6 +94,7 @@ class Visitor extends YamlVisitor { sequence = sequence.withEntries(ctx.receiveNodes(sequence.entries, ctx.receiveTree)!); sequence = sequence.withClosingBracketPrefix(ctx.receiveValue(sequence.closingBracketPrefix, ValueType.Primitive)); sequence = sequence.withAnchor(ctx.receiveNode(sequence.anchor, ctx.receiveTree)); + sequence = sequence.withTag(ctx.receiveNode(sequence.tag, ctx.receiveTree)); return sequence; } @@ -122,6 +125,16 @@ class Visitor extends YamlVisitor { return anchor; } + public visitTag(tag: Tag, ctx: ReceiverContext): Yaml { + tag = tag.withId(ctx.receiveValue(tag.id, ValueType.UUID)!); + tag = tag.withPrefix(ctx.receiveValue(tag.prefix, ValueType.Primitive)!); + tag = tag.withMarkers(ctx.receiveNode(tag.markers, ctx.receiveMarkers)!); + tag = tag.withName(ctx.receiveValue(tag.name, ValueType.Primitive)!); + tag = tag.withSuffix(ctx.receiveValue(tag.suffix, ValueType.Primitive)!); + tag = tag.withKind(ctx.receiveValue(tag.kind, ValueType.Enum)!); + return tag; + } + } class Factory implements ReceiverFactory { @@ -166,6 +179,7 @@ class Factory implements ReceiverFactory { ctx.receiveNode(null, ctx.receiveMarkers)!, ctx.receiveValue(null, ValueType.Enum)!, ctx.receiveNode(null, ctx.receiveTree), + ctx.receiveNode(null, ctx.receiveTree), ctx.receiveValue(null, ValueType.Primitive)! ); } @@ -177,7 +191,8 @@ class Factory implements ReceiverFactory { ctx.receiveValue(null, ValueType.Primitive), ctx.receiveNodes(null, ctx.receiveTree)!, ctx.receiveValue(null, ValueType.Primitive), - ctx.receiveNode(null, ctx.receiveTree) + ctx.receiveNode(null, ctx.receiveTree), + ctx.receiveNode(null, ctx.receiveTree) ); } @@ -199,7 +214,8 @@ class Factory implements ReceiverFactory { ctx.receiveValue(null, ValueType.Primitive), ctx.receiveNodes(null, ctx.receiveTree)!, ctx.receiveValue(null, ValueType.Primitive), - ctx.receiveNode(null, ctx.receiveTree) + ctx.receiveNode(null, ctx.receiveTree), + ctx.receiveNode(null, ctx.receiveTree) ); } @@ -233,6 +249,17 @@ class Factory implements ReceiverFactory { ); } + if (type === "org.openrewrite.yaml.tree.Yaml$Tag") { + return new Tag( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveValue(null, ValueType.Primitive)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveValue(null, ValueType.Primitive)!, + ctx.receiveValue(null, ValueType.Primitive)!, + ctx.receiveValue(null, ValueType.Enum)! + ); + } + throw new Error("No factory method for type: " + type); } } diff --git a/openrewrite/src/yaml/remote/sender.ts b/openrewrite/src/yaml/remote/sender.ts index 3d715820..83b0d3de 100644 --- a/openrewrite/src/yaml/remote/sender.ts +++ b/openrewrite/src/yaml/remote/sender.ts @@ -1,8 +1,8 @@ import * as extensions from "./remote_extensions"; import {Cursor, ListUtils, Tree} from '../../core'; import {Sender, SenderContext, ValueType} from '@openrewrite/rewrite-remote'; -import {Yaml, YamlKey, Documents, Document, Block, Scalar, Mapping, Sequence, Alias, Anchor} from '../tree'; -import {YamlVisitor} from '../visitor'; +import {YamlVisitor} from '..'; +import {Yaml, YamlKey, Documents, Document, Block, Scalar, Mapping, Sequence, Alias, Anchor, Tag} from '../tree'; export class YamlSender implements Sender { public send(after: Yaml, before: Yaml | null, ctx: SenderContext): void { @@ -56,6 +56,7 @@ class Visitor extends YamlVisitor { ctx.sendNode(scalar, v => v.markers, ctx.sendMarkers); ctx.sendValue(scalar, v => v.style, ValueType.Enum); ctx.sendNode(scalar, v => v.anchor, ctx.sendTree); + ctx.sendNode(scalar, v => v.tag, ctx.sendTree); ctx.sendValue(scalar, v => v.value, ValueType.Primitive); return scalar; } @@ -67,6 +68,7 @@ class Visitor extends YamlVisitor { ctx.sendNodes(mapping, v => v.entries, ctx.sendTree, t => t.id); ctx.sendValue(mapping, v => v.closingBracePrefix, ValueType.Primitive); ctx.sendNode(mapping, v => v.anchor, ctx.sendTree); + ctx.sendNode(mapping, v => v.tag, ctx.sendTree); return mapping; } @@ -87,6 +89,7 @@ class Visitor extends YamlVisitor { ctx.sendNodes(sequence, v => v.entries, ctx.sendTree, t => t.id); ctx.sendValue(sequence, v => v.closingBracketPrefix, ValueType.Primitive); ctx.sendNode(sequence, v => v.anchor, ctx.sendTree); + ctx.sendNode(sequence, v => v.tag, ctx.sendTree); return sequence; } @@ -117,4 +120,14 @@ class Visitor extends YamlVisitor { return anchor; } + public visitTag(tag: Tag, ctx: SenderContext): Yaml { + ctx.sendValue(tag, v => v.id, ValueType.UUID); + ctx.sendValue(tag, v => v.prefix, ValueType.Primitive); + ctx.sendNode(tag, v => v.markers, ctx.sendMarkers); + ctx.sendValue(tag, v => v.name, ValueType.Primitive); + ctx.sendValue(tag, v => v.suffix, ValueType.Primitive); + ctx.sendValue(tag, v => v.kind, ValueType.Enum); + return tag; + } + } diff --git a/openrewrite/src/yaml/tree/tree.ts b/openrewrite/src/yaml/tree/tree.ts index 41ea606d..15623482 100644 --- a/openrewrite/src/yaml/tree/tree.ts +++ b/openrewrite/src/yaml/tree/tree.ts @@ -251,13 +251,14 @@ export interface Block extends Yaml { @LstType("org.openrewrite.yaml.tree.Yaml$Scalar") export class Scalar extends YamlMixin(Object) implements Block, YamlKey { - public constructor(id: UUID, prefix: string, markers: Markers, style: Scalar.Style, anchor: Anchor | null, value: string) { + public constructor(id: UUID, prefix: string, markers: Markers, style: Scalar.Style, anchor: Anchor | null, tag: Tag | null, value: string) { super(); this._id = id; this._prefix = prefix; this._markers = markers; this._style = style; this._anchor = anchor; + this._tag = tag; this._value = value; } @@ -268,7 +269,7 @@ export class Scalar extends YamlMixin(Object) implements Block, YamlKey { } public withId(id: UUID): Scalar { - return id === this._id ? this : new Scalar(id, this._prefix, this._markers, this._style, this._anchor, this._value); + return id === this._id ? this : new Scalar(id, this._prefix, this._markers, this._style, this._anchor, this._tag, this._value); } private readonly _prefix: string; @@ -278,7 +279,7 @@ export class Scalar extends YamlMixin(Object) implements Block, YamlKey { } public withPrefix(prefix: string): Scalar { - return prefix === this._prefix ? this : new Scalar(this._id, prefix, this._markers, this._style, this._anchor, this._value); + return prefix === this._prefix ? this : new Scalar(this._id, prefix, this._markers, this._style, this._anchor, this._tag, this._value); } private readonly _markers: Markers; @@ -288,7 +289,7 @@ export class Scalar extends YamlMixin(Object) implements Block, YamlKey { } public withMarkers(markers: Markers): Scalar { - return markers === this._markers ? this : new Scalar(this._id, this._prefix, markers, this._style, this._anchor, this._value); + return markers === this._markers ? this : new Scalar(this._id, this._prefix, markers, this._style, this._anchor, this._tag, this._value); } private readonly _style: Scalar.Style; @@ -298,7 +299,7 @@ export class Scalar extends YamlMixin(Object) implements Block, YamlKey { } public withStyle(style: Scalar.Style): Scalar { - return style === this._style ? this : new Scalar(this._id, this._prefix, this._markers, style, this._anchor, this._value); + return style === this._style ? this : new Scalar(this._id, this._prefix, this._markers, style, this._anchor, this._tag, this._value); } private readonly _anchor: Anchor | null; @@ -308,7 +309,17 @@ export class Scalar extends YamlMixin(Object) implements Block, YamlKey { } public withAnchor(anchor: Anchor | null): Scalar { - return anchor === this._anchor ? this : new Scalar(this._id, this._prefix, this._markers, this._style, anchor, this._value); + return anchor === this._anchor ? this : new Scalar(this._id, this._prefix, this._markers, this._style, anchor, this._tag, this._value); + } + + private readonly _tag: Tag | null; + + public get tag(): Tag | null { + return this._tag; + } + + public withTag(tag: Tag | null): Scalar { + return tag === this._tag ? this : new Scalar(this._id, this._prefix, this._markers, this._style, this._anchor, tag, this._value); } private readonly _value: string; @@ -318,7 +329,7 @@ export class Scalar extends YamlMixin(Object) implements Block, YamlKey { } public withValue(value: string): Scalar { - return value === this._value ? this : new Scalar(this._id, this._prefix, this._markers, this._style, this._anchor, value); + return value === this._value ? this : new Scalar(this._id, this._prefix, this._markers, this._style, this._anchor, this._tag, value); } public acceptYaml

(v: YamlVisitor

, p: P): Yaml | null { @@ -341,7 +352,7 @@ export namespace Scalar { @LstType("org.openrewrite.yaml.tree.Yaml$Mapping") export class Mapping extends YamlMixin(Object) implements Block { - public constructor(id: UUID, markers: Markers, openingBracePrefix: string | null, entries: Mapping.Entry[], closingBracePrefix: string | null, anchor: Anchor | null) { + public constructor(id: UUID, markers: Markers, openingBracePrefix: string | null, entries: Mapping.Entry[], closingBracePrefix: string | null, anchor: Anchor | null, tag: Tag | null) { super(); this._id = id; this._markers = markers; @@ -349,6 +360,7 @@ export class Mapping extends YamlMixin(Object) implements Block { this._entries = entries; this._closingBracePrefix = closingBracePrefix; this._anchor = anchor; + this._tag = tag; } private readonly _id: UUID; @@ -358,7 +370,7 @@ export class Mapping extends YamlMixin(Object) implements Block { } public withId(id: UUID): Mapping { - return id === this._id ? this : new Mapping(id, this._markers, this._openingBracePrefix, this._entries, this._closingBracePrefix, this._anchor); + return id === this._id ? this : new Mapping(id, this._markers, this._openingBracePrefix, this._entries, this._closingBracePrefix, this._anchor, this._tag); } private readonly _markers: Markers; @@ -368,7 +380,7 @@ export class Mapping extends YamlMixin(Object) implements Block { } public withMarkers(markers: Markers): Mapping { - return markers === this._markers ? this : new Mapping(this._id, markers, this._openingBracePrefix, this._entries, this._closingBracePrefix, this._anchor); + return markers === this._markers ? this : new Mapping(this._id, markers, this._openingBracePrefix, this._entries, this._closingBracePrefix, this._anchor, this._tag); } private readonly _openingBracePrefix: string | null; @@ -378,7 +390,7 @@ export class Mapping extends YamlMixin(Object) implements Block { } public withOpeningBracePrefix(openingBracePrefix: string | null): Mapping { - return openingBracePrefix === this._openingBracePrefix ? this : new Mapping(this._id, this._markers, openingBracePrefix, this._entries, this._closingBracePrefix, this._anchor); + return openingBracePrefix === this._openingBracePrefix ? this : new Mapping(this._id, this._markers, openingBracePrefix, this._entries, this._closingBracePrefix, this._anchor, this._tag); } private readonly _entries: Mapping.Entry[]; @@ -388,7 +400,7 @@ export class Mapping extends YamlMixin(Object) implements Block { } public withEntries(entries: Mapping.Entry[]): Mapping { - return entries === this._entries ? this : new Mapping(this._id, this._markers, this._openingBracePrefix, entries, this._closingBracePrefix, this._anchor); + return entries === this._entries ? this : new Mapping(this._id, this._markers, this._openingBracePrefix, entries, this._closingBracePrefix, this._anchor, this._tag); } private readonly _closingBracePrefix: string | null; @@ -398,7 +410,7 @@ export class Mapping extends YamlMixin(Object) implements Block { } public withClosingBracePrefix(closingBracePrefix: string | null): Mapping { - return closingBracePrefix === this._closingBracePrefix ? this : new Mapping(this._id, this._markers, this._openingBracePrefix, this._entries, closingBracePrefix, this._anchor); + return closingBracePrefix === this._closingBracePrefix ? this : new Mapping(this._id, this._markers, this._openingBracePrefix, this._entries, closingBracePrefix, this._anchor, this._tag); } private readonly _anchor: Anchor | null; @@ -408,7 +420,17 @@ export class Mapping extends YamlMixin(Object) implements Block { } public withAnchor(anchor: Anchor | null): Mapping { - return anchor === this._anchor ? this : new Mapping(this._id, this._markers, this._openingBracePrefix, this._entries, this._closingBracePrefix, anchor); + return anchor === this._anchor ? this : new Mapping(this._id, this._markers, this._openingBracePrefix, this._entries, this._closingBracePrefix, anchor, this._tag); + } + + private readonly _tag: Tag | null; + + public get tag(): Tag | null { + return this._tag; + } + + public withTag(tag: Tag | null): Mapping { + return tag === this._tag ? this : new Mapping(this._id, this._markers, this._openingBracePrefix, this._entries, this._closingBracePrefix, this._anchor, tag); } public acceptYaml

(v: YamlVisitor

, p: P): Yaml | null { @@ -500,7 +522,7 @@ export namespace Mapping { @LstType("org.openrewrite.yaml.tree.Yaml$Sequence") export class Sequence extends YamlMixin(Object) implements Block { - public constructor(id: UUID, markers: Markers, openingBracketPrefix: string | null, entries: Sequence.Entry[], closingBracketPrefix: string | null, anchor: Anchor | null) { + public constructor(id: UUID, markers: Markers, openingBracketPrefix: string | null, entries: Sequence.Entry[], closingBracketPrefix: string | null, anchor: Anchor | null, tag: Tag | null) { super(); this._id = id; this._markers = markers; @@ -508,6 +530,7 @@ export class Sequence extends YamlMixin(Object) implements Block { this._entries = entries; this._closingBracketPrefix = closingBracketPrefix; this._anchor = anchor; + this._tag = tag; } private readonly _id: UUID; @@ -517,7 +540,7 @@ export class Sequence extends YamlMixin(Object) implements Block { } public withId(id: UUID): Sequence { - return id === this._id ? this : new Sequence(id, this._markers, this._openingBracketPrefix, this._entries, this._closingBracketPrefix, this._anchor); + return id === this._id ? this : new Sequence(id, this._markers, this._openingBracketPrefix, this._entries, this._closingBracketPrefix, this._anchor, this._tag); } private readonly _markers: Markers; @@ -527,7 +550,7 @@ export class Sequence extends YamlMixin(Object) implements Block { } public withMarkers(markers: Markers): Sequence { - return markers === this._markers ? this : new Sequence(this._id, markers, this._openingBracketPrefix, this._entries, this._closingBracketPrefix, this._anchor); + return markers === this._markers ? this : new Sequence(this._id, markers, this._openingBracketPrefix, this._entries, this._closingBracketPrefix, this._anchor, this._tag); } private readonly _openingBracketPrefix: string | null; @@ -537,7 +560,7 @@ export class Sequence extends YamlMixin(Object) implements Block { } public withOpeningBracketPrefix(openingBracketPrefix: string | null): Sequence { - return openingBracketPrefix === this._openingBracketPrefix ? this : new Sequence(this._id, this._markers, openingBracketPrefix, this._entries, this._closingBracketPrefix, this._anchor); + return openingBracketPrefix === this._openingBracketPrefix ? this : new Sequence(this._id, this._markers, openingBracketPrefix, this._entries, this._closingBracketPrefix, this._anchor, this._tag); } private readonly _entries: Sequence.Entry[]; @@ -547,7 +570,7 @@ export class Sequence extends YamlMixin(Object) implements Block { } public withEntries(entries: Sequence.Entry[]): Sequence { - return entries === this._entries ? this : new Sequence(this._id, this._markers, this._openingBracketPrefix, entries, this._closingBracketPrefix, this._anchor); + return entries === this._entries ? this : new Sequence(this._id, this._markers, this._openingBracketPrefix, entries, this._closingBracketPrefix, this._anchor, this._tag); } private readonly _closingBracketPrefix: string | null; @@ -557,7 +580,7 @@ export class Sequence extends YamlMixin(Object) implements Block { } public withClosingBracketPrefix(closingBracketPrefix: string | null): Sequence { - return closingBracketPrefix === this._closingBracketPrefix ? this : new Sequence(this._id, this._markers, this._openingBracketPrefix, this._entries, closingBracketPrefix, this._anchor); + return closingBracketPrefix === this._closingBracketPrefix ? this : new Sequence(this._id, this._markers, this._openingBracketPrefix, this._entries, closingBracketPrefix, this._anchor, this._tag); } private readonly _anchor: Anchor | null; @@ -567,7 +590,17 @@ export class Sequence extends YamlMixin(Object) implements Block { } public withAnchor(anchor: Anchor | null): Sequence { - return anchor === this._anchor ? this : new Sequence(this._id, this._markers, this._openingBracketPrefix, this._entries, this._closingBracketPrefix, anchor); + return anchor === this._anchor ? this : new Sequence(this._id, this._markers, this._openingBracketPrefix, this._entries, this._closingBracketPrefix, anchor, this._tag); + } + + private readonly _tag: Tag | null; + + public get tag(): Tag | null { + return this._tag; + } + + public withTag(tag: Tag | null): Sequence { + return tag === this._tag ? this : new Sequence(this._id, this._markers, this._openingBracketPrefix, this._entries, this._closingBracketPrefix, this._anchor, tag); } public acceptYaml

(v: YamlVisitor

, p: P): Yaml | null { @@ -779,3 +812,91 @@ export class Anchor extends YamlMixin(Object) { } } + +@LstType("org.openrewrite.yaml.tree.Yaml$Tag") +export class Tag extends YamlMixin(Object) { + public constructor(id: UUID, prefix: string, markers: Markers, name: string, suffix: string, kind: Tag.Kind) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._name = name; + this._suffix = suffix; + this._kind = kind; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): Tag { + return id === this._id ? this : new Tag(id, this._prefix, this._markers, this._name, this._suffix, this._kind); + } + + private readonly _prefix: string; + + public get prefix(): string { + return this._prefix; + } + + public withPrefix(prefix: string): Tag { + return prefix === this._prefix ? this : new Tag(this._id, prefix, this._markers, this._name, this._suffix, this._kind); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): Tag { + return markers === this._markers ? this : new Tag(this._id, this._prefix, markers, this._name, this._suffix, this._kind); + } + + private readonly _name: string; + + public get name(): string { + return this._name; + } + + public withName(name: string): Tag { + return name === this._name ? this : new Tag(this._id, this._prefix, this._markers, name, this._suffix, this._kind); + } + + private readonly _suffix: string; + + public get suffix(): string { + return this._suffix; + } + + public withSuffix(suffix: string): Tag { + return suffix === this._suffix ? this : new Tag(this._id, this._prefix, this._markers, this._name, suffix, this._kind); + } + + private readonly _kind: Tag.Kind; + + public get kind(): Tag.Kind { + return this._kind; + } + + public withKind(kind: Tag.Kind): Tag { + return kind === this._kind ? this : new Tag(this._id, this._prefix, this._markers, this._name, this._suffix, kind); + } + + public acceptYaml

(v: YamlVisitor

, p: P): Yaml | null { + return v.visitTag(this, p); + } + +} + +export namespace Tag { + export enum Kind { + LOCAL = 0, + IMPLICIT_GLOBAL = 1, + EXPLICIT_GLOBAL = 2, + + } + +} diff --git a/openrewrite/src/yaml/visitor.ts b/openrewrite/src/yaml/visitor.ts index ad2798b3..6923e5ad 100644 --- a/openrewrite/src/yaml/visitor.ts +++ b/openrewrite/src/yaml/visitor.ts @@ -1,5 +1,7 @@ +import * as extensions from "./extensions"; import {ListUtils, SourceFile, Tree, TreeVisitor} from "../core"; -import {Yaml, isYaml, YamlKey, Documents, Document, Block, Scalar, Mapping, Sequence, Alias, Anchor} from "./tree"; +import {Yaml, isYaml, YamlKey} from "./tree"; +import {Documents, Document, Block, Scalar, Mapping, Sequence, Alias, Anchor, Tag} from "./tree"; export class YamlVisitor

extends TreeVisitor { isAcceptable(sourceFile: SourceFile, p: P): boolean { @@ -27,6 +29,7 @@ export class YamlVisitor

extends TreeVisitor { public visitScalar(scalar: Scalar, p: P): Yaml | null { scalar = scalar.withMarkers(this.visitMarkers(scalar.markers, p)); scalar = scalar.withAnchor(this.visitAndCast(scalar.anchor, p)); + scalar = scalar.withTag(this.visitAndCast(scalar.tag, p)); return scalar; } @@ -34,6 +37,7 @@ export class YamlVisitor

extends TreeVisitor { mapping = mapping.withMarkers(this.visitMarkers(mapping.markers, p)); mapping = mapping.withEntries(ListUtils.map(mapping.entries, el => this.visitAndCast(el, p))); mapping = mapping.withAnchor(this.visitAndCast(mapping.anchor, p)); + mapping = mapping.withTag(this.visitAndCast(mapping.tag, p)); return mapping; } @@ -48,6 +52,7 @@ export class YamlVisitor

extends TreeVisitor { sequence = sequence.withMarkers(this.visitMarkers(sequence.markers, p)); sequence = sequence.withEntries(ListUtils.map(sequence.entries, el => this.visitAndCast(el, p))); sequence = sequence.withAnchor(this.visitAndCast(sequence.anchor, p)); + sequence = sequence.withTag(this.visitAndCast(sequence.tag, p)); return sequence; } @@ -68,4 +73,9 @@ export class YamlVisitor

extends TreeVisitor { return anchor; } + public visitTag(tag: Tag, p: P): Yaml | null { + tag = tag.withMarkers(this.visitMarkers(tag.markers, p)); + return tag; + } + } diff --git a/rewrite-javascript-remote-server/src/test/java/org/openrewrite/javascript/remote/RemotingRecipeRunTest.java b/rewrite-javascript-remote-server/src/test/java/org/openrewrite/javascript/remote/RemotingRecipeRunTest.java index 5b406864..573ac4a6 100644 --- a/rewrite-javascript-remote-server/src/test/java/org/openrewrite/javascript/remote/RemotingRecipeRunTest.java +++ b/rewrite-javascript-remote-server/src/test/java/org/openrewrite/javascript/remote/RemotingRecipeRunTest.java @@ -82,6 +82,7 @@ public void testRemotingRecipeInstallAndRun() throws IOException { RemotingRecipe remotingRecipe = new RemotingRecipe(new RecipeDescriptor( recipes.getRecipes().get(0).getDescriptor().getName(), recipes.getRecipes().get(0).getDescriptor().getDisplayName(), + recipes.getRecipes().get(0).getDescriptor().getInstanceName(), recipes.getRecipes().get(0).getDescriptor().getDescription(), recipes.getRecipes().get(0).getDescriptor().getTags(), recipes.getRecipes().get(0).getDescriptor().getEstimatedEffortPerOccurrence(),