Skip to content

chore: enable eslint-plugin-perfectionist on type-utils package #9849

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 @@ -609,6 +609,7 @@ export default tseslint.config(
'packages/rule-schema-to-typescript-types/src/**/*.ts',
'packages/rule-tester/{src,tests,typings}/**/*.ts',
'packages/scope-manager/{src,tests}/**/*.ts',
'packages/type-utils/{src,tests,typings}/**/*.ts',
'packages/types/{src,tools}/**/*.ts',
'packages/typescript-eslint/{src,tests}/**/*.ts',
'packages/utils/src/**/*.ts',
Expand Down
35 changes: 18 additions & 17 deletions packages/type-utils/src/TypeOrValueSpecifier.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema';
import * as tsutils from 'ts-api-utils';
import type * as ts from 'typescript';

import * as tsutils from 'ts-api-utils';

import { specifierNameMatches } from './typeOrValueSpecifiers/specifierNameMatches';
import { typeDeclaredInFile } from './typeOrValueSpecifiers/typeDeclaredInFile';
import { typeDeclaredInLib } from './typeOrValueSpecifiers/typeDeclaredInLib';
Expand Down Expand Up @@ -67,32 +68,30 @@ export type TypeOrValueSpecifier =
| string;

export const typeOrValueSpecifiersSchema = {
type: 'array',
items: {
oneOf: [
{
type: 'string',
},
{
type: 'object',
additionalProperties: false,
properties: {
from: {
type: 'string',
enum: ['file'],
type: 'string',
},
name: {
oneOf: [
{
type: 'string',
},
{
type: 'array',
minItems: 1,
uniqueItems: true,
items: {
type: 'string',
},
minItems: 1,
type: 'array',
uniqueItems: true,
},
],
},
Expand All @@ -101,53 +100,53 @@ export const typeOrValueSpecifiersSchema = {
},
},
required: ['from', 'name'],
type: 'object',
},
{
type: 'object',
additionalProperties: false,
properties: {
from: {
type: 'string',
enum: ['lib'],
type: 'string',
},
name: {
oneOf: [
{
type: 'string',
},
{
type: 'array',
minItems: 1,
uniqueItems: true,
items: {
type: 'string',
},
minItems: 1,
type: 'array',
uniqueItems: true,
},
],
},
},
required: ['from', 'name'],
type: 'object',
},
{
type: 'object',
additionalProperties: false,
properties: {
from: {
type: 'string',
enum: ['package'],
type: 'string',
},
name: {
oneOf: [
{
type: 'string',
},
{
type: 'array',
minItems: 1,
uniqueItems: true,
items: {
type: 'string',
},
minItems: 1,
type: 'array',
uniqueItems: true,
},
],
},
Expand All @@ -156,9 +155,11 @@ export const typeOrValueSpecifiersSchema = {
},
},
required: ['from', 'name', 'package'],
type: 'object',
},
],
},
type: 'array',
} as const satisfies JSONSchema4;

export function typeMatchesSpecifier(
Expand Down
14 changes: 7 additions & 7 deletions packages/type-utils/src/builtinSymbolLikes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ export function isReadonlyTypeLike(
program: ts.Program,
type: ts.Type,
predicate?: (
subType: ts.Type & {
subType: {
aliasSymbol: ts.Symbol;
aliasTypeArguments: readonly ts.Type[];
},
} & ts.Type,
) => boolean,
): boolean {
return isBuiltinTypeAliasLike(program, type, subtype => {
Expand All @@ -89,10 +89,10 @@ export function isBuiltinTypeAliasLike(
program: ts.Program,
type: ts.Type,
predicate: (
subType: ts.Type & {
subType: {
aliasSymbol: ts.Symbol;
aliasTypeArguments: readonly ts.Type[];
},
} & ts.Type,
) => boolean,
): boolean {
return isBuiltinSymbolLikeRecurser(program, type, subtype => {
Expand All @@ -105,10 +105,10 @@ export function isBuiltinTypeAliasLike(
if (
isSymbolFromDefaultLibrary(program, aliasSymbol) &&
predicate(
subtype as ts.Type & {
subtype as {
aliasSymbol: ts.Symbol;
aliasTypeArguments: readonly ts.Type[];
},
} & ts.Type,
)
) {
return true;
Expand All @@ -121,7 +121,7 @@ export function isBuiltinTypeAliasLike(
export function isBuiltinSymbolLike(
program: ts.Program,
type: ts.Type,
symbolName: string | string[],
symbolName: string[] | string,
): boolean {
return isBuiltinSymbolLikeRecurser(program, type, subType => {
const symbol = subType.getSymbol();
Expand Down
2 changes: 1 addition & 1 deletion packages/type-utils/src/getContextualType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function getContextualType(
// is RHS of assignment
return checker.getTypeAtLocation(parent.left);
} else if (
![ts.SyntaxKind.TemplateSpan, ts.SyntaxKind.JsxExpression].includes(
![ts.SyntaxKind.JsxExpression, ts.SyntaxKind.TemplateSpan].includes(
parent.kind,
)
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/type-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export * from './isUnsafeAssignment';
export * from './predicates';
export * from './propertyTypes';
export * from './requiresQuoting';
export * from './TypeOrValueSpecifier';
export * from './typeFlagUtils';
export * from './TypeOrValueSpecifier';
export {
getDecorators,
getModifiers,
Expand Down
16 changes: 9 additions & 7 deletions packages/type-utils/src/isTypeReadonly.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ESLintUtils } from '@typescript-eslint/utils';
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema';

import { ESLintUtils } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';

import { getTypeOfPropertyOfType } from './propertyTypes';
import type { TypeOrValueSpecifier } from './TypeOrValueSpecifier';

import { getTypeOfPropertyOfType } from './propertyTypes';
import {
typeMatchesSomeSpecifier,
typeOrValueSpecifiersSchema,
Expand All @@ -20,27 +22,27 @@ const enum Readonlyness {
}

export interface ReadonlynessOptions {
readonly treatMethodsAsReadonly?: boolean;
readonly allow?: TypeOrValueSpecifier[];
readonly treatMethodsAsReadonly?: boolean;
}

export const readonlynessOptionsSchema = {
type: 'object',
additionalProperties: false,
properties: {
allow: typeOrValueSpecifiersSchema,
treatMethodsAsReadonly: {
type: 'boolean',
},
allow: typeOrValueSpecifiersSchema,
},
type: 'object',
} satisfies JSONSchema4;

export const readonlynessOptionsDefaults: ReadonlynessOptions = {
treatMethodsAsReadonly: false,
allow: [],
treatMethodsAsReadonly: false,
};

function hasSymbol(node: ts.Node): node is ts.Node & { symbol: ts.Symbol } {
function hasSymbol(node: ts.Node): node is { symbol: ts.Symbol } & ts.Node {
return Object.hasOwn(node, 'symbol');
}

Expand Down
11 changes: 6 additions & 5 deletions packages/type-utils/src/isUnsafeAssignment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { TSESTree } from '@typescript-eslint/utils';
import type * as ts from 'typescript';

import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import type * as ts from 'typescript';

import { isTypeAnyType, isTypeUnknownType } from './predicates';

Expand All @@ -20,7 +21,7 @@ export function isUnsafeAssignment(
receiver: ts.Type,
checker: ts.TypeChecker,
senderNode: TSESTree.Node | null,
): false | { sender: ts.Type; receiver: ts.Type } {
): { receiver: ts.Type; sender: ts.Type } | false {
return isUnsafeAssignmentWorker(
type,
receiver,
Expand All @@ -36,15 +37,15 @@ function isUnsafeAssignmentWorker(
checker: ts.TypeChecker,
senderNode: TSESTree.Node | null,
visited: Map<ts.Type, Set<ts.Type>>,
): false | { sender: ts.Type; receiver: ts.Type } {
): { receiver: ts.Type; sender: ts.Type } | false {
if (isTypeAnyType(type)) {
// Allow assignment of any ==> unknown.
if (isTypeUnknownType(receiver)) {
return false;
}

if (!isTypeAnyType(receiver)) {
return { sender: type, receiver };
return { receiver, sender: type };
}
}

Expand Down Expand Up @@ -107,7 +108,7 @@ function isUnsafeAssignmentWorker(
visited,
);
if (unsafe) {
return { sender: type, receiver };
return { receiver, sender: type };
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as tsutils from 'ts-api-utils';
import type * as ts from 'typescript';

import * as tsutils from 'ts-api-utils';

export function specifierNameMatches(
type: ts.Type,
names: string[] | string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';
import type * as ts from 'typescript';

import { getCanonicalFileName } from '@typescript-eslint/typescript-estree';
import type * as ts from 'typescript';
import path from 'node:path';

export function typeDeclaredInFile(
relativePath: string | undefined,
Expand Down
9 changes: 5 additions & 4 deletions packages/type-utils/tests/TypeOrValueSpecifier.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import path from 'node:path';
import type { TSESTree } from '@typescript-eslint/utils';

import { parseForESLint } from '@typescript-eslint/parser';
import type { TSESTree } from '@typescript-eslint/utils';
import Ajv from 'ajv';
import path from 'node:path';

import type { TypeOrValueSpecifier } from '../src/TypeOrValueSpecifier';

import type { TypeOrValueSpecifier } from '../src';
import { typeMatchesSpecifier, typeOrValueSpecifiersSchema } from '../src';

describe('TypeOrValueSpecifier', () => {
Expand Down Expand Up @@ -130,8 +131,8 @@ describe('TypeOrValueSpecifier', () => {
const rootDir = path.join(__dirname, 'fixtures');
const { ast, services } = parseForESLint(code, {
disallowAutomaticSingleRunInference: true,
project: './tsconfig.json',
filePath: path.join(rootDir, 'file.ts'),
project: './tsconfig.json',
tsconfigRootDir: rootDir,
});
const type = services
Expand Down
10 changes: 5 additions & 5 deletions packages/type-utils/tests/containsAllTypesByName.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'node:path';

import { parseForESLint } from '@typescript-eslint/parser';
import type { TSESTree } from '@typescript-eslint/typescript-estree';
import type * as ts from 'typescript';

import { parseForESLint } from '@typescript-eslint/parser';
import path from 'node:path';

import { containsAllTypesByName } from '../src';
import { expectToHaveParserServices } from './test-utils/expectToHaveParserServices';

Expand All @@ -13,8 +13,8 @@ describe('containsAllTypesByName', () => {
function getType(code: string): ts.Type {
const { ast, services } = parseForESLint(code, {
disallowAutomaticSingleRunInference: true,
project: './tsconfig.json',
filePath: path.join(rootDir, 'file.ts'),
project: './tsconfig.json',
tsconfigRootDir: rootDir,
});
expectToHaveParserServices(services);
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('containsAllTypesByName', () => {
const result = containsAllTypesByName(
type,
false,
new Set(['Promise', 'Object']),
new Set(['Object', 'Promise']),
matchAnyInstead,
);
expect(result).toBe(expected);
Expand Down
10 changes: 5 additions & 5 deletions packages/type-utils/tests/getTypeName.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'node:path';

import { parseForESLint } from '@typescript-eslint/parser';
import type { TSESTree } from '@typescript-eslint/typescript-estree';
import type * as ts from 'typescript';

import { parseForESLint } from '@typescript-eslint/parser';
import path from 'node:path';

import { getTypeName } from '../src';
import { expectToHaveParserServices } from './test-utils/expectToHaveParserServices';

Expand All @@ -13,8 +13,8 @@ describe('getTypeName', () => {

const { ast, services } = parseForESLint(code, {
disallowAutomaticSingleRunInference: true,
project: './tsconfig.json',
filePath: path.join(rootDir, 'file.ts'),
project: './tsconfig.json',
tsconfigRootDir: rootDir,
});
expectToHaveParserServices(services);
Expand All @@ -25,7 +25,7 @@ describe('getTypeName', () => {
}

function runTest(code: string, expected: string): void {
const { type, checker } = getTypes(code);
const { checker, type } = getTypes(code);
const result = getTypeName(checker, type);
expect(result).toBe(expected);
}
Expand Down
Loading
Loading