From 81e9b9c49aa27c3b90d86c4a6527754daf562cc7 Mon Sep 17 00:00:00 2001 From: Kirk Waiblinger <53019676+kirkwaiblinger@users.noreply.github.com> Date: Sun, 4 May 2025 22:52:24 -0600 Subject: [PATCH] docs: use `T[]` instead of `[T, ...T[]]` in rule options --- .../tests/schema-snapshots/no-deprecated.shot | 6 +-- .../no-floating-promises.shot | 12 ++--- .../no-invalid-void-type.shot | 2 +- .../schema-snapshots/no-misused-spread.shot | 6 +-- .../no-restricted-imports.shot | 6 +-- .../schema-snapshots/only-throw-error.shot | 6 +-- .../prefer-readonly-parameter-types.shot | 6 +-- .../restrict-template-expressions.shot | 6 +-- .../src/generateArrayType.ts | 48 +++++++------------ 9 files changed, 42 insertions(+), 56 deletions(-) diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-deprecated.shot b/packages/eslint-plugin/tests/schema-snapshots/no-deprecated.shot index 2f5271ef22d5..b6f42e630c7a 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/no-deprecated.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/no-deprecated.shot @@ -114,16 +114,16 @@ type Options = [ allow?: ( | { from: 'file'; - name: [string, ...string[]] | string; + name: string | string[]; path?: string; } | { from: 'lib'; - name: [string, ...string[]] | string; + name: string | string[]; } | { from: 'package'; - name: [string, ...string[]] | string; + name: string | string[]; package: string; } | string diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-floating-promises.shot b/packages/eslint-plugin/tests/schema-snapshots/no-floating-promises.shot index 82d375d35447..73c152a36d2b 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/no-floating-promises.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/no-floating-promises.shot @@ -221,16 +221,16 @@ type Options = [ allowForKnownSafeCalls?: ( | { from: 'file'; - name: [string, ...string[]] | string; + name: string | string[]; path?: string; } | { from: 'lib'; - name: [string, ...string[]] | string; + name: string | string[]; } | { from: 'package'; - name: [string, ...string[]] | string; + name: string | string[]; package: string; } | string @@ -239,16 +239,16 @@ type Options = [ allowForKnownSafePromises?: ( | { from: 'file'; - name: [string, ...string[]] | string; + name: string | string[]; path?: string; } | { from: 'lib'; - name: [string, ...string[]] | string; + name: string | string[]; } | { from: 'package'; - name: [string, ...string[]] | string; + name: string | string[]; package: string; } | string diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-invalid-void-type.shot b/packages/eslint-plugin/tests/schema-snapshots/no-invalid-void-type.shot index 863cd578dd46..77ff5231ddee 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/no-invalid-void-type.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/no-invalid-void-type.shot @@ -45,6 +45,6 @@ type Options = [ */ | boolean /** Allowlist of types that may accept `void` as a generic type parameter. */ - | [string, ...string[]]; + | string[]; }, ]; diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-misused-spread.shot b/packages/eslint-plugin/tests/schema-snapshots/no-misused-spread.shot index 7d2f67e4b82e..bca0b9d5f42d 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/no-misused-spread.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/no-misused-spread.shot @@ -114,16 +114,16 @@ type Options = [ allow?: ( | { from: 'file'; - name: [string, ...string[]] | string; + name: string | string[]; path?: string; } | { from: 'lib'; - name: [string, ...string[]] | string; + name: string | string[]; } | { from: 'package'; - name: [string, ...string[]] | string; + name: string | string[]; package: string; } | string diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-restricted-imports.shot b/packages/eslint-plugin/tests/schema-snapshots/no-restricted-imports.shot index 6f756c298834..47c5082478ed 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/no-restricted-imports.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/no-restricted-imports.shot @@ -197,13 +197,13 @@ type Options = patterns?: | { allowImportNamePattern?: string; - allowImportNames?: [string, ...string[]]; + allowImportNames?: string[]; /** Whether to allow type-only imports for a path. */ allowTypeImports?: boolean; caseSensitive?: boolean; - group?: [string, ...string[]]; + group?: string[]; importNamePattern?: string; - importNames?: [string, ...string[]]; + importNames?: string[]; message?: string; regex?: string; }[] diff --git a/packages/eslint-plugin/tests/schema-snapshots/only-throw-error.shot b/packages/eslint-plugin/tests/schema-snapshots/only-throw-error.shot index 8727d31475d7..c82b860b76f2 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/only-throw-error.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/only-throw-error.shot @@ -122,16 +122,16 @@ type Options = [ allow?: ( | { from: 'file'; - name: [string, ...string[]] | string; + name: string | string[]; path?: string; } | { from: 'lib'; - name: [string, ...string[]] | string; + name: string | string[]; } | { from: 'package'; - name: [string, ...string[]] | string; + name: string | string[]; package: string; } | string diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-readonly-parameter-types.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-readonly-parameter-types.shot index 26b2e1e406cd..ced28e626959 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/prefer-readonly-parameter-types.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-readonly-parameter-types.shot @@ -126,16 +126,16 @@ type Options = [ allow?: ( | { from: 'file'; - name: [string, ...string[]] | string; + name: string | string[]; path?: string; } | { from: 'lib'; - name: [string, ...string[]] | string; + name: string | string[]; } | { from: 'package'; - name: [string, ...string[]] | string; + name: string | string[]; package: string; } | string diff --git a/packages/eslint-plugin/tests/schema-snapshots/restrict-template-expressions.shot b/packages/eslint-plugin/tests/schema-snapshots/restrict-template-expressions.shot index 770b5cc78a16..48bb9b1c61e9 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/restrict-template-expressions.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/restrict-template-expressions.shot @@ -142,16 +142,16 @@ type Options = [ allow?: ( | { from: 'file'; - name: [string, ...string[]] | string; + name: string | string[]; path?: string; } | { from: 'lib'; - name: [string, ...string[]] | string; + name: string | string[]; } | { from: 'package'; - name: [string, ...string[]] | string; + name: string | string[]; package: string; } | string diff --git a/packages/rule-schema-to-typescript-types/src/generateArrayType.ts b/packages/rule-schema-to-typescript-types/src/generateArrayType.ts index 1952347948f4..54c1499f8a6c 100644 --- a/packages/rule-schema-to-typescript-types/src/generateArrayType.ts +++ b/packages/rule-schema-to-typescript-types/src/generateArrayType.ts @@ -40,41 +40,27 @@ export function generateArrayType( schema.maxItems != null && schema.maxItems < MAX_ITEMS_TO_TUPLIZE ? schema.maxItems : -1; - const hasMinItems = minItems > 0; const hasMaxItems = maxItems >= 0; - let items: JSONSchema4[]; + if (!TSUtils.isArray(schema.items)) { + // While we could support `minItems` and `maxItems` with tuple types, + // for example `[T, ...T[]]`, it harms readability for documentation purposes. + // See https://github.com/typescript-eslint/typescript-eslint/issues/11117 + return { + commentLines, + elementType: generateType(schema.items, refMap), + type: 'array', + }; + } + // treat as a tuple + const items: JSONSchema4[] = schema.items; let spreadItemSchema: JSONSchema4 | null = null; - if (!TSUtils.isArray(schema.items)) { - if (hasMinItems || hasMaxItems) { - // treat as a tuple - items = Array( - (hasMaxItems && maxItems) || minItems || 0, - ).fill(schema.items); - if (!hasMaxItems) { - spreadItemSchema = - typeof schema.additionalItems === 'object' - ? schema.additionalItems - : schema.items; - } - } else { - // treat as an array type - return { - commentLines, - elementType: generateType(schema.items, refMap), - type: 'array', - }; - } - } else { - // treat as a tuple - items = schema.items; - if (hasMaxItems && items.length < maxItems) { - spreadItemSchema = - typeof schema.additionalItems === 'object' - ? schema.additionalItems - : { type: 'any' }; - } + if (hasMaxItems && items.length < maxItems) { + spreadItemSchema = + typeof schema.additionalItems === 'object' + ? schema.additionalItems + : { type: 'any' }; } // quick validation so we generate sensible types