Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions rewrite-javascript/rewrite/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rewrite-javascript/rewrite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"diff": "^7.0.0",
"immer": "^10.1.1",
"picomatch": "^4.0.3",
"reflect-metadata": "^0.2.2",
"tmp-promise": "^3.0.3",
"typescript": "^5.8.3",
"vscode-jsonrpc": "^8.2.1"
Expand Down
50 changes: 41 additions & 9 deletions rewrite-javascript/rewrite/src/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {Cursor, SourceFile, Tree} from "./tree";
import {ExecutionContext} from "./execution";
import {DataTableDescriptor} from "./data-table";
import {mapAsync} from "./util";
import "reflect-metadata";

const OPTIONS_KEY = "__recipe_options__";

Expand Down Expand Up @@ -88,7 +89,7 @@ export abstract class Recipe {
}

async descriptor(): Promise<RecipeDescriptor> {
const optionsRecord: Record<string, OptionDescriptor> = (this as any).constructor[OPTIONS_KEY] || {}
const optionsRecord: Record<string, OptionAnnotationDescriptor> = (this as any).constructor[OPTIONS_KEY] || {}
return {
name: this.name,
displayName: this.displayName,
Expand All @@ -97,12 +98,39 @@ export abstract class Recipe {
tags: this.tags,
estimatedEffortPerOccurrence: this.estimatedEffortPerOccurrence,
recipeList: await mapAsync(await this.recipeList(), async r => r.descriptor()),
options: Object.entries(optionsRecord).map(([key, descriptor]) => ({
name: key,
value: (this as any)[key],
required: descriptor.required ?? true,
...descriptor
}))
options: Object.entries(optionsRecord).map(([key, descriptor]) => {
var t = Reflect.getMetadata("design:type", this, key);
var simplifiedType = t?.name ?? "string";
switch(t.name) {
case "String": {
simplifiedType = "string";
break;
}
case "Boolean": {
simplifiedType = "boolean";
break;
}
case "Number": {
simplifiedType = "number";
break;
}
case "Symbol": {
simplifiedType = "symbol";
break;
}
case "Object": {
simplifiedType = "object";
break;
}
}
return {
name: key,
value: (this as any)[key],
required: descriptor.required ?? true,
type: simplifiedType,
...descriptor
};
})
}
}

Expand Down Expand Up @@ -138,14 +166,18 @@ export interface RecipeDescriptor {
readonly options: ({ name: string, value?: any } & OptionDescriptor)[]
}

export interface OptionDescriptor {
export interface OptionAnnotationDescriptor {
readonly displayName: string
readonly description: string
readonly required?: boolean
readonly example?: string
readonly valid?: string[]
}

export interface OptionDescriptor extends OptionAnnotationDescriptor {
readonly type: string;
}

export abstract class ScanningRecipe<P> extends Recipe {
private readonly recipeAccMessage = Symbol("org.openrewrite.recipe.acc");

Expand Down Expand Up @@ -217,7 +249,7 @@ export class RecipeRegistry {
}
}

export function Option(descriptor: OptionDescriptor) {
export function Option(descriptor: OptionAnnotationDescriptor) {
return function (target: any, propertyKey: string) {
// Ensure the constructor has options storage.
if (!target.constructor.hasOwnProperty(OPTIONS_KEY)) {
Expand Down
3 changes: 2 additions & 1 deletion rewrite-javascript/rewrite/test/recipe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ describe("recipes", () => {
displayName: "Text",
name: "text",
required: true,
value: undefined
value: undefined,
type: "string"
}
],
recipeList: [],
Expand Down
3 changes: 2 additions & 1 deletion rewrite-javascript/rewrite/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"skipLibCheck": true,
"rootDir": ".",
"outDir": "./dist",
"experimentalDecorators": true
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"include": [
"src/**/*",
Expand Down