From dbf8b569dbada29e4a295d6c265976e55de1b2aa Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 24 Aug 2022 16:02:21 -0400 Subject: [PATCH] fix(eslint-plugin): correct rule schemas to pass ajv validation (#5531) * fix(eslint-plugin): correct rule schemas to pass ajv validation * Fix: copy over meta.schema.prefixItems * A little more precise * Correct precision * Turns out it was I who had to docusaurus clear all along * fix explicit-member-accessibility --- .../eslint-plugin/src/rules/array-type.ts | 39 ++++++----- .../eslint-plugin/src/rules/ban-ts-comment.ts | 68 ++++++++++--------- .../rules/explicit-member-accessibility.ts | 57 +++++++++------- .../src/rules/parameter-properties.ts | 57 ++++++++-------- .../website/plugins/generated-rule-docs.ts | 19 +++++- 5 files changed, 133 insertions(+), 107 deletions(-) diff --git a/packages/eslint-plugin/src/rules/array-type.ts b/packages/eslint-plugin/src/rules/array-type.ts index 09339727abd..8af13f38ed4 100644 --- a/packages/eslint-plugin/src/rules/array-type.ts +++ b/packages/eslint-plugin/src/rules/array-type.ts @@ -102,27 +102,30 @@ export default util.createRule({ errorStringGenericSimple: "Array type using '{{readonlyPrefix}}{{type}}[]' is forbidden for non-simple types. Use '{{className}}<{{type}}>' instead.", }, - schema: [ - { - $defs: { - arrayOption: { - enum: ['array', 'generic', 'array-simple'], - }, + schema: { + $defs: { + arrayOption: { + enum: ['array', 'generic', 'array-simple'], }, - properties: { - default: { - $ref: '#/$defs/arrayOption', - description: 'The array type expected for mutable cases...', - }, - readonly: { - $ref: '#/$defs/arrayOption', - description: - 'The array type expected for readonly cases. If omitted, the value for `default` will be used.', + }, + prefixItems: [ + { + properties: { + default: { + $ref: '#/$defs/arrayOption', + description: 'The array type expected for mutable cases...', + }, + readonly: { + $ref: '#/$defs/arrayOption', + description: + 'The array type expected for readonly cases. If omitted, the value for `default` will be used.', + }, }, + type: 'object', }, - type: 'object', - }, - ], + ], + type: 'array', + }, }, defaultOptions: [ { diff --git a/packages/eslint-plugin/src/rules/ban-ts-comment.ts b/packages/eslint-plugin/src/rules/ban-ts-comment.ts index 225285fdc16..f50a9e8e269 100644 --- a/packages/eslint-plugin/src/rules/ban-ts-comment.ts +++ b/packages/eslint-plugin/src/rules/ban-ts-comment.ts @@ -38,43 +38,45 @@ export default util.createRule<[Options], MessageIds>({ tsDirectiveCommentDescriptionNotMatchPattern: 'The description for the "@ts-{{directive}}" directive must match the {{format}} format.', }, - schema: [ - { - $defs: { - directiveConfigSchema: { - oneOf: [ - { - type: 'boolean', - default: true, + schema: { + $defs: { + directiveConfigSchema: { + oneOf: [ + { + type: 'boolean', + default: true, + }, + { + enum: ['allow-with-description'], + }, + { + type: 'object', + properties: { + descriptionFormat: { type: 'string' }, }, - { - enum: ['allow-with-description'], - }, - { - type: 'object', - properties: { - descriptionFormat: { type: 'string' }, - }, - }, - ], - }, + }, + ], }, - type: 'object', - properties: { - 'ts-expect-error': { - $ref: '#/$defs/directiveConfigSchema', - }, - 'ts-ignore': { $ref: '#/$defs/directiveConfigSchema' }, - 'ts-nocheck': { $ref: '#/$defs/directiveConfigSchema' }, - 'ts-check': { $ref: '#/$defs/directiveConfigSchema' }, - minimumDescriptionLength: { - type: 'number', - default: defaultMinimumDescriptionLength, + }, + prefixItems: [ + { + properties: { + 'ts-expect-error': { + $ref: '#/$defs/directiveConfigSchema', + }, + 'ts-ignore': { $ref: '#/$defs/directiveConfigSchema' }, + 'ts-nocheck': { $ref: '#/$defs/directiveConfigSchema' }, + 'ts-check': { $ref: '#/$defs/directiveConfigSchema' }, + minimumDescriptionLength: { + type: 'number', + default: defaultMinimumDescriptionLength, + }, }, + additionalProperties: false, }, - additionalProperties: false, - }, - ], + ], + type: 'array', + }, }, defaultOptions: [ { diff --git a/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts b/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts index 7ac808329df..6087568abe9 100644 --- a/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts +++ b/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts @@ -66,36 +66,41 @@ export default util.createRule({ 'Public accessibility modifier on {{type}} {{name}}.', addExplicitAccessibility: "Add '{{ type }}' accessibility modifier", }, - schema: [ - { - $defs: { - accessibilityLevel, - }, - type: 'object', - properties: { - accessibility: { $ref: '#/$defs/accessibilityLevel' }, - overrides: { - type: 'object', - properties: { - accessors: { $ref: '#/$defs/accessibilityLevel' }, - constructors: { $ref: '#/$defs/accessibilityLevel' }, - methods: { $ref: '#/$defs/accessibilityLevel' }, - properties: { $ref: '#/$defs/accessibilityLevel' }, - parameterProperties: { $ref: '#/$defs/accessibilityLevel' }, - }, + schema: { + $defs: { + accessibilityLevel, + }, + prefixItems: [ + { + type: 'object', + properties: { + accessibility: { $ref: '#/$defs/accessibilityLevel' }, + overrides: { + type: 'object', + properties: { + accessors: { $ref: '#/$defs/accessibilityLevel' }, + constructors: { $ref: '#/$defs/accessibilityLevel' }, + methods: { $ref: '#/$defs/accessibilityLevel' }, + properties: { $ref: '#/$defs/accessibilityLevel' }, + parameterProperties: { + $ref: '#/$defs/accessibilityLevel', + }, + }, - additionalProperties: false, - }, - ignoredMethodNames: { - type: 'array', - items: { - type: 'string', + additionalProperties: false, + }, + ignoredMethodNames: { + type: 'array', + items: { + type: 'string', + }, }, }, + additionalProperties: false, }, - additionalProperties: false, - }, - ], + ], + type: 'array', + }, }, defaultOptions: [{ accessibility: 'explicit' }], create(context, [option]) { diff --git a/packages/eslint-plugin/src/rules/parameter-properties.ts b/packages/eslint-plugin/src/rules/parameter-properties.ts index e2119b38ac8..78bae4d30ea 100644 --- a/packages/eslint-plugin/src/rules/parameter-properties.ts +++ b/packages/eslint-plugin/src/rules/parameter-properties.ts @@ -36,37 +36,40 @@ export default util.createRule({ preferParameterProperty: 'Property {{parameter}} should be declared as a parameter property.', }, - schema: [ - { - $defs: { - modifier: { - enum: [ - 'readonly', - 'private', - 'protected', - 'public', - 'private readonly', - 'protected readonly', - 'public readonly', - ], - }, + schema: { + $defs: { + modifier: { + enum: [ + 'readonly', + 'private', + 'protected', + 'public', + 'private readonly', + 'protected readonly', + 'public readonly', + ], }, - type: 'object', - properties: { - allow: { - type: 'array', - items: { - $ref: '#/$defs/modifier', + }, + prefixItems: [ + { + type: 'object', + properties: { + allow: { + type: 'array', + items: { + $ref: '#/$defs/modifier', + }, + minItems: 1, + }, + prefer: { + enum: ['class-property', 'parameter-property'], }, - minItems: 1, - }, - prefer: { - enum: ['class-property', 'parameter-property'], }, + additionalProperties: false, }, - additionalProperties: false, - }, - ], + ], + type: 'array', + }, }, defaultOptions: [ { diff --git a/packages/website/plugins/generated-rule-docs.ts b/packages/website/plugins/generated-rule-docs.ts index 9590d67e2dd..33014db3e3e 100644 --- a/packages/website/plugins/generated-rule-docs.ts +++ b/packages/website/plugins/generated-rule-docs.ts @@ -4,11 +4,12 @@ import * as mdast from 'mdast'; import * as path from 'path'; import { format } from 'prettier'; import type { Plugin } from 'unified'; -import { compile } from 'json-schema-to-typescript'; +import { compile, JSONSchema } from 'json-schema-to-typescript'; import * as tseslintParser from '@typescript-eslint/parser'; import * as eslintPlugin from '@typescript-eslint/eslint-plugin'; import { EOL } from 'os'; +import { JSONSchema7 } from 'json-schema'; /** * Rules whose options schema generate annoyingly complex schemas. @@ -213,8 +214,20 @@ export const generatedRuleDocs: Plugin = () => { type: 'paragraph', } as mdast.Paragraph); } else if (!COMPLICATED_RULE_OPTIONS.has(file.stem)) { - const optionsSchema = - meta.schema instanceof Array ? meta.schema[0] : meta.schema; + const optionsSchema: JSONSchema = + meta.schema instanceof Array + ? meta.schema[0] + : meta.schema.type === 'array' + ? { + ...(meta.schema.definitions + ? { definitions: meta.schema.definitions } + : {}), + ...(meta.schema.$defs + ? { $defs: (meta.schema as JSONSchema7).$defs } + : {}), + ...(meta.schema.prefixItems as [JSONSchema])[0], + } + : meta.schema; parent.children.splice( optionsH2Index + 2,