diff --git a/eslint.config.mjs b/eslint.config.mjs index 115317235ae0..6a471b0b3b31 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -574,6 +574,7 @@ export default tseslint.config( 'packages/ast-spec/{src,tests,typings}/**/*.ts', 'packages/integration-tests/{tests,tools,typing}/**/*.ts', 'packages/parser/{src,tests}/**/*.ts', + 'packages/rule-schema-to-typescript-types/src/**/*.ts', 'packages/rule-tester/{src,tests,typings}/**/*.ts', 'packages/types/{src,tools}/**/*.ts', 'packages/typescript-eslint/{src,tests}/**/*.ts', diff --git a/packages/rule-schema-to-typescript-types/src/generateArrayType.ts b/packages/rule-schema-to-typescript-types/src/generateArrayType.ts index f8a551c61092..1952347948f4 100644 --- a/packages/rule-schema-to-typescript-types/src/generateArrayType.ts +++ b/packages/rule-schema-to-typescript-types/src/generateArrayType.ts @@ -1,13 +1,15 @@ -import { TSUtils } from '@typescript-eslint/utils'; import type { JSONSchema4, JSONSchema4ArraySchema, } from '@typescript-eslint/utils/json-schema'; +import { TSUtils } from '@typescript-eslint/utils'; + +import type { ArrayAST, AST, RefMap, TupleAST, UnionAST } from './types'; + import { NotSupportedError, UnexpectedError } from './errors'; import { generateType } from './generateType'; import { getCommentLines } from './getCommentLines'; -import type { ArrayAST, AST, RefMap, TupleAST, UnionAST } from './types'; /** * If there are more than 20 tuple items then we will not make it a tuple type @@ -59,9 +61,9 @@ export function generateArrayType( } else { // treat as an array type return { - type: 'array', - elementType: generateType(schema.items, refMap), commentLines, + elementType: generateType(schema.items, refMap), + type: 'array', }; } } else { @@ -128,17 +130,17 @@ export function generateArrayType( } return { - type: 'union', - elements: typesToUnion, commentLines, + elements: typesToUnion, + type: 'union', }; } return { - type: 'tuple', + commentLines, elements: itemTypes, spreadType: spreadItem, - commentLines, + type: 'tuple', }; } @@ -149,8 +151,8 @@ function createTupleType( return { type: 'tuple', // clone the array because we know we'll keep mutating it + commentLines: [], elements: [...elements], spreadType, - commentLines: [], }; } diff --git a/packages/rule-schema-to-typescript-types/src/generateObjectType.ts b/packages/rule-schema-to-typescript-types/src/generateObjectType.ts index 6f493c498aa3..7fd3d84f6b06 100644 --- a/packages/rule-schema-to-typescript-types/src/generateObjectType.ts +++ b/packages/rule-schema-to-typescript-types/src/generateObjectType.ts @@ -1,10 +1,12 @@ +import type { JSONSchema4ObjectSchema } from '@typescript-eslint/utils/json-schema'; + import { requiresQuoting } from '@typescript-eslint/type-utils'; import { TSUtils } from '@typescript-eslint/utils'; -import type { JSONSchema4ObjectSchema } from '@typescript-eslint/utils/json-schema'; + +import type { AST, ObjectAST, RefMap } from './types'; import { generateType } from './generateType'; import { getCommentLines } from './getCommentLines'; -import type { AST, ObjectAST, RefMap } from './types'; export function generateObjectType( schema: JSONSchema4ObjectSchema, @@ -18,9 +20,9 @@ export function generateObjectType( schema.additionalProperties === undefined ) { indexSignature = { + commentLines: [], type: 'type-reference', typeName: 'unknown', - commentLines: [], }; } else if (typeof schema.additionalProperties === 'object') { const indexSigType = generateType(schema.additionalProperties, refMap); @@ -47,9 +49,9 @@ export function generateObjectType( } return { - type: 'object', - properties, - indexSignature, commentLines, + indexSignature, + properties, + type: 'object', }; } diff --git a/packages/rule-schema-to-typescript-types/src/generateType.ts b/packages/rule-schema-to-typescript-types/src/generateType.ts index a2477a546338..68c39f114787 100644 --- a/packages/rule-schema-to-typescript-types/src/generateType.ts +++ b/packages/rule-schema-to-typescript-types/src/generateType.ts @@ -1,12 +1,14 @@ -import { TSUtils } from '@typescript-eslint/utils'; import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema'; +import { TSUtils } from '@typescript-eslint/utils'; + +import type { AST, RefMap } from './types'; + import { NotSupportedError, UnexpectedError } from './errors'; import { generateArrayType } from './generateArrayType'; import { generateObjectType } from './generateObjectType'; import { generateUnionType } from './generateUnionType'; import { getCommentLines } from './getCommentLines'; -import type { AST, RefMap } from './types'; // keywords we probably should support but currently do not support const UNSUPPORTED_KEYWORDS = new Set([ @@ -41,9 +43,9 @@ export function generateType(schema: JSONSchema4, refMap: RefMap): AST { ); } return { + commentLines, type: 'type-reference', typeName: refName, - commentLines, }; } if ('enum' in schema && schema.enum) { @@ -81,24 +83,24 @@ export function generateType(schema: JSONSchema4, refMap: RefMap): AST { switch (schema.type) { case 'any': return { + commentLines, type: 'type-reference', typeName: 'unknown', - commentLines, }; case 'null': return { + commentLines, type: 'type-reference', typeName: 'null', - commentLines, }; case 'number': case 'string': return { - type: 'literal', code: schema.type, commentLines, + type: 'literal', }; case 'array': @@ -106,16 +108,16 @@ export function generateType(schema: JSONSchema4, refMap: RefMap): AST { case 'boolean': return { + commentLines, type: 'type-reference', typeName: 'boolean', - commentLines, }; case 'integer': return { + commentLines, type: 'type-reference', typeName: 'number', - commentLines, }; case 'object': diff --git a/packages/rule-schema-to-typescript-types/src/generateUnionType.ts b/packages/rule-schema-to-typescript-types/src/generateUnionType.ts index dfed611c150a..ced5d9bf63aa 100644 --- a/packages/rule-schema-to-typescript-types/src/generateUnionType.ts +++ b/packages/rule-schema-to-typescript-types/src/generateUnionType.ts @@ -3,9 +3,10 @@ import type { JSONSchema4Type, } from '@typescript-eslint/utils/json-schema'; +import type { AST, RefMap, UnionAST } from './types'; + import { NotSupportedError } from './errors'; import { generateType } from './generateType'; -import type { AST, RefMap, UnionAST } from './types'; export function generateUnionType( members: (JSONSchema4 | JSONSchema4Type)[], @@ -19,17 +20,17 @@ export function generateUnionType( switch (typeof memberSchema) { case 'string': return { - type: 'literal', code: `'${memberSchema.replaceAll("'", "\\'")}'`, commentLines: [], + type: 'literal', }; case 'number': case 'boolean': return { - type: 'literal', code: `${memberSchema}`, commentLines: [], + type: 'literal', }; case 'object': @@ -46,8 +47,8 @@ export function generateUnionType( } return { - type: 'union', - elements, commentLines: [], + elements, + type: 'union', }; } diff --git a/packages/rule-schema-to-typescript-types/src/index.ts b/packages/rule-schema-to-typescript-types/src/index.ts index f4175fba634c..e046d9ced2c6 100644 --- a/packages/rule-schema-to-typescript-types/src/index.ts +++ b/packages/rule-schema-to-typescript-types/src/index.ts @@ -1,26 +1,28 @@ -import { TSUtils } from '@typescript-eslint/utils'; 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 { generateType } from './generateType'; import { optimizeAST } from './optimizeAST'; import { printTypeAlias } from './printAST'; -import type { AST } from './types'; export async function compile( schemaIn: JSONSchema4 | readonly JSONSchema4[], prettierConfig: Promise, ): Promise { - const { schema, isArraySchema } = (() => { + const { isArraySchema, schema } = (() => { if (TSUtils.isArray(schemaIn)) { return { - schema: schemaIn, isArraySchema: true, + schema: schemaIn, }; } return { - schema: [schemaIn], isArraySchema: false, + schema: [schemaIn], }; })(); @@ -38,10 +40,10 @@ export async function compile( const optionsType = isArraySchema ? printTypeAlias('Options', { - type: 'tuple', + commentLines: [], elements: types, spreadType: null, - commentLines: [], + type: 'tuple', }) : printTypeAlias('Options', types[0]); @@ -59,7 +61,7 @@ export async function compile( function compileSchema( schema: JSONSchema4, index: number, -): { type: AST; refTypes: string[] } { +): { refTypes: string[]; type: AST } { const refTypes: string[] = []; const refMap = new Map(); @@ -81,8 +83,8 @@ function compileSchema( optimizeAST(type); return { - type, refTypes, + type, }; } diff --git a/packages/rule-schema-to-typescript-types/src/types.ts b/packages/rule-schema-to-typescript-types/src/types.ts index 78d0bfa994d6..9edcbf0b404c 100644 --- a/packages/rule-schema-to-typescript-types/src/types.ts +++ b/packages/rule-schema-to-typescript-types/src/types.ts @@ -18,32 +18,32 @@ interface BaseASTNode { } export interface ArrayAST extends BaseASTNode { - readonly type: 'array'; readonly elementType: AST; + readonly type: 'array'; } export interface LiteralAST extends BaseASTNode { - readonly type: 'literal'; readonly code: string; + readonly type: 'literal'; } export interface ObjectAST extends BaseASTNode { - readonly type: 'object'; + readonly indexSignature: AST | null; readonly properties: { readonly name: string; readonly optional: boolean; readonly type: AST; }[]; - readonly indexSignature: AST | null; + readonly type: 'object'; } export interface TupleAST extends BaseASTNode { - readonly type: 'tuple'; readonly elements: AST[]; readonly spreadType: AST | null; + readonly type: 'tuple'; } export interface TypeReferenceAST extends BaseASTNode { readonly type: 'type-reference'; readonly typeName: string; } export interface UnionAST extends BaseASTNode { - readonly type: 'union'; readonly elements: AST[]; + readonly type: 'union'; }