Skip to content

chore: enable eslint-plugin-perfectionist on scope-manager package #9848

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
4 changes: 3 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ export default tseslint.config(
},
},
{
files: ['eslint.config.{js,cjs,mjs}'],
files: ['eslint.config.{js,cjs,mjs}', 'knip.ts', 'packages/*/src/index.ts'],
rules: {
// requirement
'import/no-default-export': 'off',
Expand Down Expand Up @@ -598,6 +598,7 @@ export default tseslint.config(
extends: [perfectionistPlugin.configs['recommended-alphabetical']],
ignores: [
'packages/eslint-plugin/src/configs/*',
'packages/scope-manager/src/configs/*',
'packages/typescript-eslint/src/configs/*',
],
files: [
Expand All @@ -607,6 +608,7 @@ export default tseslint.config(
'packages/parser/{src,tests}/**/*.ts',
'packages/rule-schema-to-typescript-types/src/**/*.ts',
'packages/rule-tester/{src,tests,typings}/**/*.ts',
'packages/scope-manager/{src,tests}/**/*.ts',
'packages/types/{src,tools}/**/*.ts',
'packages/typescript-eslint/{src,tests}/**/*.ts',
'packages/utils/src/**/*.ts',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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-perfectionist": "^3.8.0",
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-regexp": "^2.6.0",
Expand Down
76 changes: 42 additions & 34 deletions packages/scope-manager/src/ScopeManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { SourceType, TSESTree } from '@typescript-eslint/types';

import { assert } from './assert';
import type { Scope } from './scope';
import type { Variable } from './variable';

import { assert } from './assert';
import {
BlockScope,
CatchScope,
Expand All @@ -23,7 +25,6 @@
} from './scope';
import { ClassFieldInitializerScope } from './scope/ClassFieldInitializerScope';
import { ClassStaticBlockScope } from './scope/ClassStaticBlockScope';
import type { Variable } from './variable';

interface ScopeManagerOptions {
globalReturn?: boolean;
Expand All @@ -35,30 +36,25 @@
* @see https://eslint.org/docs/latest/developer-guide/scope-manager-interface#scopemanager-interface
*/
class ScopeManager {
readonly #options: ScopeManagerOptions;

public currentScope: Scope | null;

public readonly declaredVariables: WeakMap<TSESTree.Node, Variable[]>;

/**
* The root scope
*/
public globalScope: GlobalScope | null;

public readonly nodeToScope: WeakMap<TSESTree.Node, Scope[]>;
readonly #options: ScopeManagerOptions;

/**
* All scopes
* @public
*/
public readonly scopes: Scope[];

public get variables(): Variable[] {
const variables = new Set<Variable>();
function recurse(scope: Scope): void {
scope.variables.forEach(v => variables.add(v));
scope.childScopes.forEach(recurse);
}
this.scopes.forEach(recurse);
return [...variables].sort((a, b) => a.$id - b.$id);
}

constructor(options: ScopeManagerOptions) {
this.scopes = [];
this.globalScope = null;
Expand All @@ -68,24 +64,34 @@
this.declaredVariables = new WeakMap();
}

public isGlobalReturn(): boolean {
return this.#options.globalReturn === true;
public isES6(): boolean {
return true;

Check warning on line 68 in packages/scope-manager/src/ScopeManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/scope-manager/src/ScopeManager.ts#L68

Added line #L68 was not covered by tests
}

public isModule(): boolean {
return this.#options.sourceType === 'module';
public isGlobalReturn(): boolean {
return this.#options.globalReturn === true;
}

public isImpliedStrict(): boolean {
return this.#options.impliedStrict === true;
}

public isModule(): boolean {
return this.#options.sourceType === 'module';
}

public isStrictModeSupported(): boolean {
return true;
}

public isES6(): boolean {
return true;
public get variables(): Variable[] {
const variables = new Set<Variable>();
function recurse(scope: Scope): void {
scope.variables.forEach(v => variables.add(v));
scope.childScopes.forEach(recurse);
}
this.scopes.forEach(recurse);
return [...variables].sort((a, b) => a.$id - b.$id);
}

/**
Expand Down Expand Up @@ -141,16 +147,6 @@
return scopes.find(predicate) ?? null;
}

protected nestScope<T extends Scope>(scope: T): T;
protected nestScope(scope: Scope): Scope {
if (scope instanceof GlobalScope) {
assert(this.currentScope == null);
this.globalScope = scope;
}
this.currentScope = scope;
return scope;
}

public nestBlockScope(node: BlockScope['block']): BlockScope {
assert(this.currentScope);
return this.nestScope(new BlockScope(this, this.currentScope, node));
Expand All @@ -161,11 +157,6 @@
return this.nestScope(new CatchScope(this, this.currentScope, node));
}

public nestClassScope(node: ClassScope['block']): ClassScope {
assert(this.currentScope);
return this.nestScope(new ClassScope(this, this.currentScope, node));
}

public nestClassFieldInitializerScope(
node: ClassFieldInitializerScope['block'],
): ClassFieldInitializerScope {
Expand All @@ -175,6 +166,11 @@
);
}

public nestClassScope(node: ClassScope['block']): ClassScope {
assert(this.currentScope);
return this.nestScope(new ClassScope(this, this.currentScope, node));
}

public nestClassStaticBlockScope(
node: ClassStaticBlockScope['block'],
): ClassStaticBlockScope {
Expand Down Expand Up @@ -262,6 +258,18 @@
assert(this.currentScope);
return this.nestScope(new WithScope(this, this.currentScope, node));
}

// Scope helpers

protected nestScope<T extends Scope>(scope: T): T;
protected nestScope(scope: Scope): Scope {
if (scope instanceof GlobalScope) {
assert(this.currentScope == null);
this.globalScope = scope;
}
this.currentScope = scope;
return scope;
}
}

export { ScopeManager };
14 changes: 8 additions & 6 deletions packages/scope-manager/src/analyze.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Lib, SourceType, TSESTree } from '@typescript-eslint/types';

import { visitorKeys } from '@typescript-eslint/visitor-keys';

import type { ReferencerOptions } from './referencer';

import { Referencer } from './referencer';
import { ScopeManager } from './ScopeManager';

Expand Down Expand Up @@ -66,13 +68,13 @@ interface AnalyzeOptions {

const DEFAULT_OPTIONS: Required<AnalyzeOptions> = {
childVisitorKeys: visitorKeys,
emitDecoratorMetadata: false,
globalReturn: false,
impliedStrict: false,
jsxPragma: 'React',
jsxFragmentName: null,
jsxPragma: 'React',
lib: ['es2018'],
sourceType: 'script',
emitDecoratorMetadata: false,
};

/**
Expand All @@ -85,18 +87,18 @@ function analyze(
const options: Required<AnalyzeOptions> = {
childVisitorKeys:
providedOptions?.childVisitorKeys ?? DEFAULT_OPTIONS.childVisitorKeys,
emitDecoratorMetadata: false,
globalReturn: providedOptions?.globalReturn ?? DEFAULT_OPTIONS.globalReturn,
impliedStrict:
providedOptions?.impliedStrict ?? DEFAULT_OPTIONS.impliedStrict,
jsxFragmentName:
providedOptions?.jsxFragmentName ?? DEFAULT_OPTIONS.jsxFragmentName,
jsxPragma:
providedOptions?.jsxPragma === undefined
? DEFAULT_OPTIONS.jsxPragma
: providedOptions.jsxPragma,
jsxFragmentName:
providedOptions?.jsxFragmentName ?? DEFAULT_OPTIONS.jsxFragmentName,
sourceType: providedOptions?.sourceType ?? DEFAULT_OPTIONS.sourceType,
lib: providedOptions?.lib ?? ['esnext'],
emitDecoratorMetadata: false,
sourceType: providedOptions?.sourceType ?? DEFAULT_OPTIONS.sourceType,
};

// ensure the option is lower cased
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class CatchClauseDefinition extends DefinitionBase<
null,
TSESTree.BindingName
> {
public readonly isTypeDefinition = false;
public readonly isVariableDefinition = true;

constructor(name: TSESTree.BindingName, node: CatchClauseDefinition['node']) {
super(DefinitionType.CatchClause, name, node, null);
}

public readonly isTypeDefinition = false;
public readonly isVariableDefinition = true;
}

export { CatchClauseDefinition };
6 changes: 3 additions & 3 deletions packages/scope-manager/src/definition/ClassNameDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class ClassNameDefinition extends DefinitionBase<
null,
TSESTree.Identifier
> {
public readonly isTypeDefinition = true;
public readonly isVariableDefinition = true;

constructor(name: TSESTree.Identifier, node: ClassNameDefinition['node']) {
super(DefinitionType.ClassName, name, node, null);
}

public readonly isTypeDefinition = true;
public readonly isVariableDefinition = true;
}

export { ClassNameDefinition };
3 changes: 2 additions & 1 deletion packages/scope-manager/src/definition/DefinitionBase.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { TSESTree } from '@typescript-eslint/types';

import { createIdGenerator } from '../ID';
import type { DefinitionType } from './DefinitionType';

import { createIdGenerator } from '../ID';

const generator = createIdGenerator();

abstract class DefinitionBase<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class FunctionNameDefinition extends DefinitionBase<
null,
TSESTree.Identifier
> {
public readonly isTypeDefinition = false;
public readonly isVariableDefinition = true;

constructor(name: TSESTree.Identifier, node: FunctionNameDefinition['node']) {
super(DefinitionType.FunctionName, name, node, null);
}

public readonly isTypeDefinition = false;
public readonly isVariableDefinition = true;
}

export { FunctionNameDefinition };
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class ImplicitGlobalVariableDefinition extends DefinitionBase<
null,
TSESTree.BindingName
> {
public readonly isTypeDefinition = false;
public readonly isVariableDefinition = true;

constructor(
name: TSESTree.BindingName,
node: ImplicitGlobalVariableDefinition['node'],
) {
super(DefinitionType.ImplicitGlobalVariable, name, node, null);
}

public readonly isTypeDefinition = false;
public readonly isVariableDefinition = true;
}

export { ImplicitGlobalVariableDefinition };
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class ImportBindingDefinition extends DefinitionBase<
TSESTree.ImportDeclaration | TSESTree.TSImportEqualsDeclaration,
TSESTree.Identifier
> {
public readonly isTypeDefinition = true;
public readonly isVariableDefinition = true;

constructor(
name: TSESTree.Identifier,
node: TSESTree.TSImportEqualsDeclaration,
Expand All @@ -32,9 +35,6 @@ class ImportBindingDefinition extends DefinitionBase<
) {
super(DefinitionType.ImportBinding, name, node, decl);
}

public readonly isTypeDefinition = true;
public readonly isVariableDefinition = true;
}

export { ImportBindingDefinition };
6 changes: 3 additions & 3 deletions packages/scope-manager/src/definition/ParameterDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class ParameterDefinition extends DefinitionBase<
/**
* Whether the parameter definition is a part of a rest parameter.
*/
public readonly isTypeDefinition = false;
public readonly isVariableDefinition = true;
public readonly rest: boolean;

constructor(
name: TSESTree.BindingName,
node: ParameterDefinition['node'],
Expand All @@ -30,9 +33,6 @@ class ParameterDefinition extends DefinitionBase<
super(DefinitionType.Parameter, name, node, null);
this.rest = rest;
}

public readonly isTypeDefinition = false;
public readonly isVariableDefinition = true;
}

export { ParameterDefinition };
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class TSEnumMemberDefinition extends DefinitionBase<
null,
TSESTree.Identifier | TSESTree.StringLiteral
> {
public readonly isTypeDefinition = true;
public readonly isVariableDefinition = true;

constructor(
name: TSESTree.Identifier | TSESTree.StringLiteral,
node: TSEnumMemberDefinition['node'],
) {
super(DefinitionType.TSEnumMember, name, node, null);
}

public readonly isTypeDefinition = true;
public readonly isVariableDefinition = true;
}

export { TSEnumMemberDefinition };
6 changes: 3 additions & 3 deletions packages/scope-manager/src/definition/TSEnumNameDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class TSEnumNameDefinition extends DefinitionBase<
null,
TSESTree.Identifier
> {
public readonly isTypeDefinition = true;
public readonly isVariableDefinition = true;

constructor(name: TSESTree.Identifier, node: TSEnumNameDefinition['node']) {
super(DefinitionType.TSEnumName, name, node, null);
}

public readonly isTypeDefinition = true;
public readonly isVariableDefinition = true;
}

export { TSEnumNameDefinition };
Loading
Loading