Skip to content

chore: enable eslint-plugin-perfectionist on rule-schema-to-typescript-types package #9846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
20 changes: 11 additions & 9 deletions packages/rule-schema-to-typescript-types/src/generateArrayType.ts
Original file line number Diff line number Diff line change
@@ -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';
Comment on lines 1 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2c: I dislike this style of splitting type-only imports from value imports.
I personally didn't really like the grouping at all, but simple-import-sort used to be the "best autofixable" rule.

Question: Can we turn import grouping off entirely?
Given all relative imports are prefixed with ./ or ../ -- the relative imports are already "grouped".

If not, w/e -- it's autofixed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we should be able to. I commented #8479 (comment) to make sure it's done as part of #8479.

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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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',
};
}

Expand All @@ -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: [],
};
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
Expand All @@ -47,9 +49,9 @@ export function generateObjectType(
}

return {
type: 'object',
properties,
indexSignature,
commentLines,
indexSignature,
properties,
type: 'object',
};
}
18 changes: 10 additions & 8 deletions packages/rule-schema-to-typescript-types/src/generateType.ts
Original file line number Diff line number Diff line change
@@ -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([
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -81,41 +83,41 @@ 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':
return generateArrayType(schema, refMap);

case 'boolean':
return {
commentLines,
type: 'type-reference',
typeName: 'boolean',
commentLines,
};

case 'integer':
return {
commentLines,
type: 'type-reference',
typeName: 'number',
commentLines,
};

case 'object':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)[],
Expand All @@ -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':
Expand All @@ -46,8 +47,8 @@ export function generateUnionType(
}

return {
type: 'union',
elements,
commentLines: [],
elements,
type: 'union',
};
}
20 changes: 11 additions & 9 deletions packages/rule-schema-to-typescript-types/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<prettier.Options>,
): Promise<string> {
const { schema, isArraySchema } = (() => {
const { isArraySchema, schema } = (() => {
if (TSUtils.isArray(schemaIn)) {
return {
schema: schemaIn,
isArraySchema: true,
schema: schemaIn,
};
}
return {
schema: [schemaIn],
isArraySchema: false,
schema: [schemaIn],
};
})();

Expand All @@ -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]);

Expand All @@ -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<string, string>();
Expand All @@ -81,8 +83,8 @@ function compileSchema(
optimizeAST(type);

return {
type,
refTypes,
type,
};
}

Expand Down
12 changes: 6 additions & 6 deletions packages/rule-schema-to-typescript-types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Loading