Skip to content

Commit 34f49dc

Browse files
committed
Migration script for getExamples().
1 parent 82fa0d1 commit 34f49dc

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

migration/index.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe("Migration", () => {
2222
`import {} from "zod";`,
2323
`ez.dateIn({ examples: ["1963-04-21"] });`,
2424
`ez.dateOut({ examples: ["2021-12-31T00:00:00.000Z"] });`,
25+
`schema.meta()?.examples;`,
2526
],
2627
invalid: [
2728
{
@@ -54,6 +55,20 @@ describe("Migration", () => {
5455
},
5556
],
5657
},
58+
{
59+
code: `getExamples(schema);`,
60+
output: `(schema.meta()?.examples || []);`,
61+
errors: [
62+
{
63+
messageId: "change",
64+
data: {
65+
subject: "method",
66+
from: "getExamples()",
67+
to: ".meta()?.examples || []",
68+
},
69+
},
70+
],
71+
},
5772
],
5873
});
5974
});

migration/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type NamedProp = TSESTree.PropertyNonComputedName & {
1212
interface Queries {
1313
zod: TSESTree.ImportDeclaration;
1414
dateInOutExample: NamedProp;
15+
getExamples: TSESTree.CallExpression;
1516
}
1617

1718
type Listener = keyof Queries;
@@ -21,6 +22,7 @@ const queries: Record<Listener, string> = {
2122
dateInOutExample:
2223
`${NT.CallExpression}[callee.object.name='ez'][callee.property.name=/date(In|Out)/] >` +
2324
`${NT.ObjectExpression} > ${NT.Property}[key.name='example']`,
25+
getExamples: `${NT.CallExpression}[callee.name='getExamples']`,
2426
};
2527

2628
const listen = <
@@ -89,6 +91,21 @@ const v25 = ESLintUtils.RuleCreator.withoutDocs({
8991
`examples: [${ctx.sourceCode.getText(node.value)}]`,
9092
),
9193
}),
94+
getExamples: (node) =>
95+
ctx.report({
96+
node,
97+
messageId: "change",
98+
data: {
99+
subject: "method",
100+
from: "getExamples()",
101+
to: ".meta()?.examples || []",
102+
},
103+
fix: (fixer) =>
104+
fixer.replaceText(
105+
node,
106+
`(${ctx.sourceCode.getText(node.arguments[0])}.meta()?.examples || [])`,
107+
),
108+
}),
92109
}),
93110
});
94111

0 commit comments

Comments
 (0)