diff --git a/.github/ISSUE_TEMPLATE/06-bug-report-other.yaml b/.github/ISSUE_TEMPLATE/06-bug-report-other.yaml
index 69fc7d6ef7d7..d1d8b6fc4225 100644
--- a/.github/ISSUE_TEMPLATE/06-bug-report-other.yaml
+++ b/.github/ISSUE_TEMPLATE/06-bug-report-other.yaml
@@ -41,6 +41,7 @@ body:
- eslint-plugin
- parser
- project-service
+ - rule-schema-to-typescript-types
- rule-tester
- scope-manager
- tsconfig-utils
diff --git a/.github/ISSUE_TEMPLATE/07-enhancement-other.yaml b/.github/ISSUE_TEMPLATE/07-enhancement-other.yaml
index 7250ea66e0f3..de9f9324e4c0 100644
--- a/.github/ISSUE_TEMPLATE/07-enhancement-other.yaml
+++ b/.github/ISSUE_TEMPLATE/07-enhancement-other.yaml
@@ -27,6 +27,7 @@ body:
- eslint-plugin
- parser
- project-service
+ - rule-schema-to-typescript-types
- rule-tester
- scope-manager
- tsconfig-utils
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 91e78e2a606a..0d0e3f81e81e 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -195,6 +195,7 @@ jobs:
'eslint-plugin-internal',
'parser',
'project-service',
+ 'rule-schema-to-typescript-types',
'rule-tester',
'scope-manager',
'tsconfig-utils',
diff --git a/.github/workflows/pr-title-validation.yml b/.github/workflows/pr-title-validation.yml
index 175ce9457a5e..a2a5e5e47a8a 100644
--- a/.github/workflows/pr-title-validation.yml
+++ b/.github/workflows/pr-title-validation.yml
@@ -39,6 +39,7 @@ jobs:
eslint-plugin-internal
parser
project-service
+ rule-schema-to-typescript-types
rule-tester
scope-manager
tsconfig-utils
diff --git a/docs/packages/RuleSchemaToTypeScriptTypes.mdx b/docs/packages/RuleSchemaToTypeScriptTypes.mdx
new file mode 100644
index 000000000000..c684c357c5f6
--- /dev/null
+++ b/docs/packages/RuleSchemaToTypeScriptTypes.mdx
@@ -0,0 +1,36 @@
+---
+id: rule-schema-to-typescript-types
+sidebar_label: rule-schema-to-typescript-types
+toc_max_heading_level: 3
+---
+
+import GeneratedDocs from './rule-schema-to-typescript-types/generated/index.md';
+
+# `@typescript-eslint/rule-schema-to-typescript-types`
+
+
+
+> Converts ESLint rule schemas to equivalent TypeScript type strings ✨
+
+```ts
+import { schemaToTypes } from '@typescript-eslint/rule-schema-to-typescript-types';
+
+// "
+// type Options = [
+// /** My great option! */
+// string[]
+// ];
+// "
+schemaToTypes({
+ description: 'My great option!',
+ items: { type: 'string' },
+ type: 'array',
+});
+```
+
+The following documentation is auto-generated from source code.
+
+
diff --git a/packages/eslint-plugin/tests/schema-snapshots/adjacent-overload-signatures.shot b/packages/eslint-plugin/tests/schema-snapshots/adjacent-overload-signatures.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/adjacent-overload-signatures.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/adjacent-overload-signatures.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/await-thenable.shot b/packages/eslint-plugin/tests/schema-snapshots/await-thenable.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/await-thenable.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/await-thenable.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/ban-tslint-comment.shot b/packages/eslint-plugin/tests/schema-snapshots/ban-tslint-comment.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/ban-tslint-comment.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/ban-tslint-comment.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/default-param-last.shot b/packages/eslint-plugin/tests/schema-snapshots/default-param-last.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/default-param-last.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/default-param-last.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-array-constructor.shot b/packages/eslint-plugin/tests/schema-snapshots/no-array-constructor.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-array-constructor.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-array-constructor.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-array-delete.shot b/packages/eslint-plugin/tests/schema-snapshots/no-array-delete.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-array-delete.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-array-delete.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-confusing-non-null-assertion.shot b/packages/eslint-plugin/tests/schema-snapshots/no-confusing-non-null-assertion.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-confusing-non-null-assertion.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-confusing-non-null-assertion.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-dupe-class-members.shot b/packages/eslint-plugin/tests/schema-snapshots/no-dupe-class-members.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-dupe-class-members.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-dupe-class-members.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-duplicate-enum-values.shot b/packages/eslint-plugin/tests/schema-snapshots/no-duplicate-enum-values.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-duplicate-enum-values.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-duplicate-enum-values.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-dynamic-delete.shot b/packages/eslint-plugin/tests/schema-snapshots/no-dynamic-delete.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-dynamic-delete.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-dynamic-delete.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-extra-non-null-assertion.shot b/packages/eslint-plugin/tests/schema-snapshots/no-extra-non-null-assertion.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-extra-non-null-assertion.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-extra-non-null-assertion.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-for-in-array.shot b/packages/eslint-plugin/tests/schema-snapshots/no-for-in-array.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-for-in-array.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-for-in-array.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-implied-eval.shot b/packages/eslint-plugin/tests/schema-snapshots/no-implied-eval.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-implied-eval.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-implied-eval.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-import-type-side-effects.shot b/packages/eslint-plugin/tests/schema-snapshots/no-import-type-side-effects.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-import-type-side-effects.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-import-type-side-effects.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-loop-func.shot b/packages/eslint-plugin/tests/schema-snapshots/no-loop-func.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-loop-func.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-loop-func.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-loss-of-precision.shot b/packages/eslint-plugin/tests/schema-snapshots/no-loss-of-precision.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-loss-of-precision.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-loss-of-precision.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-misused-new.shot b/packages/eslint-plugin/tests/schema-snapshots/no-misused-new.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-misused-new.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-misused-new.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-mixed-enums.shot b/packages/eslint-plugin/tests/schema-snapshots/no-mixed-enums.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-mixed-enums.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-mixed-enums.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-non-null-asserted-nullish-coalescing.shot b/packages/eslint-plugin/tests/schema-snapshots/no-non-null-asserted-nullish-coalescing.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-non-null-asserted-nullish-coalescing.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-non-null-asserted-nullish-coalescing.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-non-null-asserted-optional-chain.shot b/packages/eslint-plugin/tests/schema-snapshots/no-non-null-asserted-optional-chain.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-non-null-asserted-optional-chain.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-non-null-asserted-optional-chain.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-non-null-assertion.shot b/packages/eslint-plugin/tests/schema-snapshots/no-non-null-assertion.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-non-null-assertion.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-non-null-assertion.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-redundant-type-constituents.shot b/packages/eslint-plugin/tests/schema-snapshots/no-redundant-type-constituents.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-redundant-type-constituents.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-redundant-type-constituents.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-parameter-property-assignment.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-parameter-property-assignment.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-parameter-property-assignment.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-parameter-property-assignment.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-qualifier.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-qualifier.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-qualifier.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-qualifier.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-template-expression.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-template-expression.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-template-expression.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-template-expression.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-arguments.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-arguments.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-arguments.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-arguments.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-constraint.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-constraint.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-constraint.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-constraint.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-conversion.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-conversion.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-conversion.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-conversion.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-parameters.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-parameters.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-parameters.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-parameters.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-argument.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-argument.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-argument.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-argument.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-assignment.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-assignment.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-assignment.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-assignment.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-call.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-call.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-call.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-call.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-declaration-merging.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-declaration-merging.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-declaration-merging.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-declaration-merging.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-enum-comparison.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-enum-comparison.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-enum-comparison.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-enum-comparison.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-function-type.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-function-type.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-function-type.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-function-type.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-member-access.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-member-access.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-member-access.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-member-access.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-return.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-return.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-return.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-return.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-type-assertion.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-type-assertion.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-type-assertion.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-type-assertion.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-unary-minus.shot b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-unary-minus.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-unary-minus.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-unsafe-unary-minus.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-useless-constructor.shot b/packages/eslint-plugin/tests/schema-snapshots/no-useless-constructor.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-useless-constructor.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-useless-constructor.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-useless-empty-export.shot b/packages/eslint-plugin/tests/schema-snapshots/no-useless-empty-export.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-useless-empty-export.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-useless-empty-export.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-wrapper-object-types.shot b/packages/eslint-plugin/tests/schema-snapshots/no-wrapper-object-types.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/no-wrapper-object-types.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/no-wrapper-object-types.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/non-nullable-type-assertion-style.shot b/packages/eslint-plugin/tests/schema-snapshots/non-nullable-type-assertion-style.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/non-nullable-type-assertion-style.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/non-nullable-type-assertion-style.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-as-const.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-as-const.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/prefer-as-const.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-as-const.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-enum-initializers.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-enum-initializers.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/prefer-enum-initializers.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-enum-initializers.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-find.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-find.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/prefer-find.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-find.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-for-of.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-for-of.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/prefer-for-of.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-for-of.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-function-type.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-function-type.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/prefer-function-type.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-function-type.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-includes.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-includes.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/prefer-includes.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-includes.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-namespace-keyword.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-namespace-keyword.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/prefer-namespace-keyword.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-namespace-keyword.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-reduce-type-parameter.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-reduce-type-parameter.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/prefer-reduce-type-parameter.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-reduce-type-parameter.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-regexp-exec.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-regexp-exec.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/prefer-regexp-exec.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-regexp-exec.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-return-this-type.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-return-this-type.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/prefer-return-this-type.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-return-this-type.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/prefer-ts-expect-error.shot b/packages/eslint-plugin/tests/schema-snapshots/prefer-ts-expect-error.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/prefer-ts-expect-error.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/prefer-ts-expect-error.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/related-getter-setter-pairs.shot b/packages/eslint-plugin/tests/schema-snapshots/related-getter-setter-pairs.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/related-getter-setter-pairs.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/related-getter-setter-pairs.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/require-await.shot b/packages/eslint-plugin/tests/schema-snapshots/require-await.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/require-await.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/require-await.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schema-snapshots/use-unknown-in-catch-callback-variable.shot b/packages/eslint-plugin/tests/schema-snapshots/use-unknown-in-catch-callback-variable.shot
index 42f81875ed94..cdd9f8375858 100644
--- a/packages/eslint-plugin/tests/schema-snapshots/use-unknown-in-catch-callback-variable.shot
+++ b/packages/eslint-plugin/tests/schema-snapshots/use-unknown-in-catch-callback-variable.shot
@@ -7,4 +7,4 @@
# TYPES:
/** No options declared */
-type Options = [];
\ No newline at end of file
+type Options = [];
diff --git a/packages/eslint-plugin/tests/schemas.test.ts b/packages/eslint-plugin/tests/schemas.test.ts
index 14380218c9e8..4d4e5c9b2ac3 100644
--- a/packages/eslint-plugin/tests/schemas.test.ts
+++ b/packages/eslint-plugin/tests/schemas.test.ts
@@ -1,4 +1,4 @@
-import { compile } from '@typescript-eslint/rule-schema-to-typescript-types';
+import { schemaToTypes } from '@typescript-eslint/rule-schema-to-typescript-types';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import prettier from 'prettier';
@@ -41,7 +41,7 @@ const ruleEntries = Object.entries(rules);
describe('Rule schemas should be convertible to TS types for documentation purposes', async () => {
const PRETTIER_CONFIG = {
schema: await getPrettierConfig(SCHEMA_FILEPATH),
- tsType: getPrettierConfig(TS_TYPE_FILEPATH),
+ tsType: await getPrettierConfig(TS_TYPE_FILEPATH),
};
beforeAll(async () => {
@@ -83,8 +83,8 @@ describe('Rule schemas should be convertible to TS types for documentation purpo
),
PRETTIER_CONFIG.schema,
);
- const compilationResult = await compile(
- ruleDef.meta.schema,
+ const compilationResult = await prettier.format(
+ schemaToTypes(ruleDef.meta.schema),
PRETTIER_CONFIG.tsType,
);
diff --git a/packages/rule-schema-to-typescript-types/package.json b/packages/rule-schema-to-typescript-types/package.json
index bdb5dc885613..44432badd288 100644
--- a/packages/rule-schema-to-typescript-types/package.json
+++ b/packages/rule-schema-to-typescript-types/package.json
@@ -1,8 +1,8 @@
{
"name": "@typescript-eslint/rule-schema-to-typescript-types",
"version": "8.45.0",
- "private": true,
- "type": "commonjs",
+ "description": "Converts ESLint rule schemas to equivalent TypeScript type strings.",
+ "type": "module",
"exports": {
".": {
"types": "./index.d.ts",
@@ -34,8 +34,7 @@
"dependencies": {
"@typescript-eslint/type-utils": "8.45.0",
"@typescript-eslint/utils": "8.45.0",
- "natural-compare": "^1.4.0",
- "prettier": "3.6.2"
+ "natural-compare": "^1.4.0"
},
"devDependencies": {
"@vitest/coverage-v8": "^3.1.3",
@@ -48,6 +47,9 @@
"type": "opencollective",
"url": "https://opencollective.com/typescript-eslint"
},
+ "publishConfig": {
+ "access": "public"
+ },
"nx": {
"name": "rule-schema-to-typescript-types",
"includedScripts": [
diff --git a/packages/rule-schema-to-typescript-types/src/generateArrayType.ts b/packages/rule-schema-to-typescript-types/src/generateArrayType.ts
index 54c1499f8a6c..e86e51f72dce 100644
--- a/packages/rule-schema-to-typescript-types/src/generateArrayType.ts
+++ b/packages/rule-schema-to-typescript-types/src/generateArrayType.ts
@@ -5,11 +5,17 @@ import type {
import { TSUtils } from '@typescript-eslint/utils';
-import type { ArrayAST, AST, RefMap, TupleAST, UnionAST } from './types';
+import type {
+ ArrayAST,
+ SchemaAST,
+ RefMap,
+ TupleAST,
+ UnionAST,
+} from './types.js';
-import { NotSupportedError, UnexpectedError } from './errors';
-import { generateType } from './generateType';
-import { getCommentLines } from './getCommentLines';
+import { NotSupportedError, UnexpectedError } from './errors.js';
+import { generateType } from './generateType.js';
+import { getCommentLines } from './getCommentLines.js';
/**
* If there are more than 20 tuple items then we will not make it a tuple type
@@ -95,7 +101,7 @@ export function generateArrayType(
const b: B = ['a', undefined, 'c'] // TS error
*/
const cumulativeTypesList = itemTypes.slice(0, minItems);
- const typesToUnion: AST[] = [];
+ const typesToUnion: SchemaAST[] = [];
if (cumulativeTypesList.length > 0) {
// actually has minItems, so add the initial state
typesToUnion.push(createTupleType(cumulativeTypesList));
@@ -131,8 +137,8 @@ export function generateArrayType(
}
function createTupleType(
- elements: AST[],
- spreadType: AST | null = null,
+ elements: SchemaAST[],
+ spreadType: SchemaAST | null = null,
): TupleAST {
return {
type: 'tuple',
diff --git a/packages/rule-schema-to-typescript-types/src/generateObjectType.ts b/packages/rule-schema-to-typescript-types/src/generateObjectType.ts
index 61a88a4f1e8c..1d2592f62aff 100644
--- a/packages/rule-schema-to-typescript-types/src/generateObjectType.ts
+++ b/packages/rule-schema-to-typescript-types/src/generateObjectType.ts
@@ -3,10 +3,10 @@ import type { JSONSchema4ObjectSchema } from '@typescript-eslint/utils/json-sche
import { requiresQuoting } from '@typescript-eslint/type-utils';
import { TSUtils } from '@typescript-eslint/utils';
-import type { AST, ObjectAST, RefMap } from './types';
+import type { SchemaAST, ObjectAST, RefMap } from './types.js';
-import { generateType } from './generateType';
-import { getCommentLines } from './getCommentLines';
+import { generateType } from './generateType.js';
+import { getCommentLines } from './getCommentLines.js';
export function generateObjectType(
schema: JSONSchema4ObjectSchema,
@@ -14,7 +14,7 @@ export function generateObjectType(
): ObjectAST {
const commentLines = getCommentLines(schema);
- let indexSignature: AST | null = null;
+ let indexSignature: SchemaAST | null = null;
if (
schema.additionalProperties === true ||
// eslint-disable-next-line @typescript-eslint/internal/eqeq-nullish
diff --git a/packages/rule-schema-to-typescript-types/src/generateType.ts b/packages/rule-schema-to-typescript-types/src/generateType.ts
index 00d4e0d6e9c0..f5ef138ce3a0 100644
--- a/packages/rule-schema-to-typescript-types/src/generateType.ts
+++ b/packages/rule-schema-to-typescript-types/src/generateType.ts
@@ -2,13 +2,13 @@ import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema';
import { TSUtils } from '@typescript-eslint/utils';
-import type { AST, RefMap } from './types';
+import type { SchemaAST, RefMap } from './types.js';
-import { NotSupportedError, UnexpectedError } from './errors';
-import { generateArrayType } from './generateArrayType';
-import { generateObjectType } from './generateObjectType';
-import { generateUnionType } from './generateUnionType';
-import { getCommentLines } from './getCommentLines';
+import { NotSupportedError, UnexpectedError } from './errors.js';
+import { generateArrayType } from './generateArrayType.js';
+import { generateObjectType } from './generateObjectType.js';
+import { generateUnionType } from './generateUnionType.js';
+import { getCommentLines } from './getCommentLines.js';
// keywords we probably should support but currently do not support
const UNSUPPORTED_KEYWORDS = new Set([
@@ -22,7 +22,7 @@ const UNSUPPORTED_KEYWORDS = new Set([
'patternProperties',
]);
-export function generateType(schema: JSONSchema4, refMap: RefMap): AST {
+export function generateType(schema: JSONSchema4, refMap: RefMap): SchemaAST {
const unsupportedProps = Object.keys(schema).filter(key =>
UNSUPPORTED_KEYWORDS.has(key),
);
diff --git a/packages/rule-schema-to-typescript-types/src/generateUnionType.ts b/packages/rule-schema-to-typescript-types/src/generateUnionType.ts
index ced5d9bf63aa..6370abf3a8e1 100644
--- a/packages/rule-schema-to-typescript-types/src/generateUnionType.ts
+++ b/packages/rule-schema-to-typescript-types/src/generateUnionType.ts
@@ -3,20 +3,20 @@ import type {
JSONSchema4Type,
} from '@typescript-eslint/utils/json-schema';
-import type { AST, RefMap, UnionAST } from './types';
+import type { SchemaAST, RefMap, UnionAST } from './types.js';
-import { NotSupportedError } from './errors';
-import { generateType } from './generateType';
+import { NotSupportedError } from './errors.js';
+import { generateType } from './generateType.js';
export function generateUnionType(
members: (JSONSchema4 | JSONSchema4Type)[],
refMap: RefMap,
): UnionAST {
- const elements: AST[] = [];
+ const elements: SchemaAST[] = [];
for (const memberSchema of members) {
elements.push(
- ((): AST => {
+ ((): SchemaAST => {
switch (typeof memberSchema) {
case 'string':
return {
diff --git a/packages/rule-schema-to-typescript-types/src/index.ts b/packages/rule-schema-to-typescript-types/src/index.ts
index e046d9ced2c6..d61da632a371 100644
--- a/packages/rule-schema-to-typescript-types/src/index.ts
+++ b/packages/rule-schema-to-typescript-types/src/index.ts
@@ -1,39 +1,34 @@
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema';
import { TSUtils } from '@typescript-eslint/utils';
-import prettier from 'prettier';
-import type { AST } from './types';
+import type { SchemaAST } from './types.js';
-import { generateType } from './generateType';
-import { optimizeAST } from './optimizeAST';
-import { printTypeAlias } from './printAST';
+import { generateType } from './generateType.js';
+import { optimizeAST } from './optimizeAST.js';
+import { printTypeAlias } from './printAST.js';
-export async function compile(
- schemaIn: JSONSchema4 | readonly JSONSchema4[],
- prettierConfig: Promise,
-): Promise {
- const { isArraySchema, schema } = (() => {
- if (TSUtils.isArray(schemaIn)) {
- return {
- isArraySchema: true,
- schema: schemaIn,
- };
- }
- return {
- isArraySchema: false,
- schema: [schemaIn],
- };
- })();
+/**
+ * Converts rule options schema(s) to the equivalent TypeScript type string.
+ *
+ * @param schema Original rule schema(s) as declared in `meta.schema`.
+ * @returns Stringified TypeScript type(s) equivalent to the options schema(s).
+ */
+export function schemaToTypes(
+ schema: JSONSchema4 | readonly JSONSchema4[],
+): string {
+ const [isArraySchema, schemaNormalized] = TSUtils.isArray(schema)
+ ? [true, schema]
+ : [false, [schema]];
- if (schema.length === 0) {
+ if (schemaNormalized.length === 0) {
return ['/** No options declared */', 'type Options = [];'].join('\n');
}
const refTypes: string[] = [];
- const types: AST[] = [];
- for (let i = 0; i < schema.length; i += 1) {
- const result = compileSchema(schema[i], i);
+ const types: SchemaAST[] = [];
+ for (let i = 0; i < schemaNormalized.length; i += 1) {
+ const result = compileSchema(schemaNormalized[i], i);
refTypes.push(...result.refTypes);
types.push(result.type);
}
@@ -47,21 +42,13 @@ export async function compile(
})
: printTypeAlias('Options', types[0]);
- const unformattedCode = [...refTypes, optionsType].join('\n\n');
- try {
- return await prettier.format(unformattedCode, await prettierConfig);
- } catch (e) {
- if (e instanceof Error) {
- e.message += `\n\nUnformatted Code:\n${unformattedCode}`;
- }
- throw e;
- }
+ return [...refTypes, optionsType].join('\n\n');
}
function compileSchema(
schema: JSONSchema4,
index: number,
-): { refTypes: string[]; type: AST } {
+): { refTypes: string[]; type: SchemaAST } {
const refTypes: string[] = [];
const refMap = new Map();
diff --git a/packages/rule-schema-to-typescript-types/src/optimizeAST.ts b/packages/rule-schema-to-typescript-types/src/optimizeAST.ts
index 14a0fd1c778a..0ec3626a46cd 100644
--- a/packages/rule-schema-to-typescript-types/src/optimizeAST.ts
+++ b/packages/rule-schema-to-typescript-types/src/optimizeAST.ts
@@ -1,6 +1,6 @@
-import type { AST, UnionAST } from './types';
+import type { SchemaAST, UnionAST } from './types.js';
-export function optimizeAST(ast: AST | null): void {
+export function optimizeAST(ast: SchemaAST | null): void {
if (ast == null) {
return;
}
@@ -40,7 +40,7 @@ export function optimizeAST(ast: AST | null): void {
}
// hacky way to deduplicate union members
- const uniqueElementsMap = new Map();
+ const uniqueElementsMap = new Map();
for (const element of elements) {
uniqueElementsMap.set(JSON.stringify(element), element);
}
@@ -53,8 +53,8 @@ export function optimizeAST(ast: AST | null): void {
}
}
-function unwrapUnions(union: UnionAST): AST[] {
- const elements: AST[] = [];
+function unwrapUnions(union: UnionAST): SchemaAST[] {
+ const elements: SchemaAST[] = [];
for (const element of union.elements) {
if (element.type === 'union') {
elements.push(...unwrapUnions(element));
diff --git a/packages/rule-schema-to-typescript-types/src/printAST.ts b/packages/rule-schema-to-typescript-types/src/printAST.ts
index 746d0b850b0c..a618b903a5d0 100644
--- a/packages/rule-schema-to-typescript-types/src/printAST.ts
+++ b/packages/rule-schema-to-typescript-types/src/printAST.ts
@@ -1,12 +1,12 @@
import naturalCompare from 'natural-compare';
-import type { AST, TupleAST } from './types';
+import type { SchemaAST, TupleAST } from './types.js';
-export function printTypeAlias(aliasName: string, ast: AST): string {
+export function printTypeAlias(aliasName: string, ast: SchemaAST): string {
return `${printComment(ast)}type ${aliasName} = ${printAST(ast).code}`;
}
-export function printASTWithComment(ast: AST): string {
+export function printASTWithComment(ast: SchemaAST): string {
const result = printAST(ast);
return `${printComment(result)}${result.code}`;
}
@@ -36,7 +36,7 @@ interface CodeWithComments {
code: string;
commentLines: string[];
}
-function printAST(ast: AST): CodeWithComments {
+function printAST(ast: SchemaAST): CodeWithComments {
switch (ast.type) {
case 'array': {
const code = printAndMaybeParenthesise(ast.elementType);
@@ -124,7 +124,7 @@ function printAST(ast: AST): CodeWithComments {
interface Element {
code: string;
- element: AST;
+ element: SchemaAST;
}
function compareElements(a: Element, b: Element): number {
if (a.element.type !== b.element.type) {
@@ -153,7 +153,7 @@ function compareElements(a: Element, b: Element): number {
}
}
-function printAndMaybeParenthesise(ast: AST): CodeWithComments {
+function printAndMaybeParenthesise(ast: SchemaAST): CodeWithComments {
const printed = printAST(ast);
if (ast.type === 'union') {
return {
diff --git a/packages/rule-schema-to-typescript-types/src/types.ts b/packages/rule-schema-to-typescript-types/src/types.ts
index 9edcbf0b404c..e2026a4e8917 100644
--- a/packages/rule-schema-to-typescript-types/src/types.ts
+++ b/packages/rule-schema-to-typescript-types/src/types.ts
@@ -1,3 +1,6 @@
+/**
+ * Maps ref paths to generated type names.
+ */
export type RefMap = ReadonlyMap<
// ref path
string,
@@ -5,7 +8,10 @@ export type RefMap = ReadonlyMap<
string
>;
-export type AST =
+/**
+ * Minimal representation of the nodes in a schema being compiled to types.
+ */
+export type SchemaAST =
| ArrayAST
| LiteralAST
| ObjectAST
@@ -13,37 +19,37 @@ export type AST =
| TypeReferenceAST
| UnionAST;
-interface BaseASTNode {
+export interface BaseSchemaASTNode {
readonly commentLines: string[];
}
-export interface ArrayAST extends BaseASTNode {
- readonly elementType: AST;
+export interface ArrayAST extends BaseSchemaASTNode {
+ readonly elementType: SchemaAST;
readonly type: 'array';
}
-export interface LiteralAST extends BaseASTNode {
+export interface LiteralAST extends BaseSchemaASTNode {
readonly code: string;
readonly type: 'literal';
}
-export interface ObjectAST extends BaseASTNode {
- readonly indexSignature: AST | null;
+export interface ObjectAST extends BaseSchemaASTNode {
+ readonly indexSignature: SchemaAST | null;
readonly properties: {
readonly name: string;
readonly optional: boolean;
- readonly type: AST;
+ readonly type: SchemaAST;
}[];
readonly type: 'object';
}
-export interface TupleAST extends BaseASTNode {
- readonly elements: AST[];
- readonly spreadType: AST | null;
+export interface TupleAST extends BaseSchemaASTNode {
+ readonly elements: SchemaAST[];
+ readonly spreadType: SchemaAST | null;
readonly type: 'tuple';
}
-export interface TypeReferenceAST extends BaseASTNode {
+export interface TypeReferenceAST extends BaseSchemaASTNode {
readonly type: 'type-reference';
readonly typeName: string;
}
-export interface UnionAST extends BaseASTNode {
- readonly elements: AST[];
+export interface UnionAST extends BaseSchemaASTNode {
+ readonly elements: SchemaAST[];
readonly type: 'union';
}
diff --git a/packages/rule-schema-to-typescript-types/tests/index.test.ts b/packages/rule-schema-to-typescript-types/tests/index.test.ts
new file mode 100644
index 000000000000..49db40f209f7
--- /dev/null
+++ b/packages/rule-schema-to-typescript-types/tests/index.test.ts
@@ -0,0 +1,148 @@
+import { schemaToTypes } from '../src/index.js';
+
+describe(schemaToTypes, () => {
+ it('returns a [] type when the schema is an empty array', () => {
+ const actual = schemaToTypes([]);
+
+ expect(actual).toMatchInlineSnapshot(`
+ "/** No options declared */
+ type Options = [];"
+ `);
+ });
+
+ it('returns a single Options type when the schema is not an array', () => {
+ const actual = schemaToTypes({ type: 'string' });
+
+ expect(actual).toMatchInlineSnapshot(`"type Options = string"`);
+ });
+
+ it('returns an array Options type when the schema is an array', () => {
+ const actual = schemaToTypes([{ type: 'string' }]);
+
+ expect(actual).toMatchInlineSnapshot(`"type Options = [string]"`);
+ });
+
+ it('returns a complex Options type when the schema contains an array of items', () => {
+ const actual = schemaToTypes([
+ {
+ items: [{ type: 'string' }],
+ type: 'array',
+ },
+ ]);
+
+ expect(actual).toMatchInlineSnapshot(`
+ "type Options = [ | []
+ | [string]]"
+ `);
+ });
+
+ it('returns a complex Options type when the schema is nested', () => {
+ const actual = schemaToTypes([
+ {
+ description: 'My schema items.',
+ items: { type: 'string' },
+ type: 'array',
+ },
+ ]);
+
+ expect(actual).toMatchInlineSnapshot(`
+ "type Options = [/** My schema items. */
+ string[]]"
+ `);
+ });
+
+ it('factors in one $ref property when a $defs property exists at the top level', () => {
+ const actual = schemaToTypes([
+ {
+ $defs: {
+ defOption: {
+ enum: ['a', 'b'],
+ type: 'string',
+ },
+ },
+ additionalProperties: false,
+ properties: {
+ one: {
+ $ref: '#/items/0/$defs/defOption',
+ },
+ },
+ type: 'object',
+ },
+ ]);
+
+ expect(actual).toMatchInlineSnapshot(`
+ "type DefOption = | 'a'
+ | 'b'
+
+ type Options = [{
+ one?: DefOption}]"
+ `);
+ });
+
+ it('factors in one $ref property when a definitions property exists at the top level', () => {
+ const actual = schemaToTypes([
+ {
+ additionalProperties: false,
+ definitions: {
+ defOption: {
+ enum: ['a', 'b'],
+ type: 'string',
+ },
+ },
+ properties: {
+ one: {
+ $ref: '#/items/0/$defs/defOption',
+ },
+ },
+ type: 'object',
+ },
+ ]);
+
+ expect(actual).toMatchInlineSnapshot(`
+ "type DefOption = | 'a'
+ | 'b'
+
+ type Options = [{
+ one?: DefOption}]"
+ `);
+ });
+
+ it('factors in two $ref properties when two $defs properties exist at the top level', () => {
+ const actual = schemaToTypes([
+ {
+ $defs: {
+ defOptionOne: {
+ enum: ['a', 'b'],
+ type: 'string',
+ },
+ defOptionTwo: {
+ enum: ['c', 'd'],
+ type: 'string',
+ },
+ },
+ additionalProperties: false,
+ properties: {
+ one: {
+ $ref: '#/items/0/$defs/defOptionOne',
+ },
+ two: {
+ $ref: '#/items/0/$defs/defOptionTwo',
+ },
+ },
+ type: 'object',
+ },
+ ]);
+
+ expect(actual).toMatchInlineSnapshot(`
+ "type DefOptionOne = | 'a'
+ | 'b'
+
+ type DefOptionTwo = | 'c'
+ | 'd'
+
+ type Options = [{
+ one?: DefOptionOne;
+ two?: DefOptionTwo}]"
+ `);
+ });
+});
diff --git a/packages/website/docusaurus.config.mts b/packages/website/docusaurus.config.mts
index 3e6ff8e252f4..9a51ea074f54 100644
--- a/packages/website/docusaurus.config.mts
+++ b/packages/website/docusaurus.config.mts
@@ -368,32 +368,34 @@ const config: Config = {
organizationName: 'typescript-eslint',
plugins: [
'./plugins/recent-blog-posts/index.ts',
- ...['ast-spec', 'project-service', 'tsconfig-utils', 'type-utils'].map(
- packageName => [
- 'docusaurus-plugin-typedoc',
- {
- entryPoints: [`../${packageName}/src/index.ts`],
- enumMembersFormat: 'table',
- exclude: '**/*.d.ts',
- excludeExternals: true,
- groupOrder: ['Functions', 'Variables', '*'],
- hidePageTitle: true,
- id: `typedoc-generated-${packageName}`,
- indexFormat: 'table',
- out: `../../docs/packages/${packageName}/generated`,
- outputFileStrategy: 'modules',
- parametersFormat: 'table',
- plugin: [
- require.resolve('./tools/typedoc-plugin-no-inherit-fork.mjs'),
- ],
- propertiesFormat: 'table',
- readme: 'none',
- tsconfig: `../${packageName}/tsconfig.json`,
- typeDeclarationFormat: 'table',
- useCodeBlocks: true,
- },
- ],
- ),
+ ...[
+ 'ast-spec',
+ 'project-service',
+ 'rule-schema-to-typescript-types',
+ 'tsconfig-utils',
+ 'type-utils',
+ ].map(packageName => [
+ 'docusaurus-plugin-typedoc',
+ {
+ entryPoints: [`../${packageName}/src/index.ts`],
+ enumMembersFormat: 'table',
+ exclude: '**/*.d.ts',
+ excludeExternals: true,
+ groupOrder: ['Functions', 'Variables', '*'],
+ hidePageTitle: true,
+ id: `typedoc-generated-${packageName}`,
+ indexFormat: 'table',
+ out: `../../docs/packages/${packageName}/generated`,
+ outputFileStrategy: 'modules',
+ parametersFormat: 'table',
+ plugin: [require.resolve('./tools/typedoc-plugin-no-inherit-fork.mjs')],
+ propertiesFormat: 'table',
+ readme: 'none',
+ tsconfig: `../${packageName}/tsconfig.json`,
+ typeDeclarationFormat: 'table',
+ useCodeBlocks: true,
+ },
+ ]),
require.resolve('./webpack.plugin'),
['@docusaurus/plugin-content-docs', pluginContentDocsOptions],
['@docusaurus/plugin-pwa', pluginPwaOptions],
diff --git a/packages/website/plugins/generated-rule-docs/insertions/insertNewRuleReferences.ts b/packages/website/plugins/generated-rule-docs/insertions/insertNewRuleReferences.ts
index 89230d62a2fb..2154ca000f61 100644
--- a/packages/website/plugins/generated-rule-docs/insertions/insertNewRuleReferences.ts
+++ b/packages/website/plugins/generated-rule-docs/insertions/insertNewRuleReferences.ts
@@ -2,7 +2,7 @@ import type { ESLintPluginDocs } from '@typescript-eslint/eslint-plugin/use-at-y
import type * as mdast from 'mdast';
import type { MdxJsxFlowElement } from 'mdast-util-mdx';
-import { compile } from '@typescript-eslint/rule-schema-to-typescript-types';
+import { schemaToTypes } from '@typescript-eslint/rule-schema-to-typescript-types';
import { EOL } from 'node:os';
import * as path from 'node:path';
import prettier from 'prettier';
@@ -32,7 +32,7 @@ const PRETTIER_CONFIG_PATH = path.resolve(
'..',
'.prettierrc.json',
);
-const prettierConfig = (async () => {
+const lazyPrettierConfig = (async () => {
const filepath = path.join(__dirname, 'file.ts');
const config = await prettier.resolveConfig(filepath, {
config: PRETTIER_CONFIG_PATH,
@@ -149,6 +149,7 @@ export async function insertNewRuleReferences(
'This rule is not configurable.',
);
} else if (!COMPLICATED_RULE_OPTIONS.has(page.file.stem)) {
+ const prettierConfig = await lazyPrettierConfig;
page.spliceChildren(
page.headingIndices.options + 1,
0,
@@ -159,11 +160,11 @@ export async function insertNewRuleReferences(
lang: 'ts',
type: 'code',
value: [
- await compile(page.rule.meta.schema, prettierConfig),
await prettier.format(
- getRuleDefaultOptions(page),
- await prettierConfig,
+ schemaToTypes(page.rule.meta.schema),
+ prettierConfig,
),
+ await prettier.format(getRuleDefaultOptions(page), prettierConfig),
]
.join(EOL)
.trim(),
diff --git a/packages/website/sidebars/sidebar.base.js b/packages/website/sidebars/sidebar.base.js
index 689a5bb14202..a6d6e196d9e0 100644
--- a/packages/website/sidebars/sidebar.base.js
+++ b/packages/website/sidebars/sidebar.base.js
@@ -104,6 +104,7 @@ module.exports = {
'packages/eslint-plugin-tslint',
'packages/parser',
'packages/project-service',
+ 'packages/rule-schema-to-typescript-types',
'packages/rule-tester',
'packages/scope-manager',
'packages/tsconfig-utils',
diff --git a/yarn.lock b/yarn.lock
index 1c943fd78410..c2f925078dab 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5945,7 +5945,6 @@ __metadata:
"@vitest/coverage-v8": ^3.1.3
eslint: "*"
natural-compare: ^1.4.0
- prettier: 3.6.2
rimraf: "*"
typescript: "*"
vitest: ^3.1.3