Skip to content
Merged
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
5 changes: 2 additions & 3 deletions compat-test/migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { readFile } from "node:fs/promises";
import { describe, test, expect } from "vitest";

describe("Migration", () => {
// @todo update
test.skip("should fix the import", async () => {
test("should fix the import", async () => {
const fixed = await readFile("./sample.ts", "utf-8");
expect(fixed).toBe("const test: HeaderSecurity = {};\n");
expect(fixed).toBe("new Documentation({ });\n");
});
});
2 changes: 1 addition & 1 deletion compat-test/sample.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const test: HeaderSecurity = {};
new Documentation({ numericRange: {}, });
56 changes: 47 additions & 9 deletions express-zod-api/src/migration.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import {
ESLintUtils,
// AST_NODE_TYPES as NT,
AST_NODE_TYPES as NT,
type TSESLint,
// type TSESTree,
type TSESTree,
} from "@typescript-eslint/utils"; // eslint-disable-line allowed/dependencies -- special case

/*
interface Queries {}
type NamedProp = TSESTree.PropertyNonComputedName & {
key: TSESTree.Identifier;
};

interface Queries {
numericRange: NamedProp;
optionalPropStyle: NamedProp;
}

type Listener = keyof Queries;

const queries: Record<Listener, string> = {};
const queries: Record<Listener, string> = {
numericRange:
`${NT.NewExpression}[callee.name='Documentation'] > ` +
`${NT.ObjectExpression} > ${NT.Property}[key.name='numericRange']`,
optionalPropStyle:
`${NT.NewExpression}[callee.name='Integration'] > ` +
`${NT.ObjectExpression} > ${NT.Property}[key.name='optionalPropStyle']`,
};

const listen = <
S extends { [K in Listener]: TSESLint.RuleFunction<Queries[K]> },
Expand All @@ -24,7 +37,15 @@ const listen = <
}),
{},
);
*/

const rangeWithComma = (
node: TSESTree.Node,
ctx: TSESLint.RuleContext<string, unknown[]>,
) =>
[
node.range[0],
node.range[1] + (ctx.sourceCode.getTokenAfter(node)?.value === "," ? 1 : 0),
] as const;

const v24 = ESLintUtils.RuleCreator.withoutDocs({
meta: {
Expand All @@ -33,12 +54,29 @@ const v24 = ESLintUtils.RuleCreator.withoutDocs({
schema: [],
messages: {
change: "change {{ subject }} from {{ from }} to {{ to }}",
add: `add {{ subject }} to {{ to }}`,
add: "add {{ subject }} to {{ to }}",
move: "move {{ subject }} to {{ to }}",
remove: "remove {{ subject }}",
},
},
defaultOptions: [],
create: () => ({}),
create: (ctx) =>
listen({
numericRange: (node) =>
ctx.report({
node,
messageId: "remove",
data: { subject: node.key.name },
fix: (fixer) => fixer.removeRange(rangeWithComma(node, ctx)),
}),
optionalPropStyle: (node) =>
ctx.report({
node,
messageId: "remove",
data: { subject: node.key.name },
fix: (fixer) => fixer.removeRange(rangeWithComma(node, ctx)),
}),
}),
});

/**
Expand All @@ -50,7 +88,7 @@ const v24 = ESLintUtils.RuleCreator.withoutDocs({
* import migration from "express-zod-api/migration";
* export default [
* { languageOptions: {parser}, plugins: {migration} },
* { files: ["**\/*.ts"], rules: { "migration/v21": "error" } }
* { files: ["**\/*.ts"], rules: { "migration/v24": "error" } }
* ];
* */
export default {
Expand Down
1 change: 1 addition & 0 deletions express-zod-api/tests/__snapshots__/migration.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports[`Migration > should consist of one rule being the major version of the p
"add": "add {{ subject }} to {{ to }}",
"change": "change {{ subject }} from {{ from }} to {{ to }}",
"move": "move {{ subject }} to {{ to }}",
"remove": "remove {{ subject }}",
},
"schema": [],
"type": "problem",
Expand Down
36 changes: 26 additions & 10 deletions express-zod-api/tests/migration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
import { RuleTester } from "@typescript-eslint/rule-tester";
import migration from "../src/migration";
// import parser from "@typescript-eslint/parser";
// import { version } from "../package.json";
import parser from "@typescript-eslint/parser";
import { version } from "../package.json";

RuleTester.afterAll = afterAll;
RuleTester.describe = describe;
RuleTester.it = it;

/*
const tester = new RuleTester({
languageOptions: { parser },
});
*/

describe("Migration", () => {
test("should consist of one rule being the major version of the package", () => {
// @todo restore
// expect(migration.rules).toHaveProperty(`v${version.split(".")[0]}`);
expect(migration.rules).toHaveProperty(`v${version.split(".")[0]}`);
expect(migration).toMatchSnapshot();
});

/*
tester.run("v24", migration.rules.v24, {
valid: [],
invalid: [],
valid: [`new Documentation({});`, `new Integration({})`],
invalid: [
{
code: `new Documentation({ numericRange: {}, });`,
output: `new Documentation({ });`,
errors: [
{
messageId: "remove",
data: { subject: "numericRange" },
},
],
},
{
code: `new Integration({ optionalPropStyle: {}, });`,
output: `new Integration({ });`,
errors: [
{
messageId: "remove",
data: { subject: "optionalPropStyle" },
},
],
},
],
});
*/
});