eslint: Prefer type.array() over z.array(type) (#1810)

## Description:

Prefer `type.array()` over `z.array(type)`.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
This commit is contained in:
Scott Anderson
2025-08-14 01:43:25 -04:00
committed by GitHub
parent 385ef5ca93
commit 2ed0cef65c
4 changed files with 55 additions and 2 deletions
+7
View File
@@ -0,0 +1,7 @@
import noZArray from "./rules/no-z-array.js";
export default {
rules: {
"no-z-array": noZArray,
},
};
+35
View File
@@ -0,0 +1,35 @@
export default {
create(context) {
return {
CallExpression(node) {
if (
node.callee.type === "MemberExpression" &&
node.callee.object.name === "z" &&
node.callee.property.name === "array" &&
node.arguments.length === 1
) {
const argSource = context.sourceCode.getText(node.arguments[0]);
context.report({
data: { type: argSource },
fix(fixer) {
return fixer.replaceText(node, `${argSource}.array()`);
},
messageId: "noZArray",
node,
});
}
},
};
},
meta: {
docs: {
description: "Disallow z.array(type) in favor of type.array()",
},
fixable: "code",
messages: {
noZArray: "Use `{{type}}.array()` instead of `z.array({{type}})`.",
},
schema: [],
type: "suggestion",
},
};
+11
View File
@@ -6,6 +6,7 @@ import stylisticTs from "@stylistic/eslint-plugin";
import tseslint from "typescript-eslint";
import { fileURLToPath } from "node:url";
import { includeIgnoreFile } from "@eslint/compat";
import eslintPluginLocal from "./eslint-plugin-local/plugin.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -26,6 +27,8 @@ export default [
projectService: {
allowDefaultProject: [
"__mocks__/fileMock.js",
"eslint-plugin-local/plugin.js",
"eslint-plugin-local/rules/no-z-array.js",
"eslint.config.js",
"jest.config.ts",
"postcss.config.js",
@@ -130,4 +133,12 @@ export default [
"sort-keys": "off",
},
},
{
plugins: {
local: eslintPluginLocal,
},
rules: {
"local/no-z-array": "error",
},
},
];
+2 -2
View File
@@ -25,7 +25,7 @@ export const CosmeticsSchema = z.object({
z.string(),
z.object({
name: z.string(),
flares: z.array(z.string()).optional(),
flares: z.string().array().optional(),
}),
),
color: z.record(
@@ -33,7 +33,7 @@ export const CosmeticsSchema = z.object({
z.object({
color: z.string(),
name: z.string(),
flares: z.array(z.string()).optional(),
flares: z.string().array().optional(),
}),
),
})