Skip to content

chore: enabled eslint-plugin-perfectionist on utils #9698

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
32 changes: 32 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import importPlugin from 'eslint-plugin-import';
import jestPlugin from 'eslint-plugin-jest';
import jsdocPlugin from 'eslint-plugin-jsdoc';
import jsxA11yPlugin from 'eslint-plugin-jsx-a11y';
import perfectionistPlugin from 'eslint-plugin-perfectionist';
import reactPlugin from 'eslint-plugin-react';
import reactHooksPlugin from 'eslint-plugin-react-hooks';
import simpleImportSortPlugin from 'eslint-plugin-simple-import-sort';
Expand Down Expand Up @@ -575,4 +576,35 @@ export default tseslint.config(
'import/no-default-export': 'off',
},
},
{
extends: [perfectionistPlugin.configs['recommended-alphabetical']],
files: ['packages/utils/src/**/*.ts'],
rules: {
'perfectionist/sort-classes': [
'error',
{
order: 'asc',
partitionByComment: true,
type: 'natural',
},
],
'perfectionist/sort-objects': [
'error',
{
order: 'asc',
partitionByComment: true,
type: 'natural',
},
],
'perfectionist/sort-union-types': [
'error',
{
order: 'asc',
groups: ['unknown', 'keyword', 'nullish'],
type: 'natural',
},
],
'simple-import-sort/imports': 'off',
},
},
);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"eslint-plugin-jest": "^27.9.0",
"eslint-plugin-jsdoc": "^47.0.2",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-perfectionist": "^3.2.0",
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
Expand Down
30 changes: 15 additions & 15 deletions packages/utils/src/ast-utils/eslint-utils/PatternMatcher.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import * as eslintUtils from '@eslint-community/eslint-utils';

interface PatternMatcher {
/**
* Iterate all matched parts in a given string.
*
* @see {@link https://eslint-community.github.io/eslint-utils/api/ast-utils.html#matcher-execall}
*/
execAll(str: string): IterableIterator<RegExpExecArray>;

/**
* Check whether this pattern matches a given string or not.
*
* @see {@link https://eslint-community.github.io/eslint-utils/api/ast-utils.html#matcher-test}
*/
test(str: string): boolean;

/**
* Replace all matched parts by a given replacer.
*
Expand All @@ -39,8 +25,22 @@ interface PatternMatcher {
*/
[Symbol.replace](
str: string,
replacer: string | ((...strs: string[]) => string),
replacer: ((...strs: string[]) => string) | string,
): string;

/**
* Iterate all matched parts in a given string.
*
* @see {@link https://eslint-community.github.io/eslint-utils/api/ast-utils.html#matcher-execall}
*/
execAll(str: string): IterableIterator<RegExpExecArray>;

/**
* Check whether this pattern matches a given string or not.
*
* @see {@link https://eslint-community.github.io/eslint-utils/api/ast-utils.html#matcher-test}
*/
test(str: string): boolean;
}

/**
Expand Down
39 changes: 20 additions & 19 deletions packages/utils/src/ast-utils/eslint-utils/ReferenceTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,56 @@ const ReferenceTrackerESM: unique symbol = eslintUtils.ReferenceTracker.ESM;
interface ReferenceTracker {
/**
* Iterate the references that the given `traceMap` determined.
* This method starts to search from global variables.
* This method starts to search from `require()` expression.
*
* @see {@link https://eslint-community.github.io/eslint-utils/api/scope-utils.html#tracker-iterateglobalreferences}
* @see {@link https://eslint-community.github.io/eslint-utils/api/scope-utils.html#tracker-iteratecjsreferences}
*/
iterateGlobalReferences<T>(
iterateCjsReferences<T>(
traceMap: ReferenceTracker.TraceMap<T>,
): IterableIterator<ReferenceTracker.FoundReference<T>>;

/**
* Iterate the references that the given `traceMap` determined.
* This method starts to search from `require()` expression.
* This method starts to search from `import`/`export` declarations.
*
* @see {@link https://eslint-community.github.io/eslint-utils/api/scope-utils.html#tracker-iteratecjsreferences}
* @see {@link https://eslint-community.github.io/eslint-utils/api/scope-utils.html#tracker-iterateesmreferences}
*/
iterateCjsReferences<T>(
iterateEsmReferences<T>(
traceMap: ReferenceTracker.TraceMap<T>,
): IterableIterator<ReferenceTracker.FoundReference<T>>;

/**
* Iterate the references that the given `traceMap` determined.
* This method starts to search from `import`/`export` declarations.
* This method starts to search from global variables.
*
* @see {@link https://eslint-community.github.io/eslint-utils/api/scope-utils.html#tracker-iterateesmreferences}
* @see {@link https://eslint-community.github.io/eslint-utils/api/scope-utils.html#tracker-iterateglobalreferences}
*/
iterateEsmReferences<T>(
iterateGlobalReferences<T>(
traceMap: ReferenceTracker.TraceMap<T>,
): IterableIterator<ReferenceTracker.FoundReference<T>>;
}
interface ReferenceTrackerStatic {
readonly CALL: typeof ReferenceTrackerCALL;
readonly CONSTRUCT: typeof ReferenceTrackerCONSTRUCT;
readonly ESM: typeof ReferenceTrackerESM;

new (
globalScope: TSESLint.Scope.Scope,
options?: {
/**
* The name list of Global Object. Optional. Default is `["global", "globalThis", "self", "window"]`.
*/
globalObjectNames?: readonly string[];
/**
* The mode which determines how the `tracker.iterateEsmReferences()` method scans CommonJS modules.
* If this is `"strict"`, the method binds CommonJS modules to the default export. Otherwise, the method binds
* CommonJS modules to both the default export and named exports. Optional. Default is `"strict"`.
*/
mode?: 'legacy' | 'strict';
/**
* The name list of Global Object. Optional. Default is `["global", "globalThis", "self", "window"]`.
*/
globalObjectNames?: readonly string[];
},
): ReferenceTracker;

readonly READ: typeof ReferenceTrackerREAD;
readonly CALL: typeof ReferenceTrackerCALL;
readonly CONSTRUCT: typeof ReferenceTrackerCONSTRUCT;
readonly ESM: typeof ReferenceTrackerESM;
}

namespace ReferenceTracker {
Expand All @@ -73,18 +74,18 @@ namespace ReferenceTracker {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type TraceMap<T = any> = Record<string, TraceMapElement<T>>;
export interface TraceMapElement<T> {
[ReferenceTrackerREAD]?: T;
[key: string]: TraceMapElement<T>;
[ReferenceTrackerCALL]?: T;
[ReferenceTrackerCONSTRUCT]?: T;
[ReferenceTrackerESM]?: true;
[key: string]: TraceMapElement<T>;
[ReferenceTrackerREAD]?: T;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface FoundReference<T = any> {
info: T;
node: TSESTree.Node;
path: readonly string[];
type: ReferenceType;
info: T;
}
}

Expand Down
11 changes: 6 additions & 5 deletions packages/utils/src/ast-utils/eslint-utils/astUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ const hasSideEffect = eslintUtils.hasSideEffect as (
) => boolean;

const isParenthesized = eslintUtils.isParenthesized as {
(
times: number,
node: TSESTree.Node,
sourceCode: TSESLint.SourceCode,
): boolean;

/**
* Check whether a given node is parenthesized or not.
* This function detects it correctly even if it's parenthesized by specific syntax.
Expand All @@ -115,11 +121,6 @@ const isParenthesized = eslintUtils.isParenthesized as {
* For example, `isParenthesized(2, node, sourceCode)` returns true for `((foo))`, but not for `(foo)`.
*/
(node: TSESTree.Node, sourceCode: TSESLint.SourceCode): boolean;
(
times: number,
node: TSESTree.Node,
sourceCode: TSESLint.SourceCode,
): boolean;
};

export {
Expand Down
5 changes: 3 additions & 2 deletions packages/utils/src/ast-utils/eslint-utils/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ type IsNotSpecificTokenFunction<SpecificToken extends TSESTree.Token> = (
token: TSESTree.Token,
) => token is Exclude<TSESTree.Token, SpecificToken>;

type PunctuatorTokenWithValue<Value extends string> =
TSESTree.PunctuatorToken & { value: Value };
type PunctuatorTokenWithValue<Value extends string> = {
value: Value;
} & TSESTree.PunctuatorToken;
type IsPunctuatorTokenWithValueFunction<Value extends string> =
IsSpecificTokenFunction<PunctuatorTokenWithValue<Value>>;
type IsNotPunctuatorTokenWithValueFunction<Value extends string> =
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/ast-utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const isTokenOfTypeWithConditions = <
// This is technically unsafe, but we find it useful to extract out the type
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
ExtractedToken extends Extract<TSESTree.Token, { type: TokenType }>,
Conditions extends Partial<TSESTree.Token & { type: TokenType }>,
Conditions extends Partial<{ type: TokenType } & TSESTree.Token>,
>(
tokenType: TokenType,
conditions: Conditions,
Expand Down
7 changes: 4 additions & 3 deletions packages/utils/src/ast-utils/predicates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { TSESTree } from '../ts-estree';

import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '../ts-estree';
import {
isNodeOfType,
Expand Down Expand Up @@ -164,20 +165,20 @@ const isLoop = isNodeOfTypes([
export {
isAwaitExpression,
isAwaitKeyword,
isConstructor,
isClassOrTypeElement,
isConstructor,
isFunction,
isFunctionOrFunctionType,
isFunctionType,
isIdentifier,
isImportKeyword,
isLoop,
isLogicalOrOperator,
isLoop,
isNonNullAssertionPunctuator,
isNotNonNullAssertionPunctuator,
isNotOptionalChainPunctuator,
isOptionalChainPunctuator,
isOptionalCallExpression,
isOptionalChainPunctuator,
isSetter,
isTSConstructorType,
isTSFunctionType,
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/eslint-utils/InferTypesFromRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ type InferMessageIdsTypeFromRule<T> =
? MessageIds
: unknown;

export { InferOptionsTypeFromRule, InferMessageIdsTypeFromRule };
export { InferMessageIdsTypeFromRule, InferOptionsTypeFromRule };
9 changes: 5 additions & 4 deletions packages/utils/src/eslint-utils/RuleCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
RuleMetaDataDocs,
RuleModule,
} from '../ts-eslint/Rule';

import { applyDefault } from './applyDefault';

export type { RuleListener, RuleModule };
Expand All @@ -15,9 +16,9 @@ export type NamedCreateRuleMetaDocs = Omit<RuleMetaDataDocs, 'url'>;
export type NamedCreateRuleMeta<
MessageIds extends string,
PluginDocs = unknown,
> = Omit<RuleMetaData<MessageIds, PluginDocs>, 'docs'> & {
docs: RuleMetaDataDocs & PluginDocs;
};
> = {
docs: PluginDocs & RuleMetaDataDocs;
} & Omit<RuleMetaData<MessageIds, PluginDocs>, 'docs'>;

export interface RuleCreateAndOptions<
Options extends readonly unknown[],
Expand Down Expand Up @@ -62,8 +63,8 @@ export function RuleCreator<PluginDocs = unknown>(
Options extends readonly unknown[],
MessageIds extends string,
>({
name,
meta,
name,
...rule
}: Readonly<
RuleWithMetaAndName<Options, MessageIds, PluginDocs>
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/eslint-utils/getParserServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
ParserServices,
ParserServicesWithTypeInformation,
} from '../ts-estree';

import { parserSeemsToBeTSESLint } from './parserSeemsToBeTSESLint';

const ERROR_MESSAGE_REQUIRES_PARSER_SERVICES =
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/eslint-utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './applyDefault';
export * from './deepMerge';
export * from './getParserServices';
export * from './InferTypesFromRule';
export * from './RuleCreator';
export * from './deepMerge';
export * from './nullThrows';
export * from './RuleCreator';
Loading
Loading