Skip to content

chore: enable eslint-plugin-perfectionist on typescript-estree package #9852

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
18 changes: 17 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ export default tseslint.config(
'packages/scope-manager/{src,tests}/**/*.ts',
'packages/types/{src,tools}/**/*.ts',
'packages/typescript-eslint/{src,tests}/**/*.ts',
'packages/typescript-estree/{src,tests,typings}/**/*.ts',
'packages/utils/src/**/*.ts',
'packages/visitor-keys/src/**/*.ts',
'packages/website*/src/**/*.ts',
Expand All @@ -625,7 +626,7 @@ export default tseslint.config(
'perfectionist/sort-union-types': [
'error',
{
groups: ['unknown', 'keyword', 'nullish'],
groups: ['keyword', 'unknown', 'nullish'],
type: 'natural',
},
],
Expand Down Expand Up @@ -682,4 +683,19 @@ export default tseslint.config(
],
},
},
{
files: ['packages/typescript-estree/src/**/*.ts'],
rules: {
'perfectionist/sort-objects': [
'error',
{
customGroups: {
first: ['type'],
second: ['loc', 'range'],
},
groups: ['first', 'second'],
},
],
},
},
);
2 changes: 1 addition & 1 deletion packages/ast-spec/src/base/LiteralBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import type { BaseNode } from './BaseNode';
export interface LiteralBase extends BaseNode {
type: AST_NODE_TYPES.Literal;
raw: string;
value: RegExp | bigint | boolean | number | string | null;
value: bigint | boolean | number | string | RegExp | null;
}
4 changes: 2 additions & 2 deletions packages/ast-spec/src/type/TSMappedType/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export interface TSMappedType extends BaseNode {
constraint: TypeNode;
key: Identifier;
nameType: TypeNode | null;
optional: '+' | '-' | boolean | undefined;
readonly: '+' | '-' | boolean | undefined;
optional: boolean | '+' | '-' | undefined;
readonly: boolean | '+' | '-' | undefined;
typeAnnotation: TypeNode | undefined;
/** @deprecated Use {@link `constraint`} and {@link `key`} instead. */
typeParameter: TSTypeParameter;
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/ban-ts-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { AST_TOKEN_TYPES } from '@typescript-eslint/utils';
import { createRule, getStringLength, nullThrows } from '../util';

type DirectiveConfig =
| boolean
| 'allow-with-description'
| { descriptionFormat: string }
| boolean;
| { descriptionFormat: string };

interface Options {
minimumDescriptionLength?: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/class-methods-use-this.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Options = [
{
enforceForClassFields?: boolean;
exceptMethods?: string[];
ignoreClassesThatImplementAnInterface?: 'public-fields' | boolean;
ignoreClassesThatImplementAnInterface?: boolean | 'public-fields';
ignoreOverrideMethods?: boolean;
},
];
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/member-ordering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ export default createRule<Options, MessageIds>({

// Standardize config
let order: Order | undefined;
let memberTypes: MemberType[] | string | undefined;
let memberTypes: string | MemberType[] | undefined;
let optionalityOrder: OptionalityOrder | undefined;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface MatchRegex {

interface Selector {
custom?: MatchRegex;
filter?: MatchRegex | string;
filter?: string | MatchRegex;
// format options
format: PredefinedFormatsString[] | null;
leadingUnderscore?: UnderscoreOptionsString;
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-invalid-void-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createRule } from '../util';

interface Options {
allowAsThisParameter?: boolean;
allowInGenericTypeArguments?: [string, ...string[]] | boolean;
allowInGenericTypeArguments?: boolean | [string, ...string[]];
}

type MessageIds =
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/no-misused-promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Options = [
{
checksConditionals?: boolean;
checksSpreads?: boolean;
checksVoidReturn?: ChecksVoidReturnOptions | boolean;
checksVoidReturn?: boolean | ChecksVoidReturnOptions;
},
];

Expand All @@ -43,7 +43,7 @@ type MessageId =
| 'voidReturnVariable';

function parseChecksVoidReturn(
checksVoidReturn: ChecksVoidReturnOptions | boolean | undefined,
checksVoidReturn: boolean | ChecksVoidReturnOptions | undefined,
): ChecksVoidReturnOptions | false {
switch (checksVoidReturn) {
case false:
Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin/src/rules/no-restricted-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { createRule, objectReduceKey } from '../util';

type Types = Record<
string,
| boolean
| string
| {
fixWith?: string;
message: string;
suggest?: readonly string[];
}
| boolean
| string
| null
>;

Expand All @@ -37,7 +37,7 @@ function stringifyNode(
}

function getCustomMessage(
bannedType: { fixWith?: string; message?: string } | true | string | null,
bannedType: string | { fixWith?: string; message?: string } | true | null,
): string {
if (!bannedType || bannedType === true) {
return '';
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-use-before-define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const SENTINEL_TYPE =
/**
* Parses a given value as options.
*/
function parseOptions(options: Config | string | null): Required<Config> {
function parseOptions(options: string | Config | null): Required<Config> {
let functions = true;
let classes = true;
let enums = true;
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/util/collectUnusedVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class UnusedVarsVisitor extends Visitor {
private markVariableAsUsed(name: string, parent: TSESTree.Node): void;

private markVariableAsUsed(
variableOrIdentifierOrName: ScopeVariable | TSESTree.Identifier | string,
variableOrIdentifierOrName: string | ScopeVariable | TSESTree.Identifier,
parent?: TSESTree.Node,
): void {
if (
Expand Down
14 changes: 7 additions & 7 deletions packages/eslint-plugin/tests/configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const EXTENSION_RULES = Object.entries(rules)
);

function filterRules(
values: Record<string, unknown[] | string>,
): [string, unknown[] | string][] {
values: Record<string, string | unknown[]>,
): [string, string | unknown[]][] {
return Object.entries(values).filter(([name]) =>
name.startsWith(RULE_NAME_PREFIX),
);
Expand Down Expand Up @@ -84,7 +84,7 @@ function filterAndMapRuleConfigs({
}

function itHasBaseRulesOverriden(
unfilteredConfigRules: Record<string, unknown[] | string>,
unfilteredConfigRules: Record<string, string | unknown[]>,
): void {
it('has the base rules overriden by the appropriate extension rules', () => {
const ruleNames = new Set(Object.keys(unfilteredConfigRules));
Expand Down Expand Up @@ -196,7 +196,7 @@ describe('recommended-type-checked-only.ts', () => {
});

describe('strict.ts', () => {
const unfilteredConfigRules: Record<string, unknown[] | string> =
const unfilteredConfigRules: Record<string, string | unknown[]> =
plugin.configs.strict.rules;

it('contains all strict rules, excluding type checked ones', () => {
Expand All @@ -217,7 +217,7 @@ describe('strict.ts', () => {
});

describe('strict-type-checked.ts', () => {
const unfilteredConfigRules: Record<string, unknown[] | string> =
const unfilteredConfigRules: Record<string, string | unknown[]> =
plugin.configs['strict-type-checked'].rules;

it('contains all strict rules', () => {
Expand All @@ -236,7 +236,7 @@ describe('strict-type-checked.ts', () => {
});

describe('strict-type-checked-only.ts', () => {
const unfilteredConfigRules: Record<string, unknown[] | string> =
const unfilteredConfigRules: Record<string, string | unknown[]> =
plugin.configs['strict-type-checked-only'].rules;

it('contains only type-checked strict rules', () => {
Expand All @@ -257,7 +257,7 @@ describe('strict-type-checked-only.ts', () => {
});

describe('stylistic.ts', () => {
const unfilteredConfigRules: Record<string, unknown[] | string> =
const unfilteredConfigRules: Record<string, string | unknown[]> =
plugin.configs.stylistic.rules;

it('contains all stylistic rules, excluding deprecated or type checked ones', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/tests/docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ describe('Validating rule docs', () => {

function lintCodeBlock(
token: mdast.Code,
shouldContainLintErrors: 'skip-check' | boolean,
shouldContainLintErrors: boolean | 'skip-check',
): void {
const lang = token.lang?.trim();
if (!lang || !/^tsx?\b/i.test(lang)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ const nullishTypes = ['null', 'undefined', 'null | undefined'];
const ignorablePrimitiveTypes = ['string', 'number', 'boolean', 'bigint'];

function typeValidTest(
cb: (type: string) => ValidTestCase<Options> | string,
): (ValidTestCase<Options> | string)[] {
cb: (type: string) => string | ValidTestCase<Options>,
): (string | ValidTestCase<Options>)[] {
return types.map(type => cb(type));
}
function nullishTypeTest<
T extends
| string
| InvalidTestCase<MessageIds, Options>
| ValidTestCase<Options>
| string,
| ValidTestCase<Options>,
>(cb: (nullish: string, type: string) => T): T[] {
return nullishTypes.flatMap(nullish => types.map(type => cb(nullish, type)));
}
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/typings/eslint-rules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ declare module 'eslint/lib/rules/no-restricted-globals' {
const rule: TSESLint.RuleModule<
'customMessage' | 'defaultMessage',
(
| string
| {
message?: string;
name: string;
}
| string
)[],
unknown,
{
Expand Down Expand Up @@ -539,14 +539,14 @@ declare module 'eslint/lib/rules/no-restricted-imports' {

namespace rule {
export type ArrayOfStringOrObject = (
| string
| {
// extended
allowTypeImports?: boolean;
importNames?: string[];
message?: string;
name: string;
}
| string
)[];
export type ArrayOfStringOrObjectPatterns =
| {
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ function getLib(compilerOptions: ts.CompilerOptions): Lib[] {
}

function parse(
code: ts.SourceFile | string,
code: string | ts.SourceFile,
options?: ParserOptions,
): ParseForESLintResult['ast'] {
return parseForESLint(code, options).ast;
}

function parseForESLint(
code: ts.SourceFile | string,
code: string | ts.SourceFile,
parserOptions?: ParserOptions | null,
): ParseForESLintResult {
if (!parserOptions || typeof parserOptions !== 'object') {
Expand Down
10 changes: 5 additions & 5 deletions packages/rule-tester/src/RuleTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export class RuleTester extends TestFramework {
* Adds the `only` property to a test to run it in isolation.
*/
static only<Options extends readonly unknown[]>(
item: ValidTestCase<Options> | string,
item: string | ValidTestCase<Options>,
): ValidTestCase<Options>;
/**
* Adds the `only` property to a test to run it in isolation.
Expand All @@ -268,9 +268,9 @@ export class RuleTester extends TestFramework {
): InvalidTestCase<MessageIds, Options>;
static only<MessageIds extends string, Options extends readonly unknown[]>(
item:
| string
| InvalidTestCase<MessageIds, Options>
| ValidTestCase<Options>
| string,
| ValidTestCase<Options>,
): InvalidTestCase<MessageIds, Options> | ValidTestCase<Options> {
if (typeof item === 'string') {
return { code: item, only: true };
Expand Down Expand Up @@ -833,7 +833,7 @@ export class RuleTester extends TestFramework {
>(
ruleName: string,
rule: RuleModule<MessageIds, Options>,
itemIn: ValidTestCase<Options> | string,
itemIn: string | ValidTestCase<Options>,
seenValidTestCases: Set<string>,
): void {
const item: ValidTestCase<Options> =
Expand Down Expand Up @@ -1374,7 +1374,7 @@ function checkDuplicateTestCase(
* value is a regular expression, it is checked against the actual
* value.
*/
function assertMessageMatches(actual: string, expected: RegExp | string): void {
function assertMessageMatches(actual: string, expected: string | RegExp): void {
if (expected instanceof RegExp) {
// assert.js doesn't have a built-in RegExp match function
assert.ok(
Expand Down
2 changes: 1 addition & 1 deletion packages/rule-tester/src/types/DependencyConstraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface RangeOptions {
}

export interface SemverVersionConstraint {
readonly options?: RangeOptions | boolean;
readonly options?: boolean | RangeOptions;
readonly range: string;
}
export type AtLeastVersionConstraint =
Expand Down
2 changes: 1 addition & 1 deletion packages/rule-tester/src/types/InvalidTestCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ export interface InvalidTestCase<
/**
* The expected code after autofixes are applied. If set to `null`, the test runner will assert that no autofix is suggested.
*/
readonly output?: string[] | string | null;
readonly output?: string | string[] | null;
}
2 changes: 1 addition & 1 deletion packages/rule-tester/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface RunTests<
> {
readonly invalid: readonly InvalidTestCase<MessageIds, Options>[];
// RuleTester.run also accepts strings for valid cases
readonly valid: readonly (ValidTestCase<Options> | string)[];
readonly valid: readonly (string | ValidTestCase<Options>)[];
}

export interface NormalizedRunTests<
Expand Down
8 changes: 4 additions & 4 deletions packages/rule-tester/src/utils/flat-config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { normalizeSeverityToNumber } from './severity';
type PluginMemberName = `${string}/${string}`;

interface ObjectPropertySchema<T = unknown> {
merge: ((a: T, b: T) => T) | string;
validate: ((value: unknown) => asserts value is T) | string;
merge: string | ((a: T, b: T) => T);
validate: string | ((value: unknown) => asserts value is T);
}

const ruleSeverities = new Map<SharedConfig.RuleLevel, SharedConfig.Severity>([
Expand Down Expand Up @@ -279,8 +279,8 @@ const ALLOWED_SEVERITIES = new Set([0, 1, 2, 'error', 'off', 'warn']);
const disableDirectiveSeveritySchema: ObjectPropertySchema<SharedConfig.RuleLevel> =
{
merge(
first: SharedConfig.RuleLevel | boolean | undefined,
second: SharedConfig.RuleLevel | boolean | undefined,
first: boolean | SharedConfig.RuleLevel | undefined,
second: boolean | SharedConfig.RuleLevel | undefined,
): SharedConfig.RuleLevel {
const value = second ?? first;

Expand Down
2 changes: 1 addition & 1 deletion packages/scope-manager/src/scope/ScopeBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ abstract class ScopeBase<
* References in default parameters isn't resolved to variables which are in their function body.
*/
protected defineVariable(
nameOrVariable: Variable | string,
nameOrVariable: string | Variable,
set: Map<string, Variable>,
variables: Variable[],
node: TSESTree.Identifier | null,
Expand Down
2 changes: 1 addition & 1 deletion packages/scope-manager/tests/test-utils/getSpecificNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function getSpecificNode<
function getSpecificNode(
ast: TSESTree.Node,
selector: AST_NODE_TYPES,
cb?: (node: TSESTree.Node) => TSESTree.Node | boolean | null | undefined,
cb?: (node: TSESTree.Node) => boolean | TSESTree.Node | null | undefined,
): TSESTree.Node {
let node: TSESTree.Node | null | undefined = null;
simpleTraverse(
Expand Down
Loading
Loading