Skip to content

Commit

Permalink
chore: drop T from internal types (#8521)
Browse files Browse the repository at this point in the history
* Add test cases per #8380

* Revert "Add test cases per #8380"

This reverts commit f3b9d24.

* Remove t prefix from DefinitionBase, ScopeBase, and getSpecificNode fiiles in scope manager

* remove t prefixed internal variables from RuleModule

* fix internal types for eslint-plugin/src/util/misc

* Addresss other files where T prefixes are used for internal types

* Remove t prefix from internal types

* Revert edit to rule.d.ts file in eslint-plugin

* remove t prefixess from rule create function

---------

Co-authored-by: auvred <61150013+auvred@users.noreply.github.com>
  • Loading branch information
peanutenthusiast and auvred committed Feb 23, 2024
1 parent 60c1cd3 commit fa67955
Show file tree
Hide file tree
Showing 24 changed files with 279 additions and 283 deletions.
18 changes: 9 additions & 9 deletions packages/eslint-plugin/src/util/collectUnusedVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
} from '@typescript-eslint/utils';

class UnusedVarsVisitor<
TMessageIds extends string,
TOptions extends readonly unknown[],
MessageIds extends string,
Options extends readonly unknown[],
> extends Visitor {
private static readonly RESULTS_CACHE = new WeakMap<
TSESTree.Program,
Expand All @@ -23,7 +23,7 @@ class UnusedVarsVisitor<
readonly #scopeManager: TSESLint.Scope.ScopeManager;
// readonly #unusedVariables = new Set<TSESLint.Scope.Variable>();

private constructor(context: TSESLint.RuleContext<TMessageIds, TOptions>) {
private constructor(context: TSESLint.RuleContext<MessageIds, Options>) {
super({
visitChildrenEvenIfSelectorExists: true,
});
Expand All @@ -35,10 +35,10 @@ class UnusedVarsVisitor<
}

public static collectUnusedVariables<
TMessageIds extends string,
TOptions extends readonly unknown[],
MessageIds extends string,
Options extends readonly unknown[],
>(
context: TSESLint.RuleContext<TMessageIds, TOptions>,
context: TSESLint.RuleContext<MessageIds, Options>,
): ReadonlySet<TSESLint.Scope.Variable> {
const program = context.sourceCode.ast;
const cached = this.RESULTS_CACHE.get(program);
Expand Down Expand Up @@ -749,10 +749,10 @@ function isUsedVariable(variable: TSESLint.Scope.Variable): boolean {
* - variables within ambient module declarations
*/
function collectUnusedVariables<
TMessageIds extends string,
TOptions extends readonly unknown[],
MessageIds extends string,
Options extends readonly unknown[],
>(
context: Readonly<TSESLint.RuleContext<TMessageIds, TOptions>>,
context: Readonly<TSESLint.RuleContext<MessageIds, Options>>,
): ReadonlySet<TSESLint.Scope.Variable> {
return UnusedVarsVisitor.collectUnusedVariables(context);
}
Expand Down
12 changes: 6 additions & 6 deletions packages/eslint-plugin/src/util/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ function getNameFromMember(
}

type ExcludeKeys<
TObj extends Record<string, unknown>,
TKeys extends keyof TObj,
> = { [k in Exclude<keyof TObj, TKeys>]: TObj[k] };
Obj extends Record<string, unknown>,
Keys extends keyof Obj,
> = { [k in Exclude<keyof Obj, Keys>]: Obj[k] };
type RequireKeys<
TObj extends Record<string, unknown>,
TKeys extends keyof TObj,
> = ExcludeKeys<TObj, TKeys> & { [k in TKeys]-?: Exclude<TObj[k], undefined> };
Obj extends Record<string, unknown>,
Keys extends keyof Obj,
> = ExcludeKeys<Obj, Keys> & { [k in Keys]-?: Exclude<Obj[k], undefined> };

function getEnumNames<T extends string>(myEnum: Record<T, unknown>): T[] {
return Object.keys(myEnum).filter(x => isNaN(parseInt(x))) as T[];
Expand Down
16 changes: 8 additions & 8 deletions packages/eslint-plugin/src/util/objectIterators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ function objectForEachKey<T extends Record<string, unknown>>(
}
}

function objectMapKey<T extends Record<string, unknown>, TReturn>(
function objectMapKey<T extends Record<string, unknown>, Return>(
obj: T,
callback: (key: keyof T) => TReturn,
): TReturn[] {
const values: TReturn[] = [];
callback: (key: keyof T) => Return,
): Return[] {
const values: Return[] = [];
objectForEachKey(obj, key => {
values.push(callback(key));
});
return values;
}

function objectReduceKey<T extends Record<string, unknown>, TAccumulator>(
function objectReduceKey<T extends Record<string, unknown>, Accumulator>(
obj: T,
callback: (acc: TAccumulator, key: keyof T) => TAccumulator,
initial: TAccumulator,
): TAccumulator {
callback: (acc: Accumulator, key: keyof T) => Accumulator,
initial: Accumulator,
): Accumulator {
let accumulator = initial;
objectForEachKey(obj, key => {
accumulator = callback(accumulator, key);
Expand Down
22 changes: 11 additions & 11 deletions packages/eslint-plugin/tests/RuleTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export function getFixturesRootDir(): string {
*
* @deprecated - DO NOT USE THIS FOR NEW RULES
*/
export function batchedSingleLineTests<TOptions extends readonly unknown[]>(
test: ValidTestCase<TOptions>,
): ValidTestCase<TOptions>[];
export function batchedSingleLineTests<Options extends readonly unknown[]>(
test: ValidTestCase<Options>,
): ValidTestCase<Options>[];
/**
* Converts a batch of single line tests into a number of separate test cases.
* This makes it easier to write tests which use the same options.
Expand All @@ -35,17 +35,17 @@ export function batchedSingleLineTests<TOptions extends readonly unknown[]>(
* @deprecated - DO NOT USE THIS FOR NEW RULES
*/
export function batchedSingleLineTests<
TMessageIds extends string,
TOptions extends readonly unknown[],
MessageIds extends string,
Options extends readonly unknown[],
>(
test: InvalidTestCase<TMessageIds, TOptions>,
): InvalidTestCase<TMessageIds, TOptions>[];
test: InvalidTestCase<MessageIds, Options>,
): InvalidTestCase<MessageIds, Options>[];
export function batchedSingleLineTests<
TMessageIds extends string,
TOptions extends readonly unknown[],
MessageIds extends string,
Options extends readonly unknown[],
>(
options: InvalidTestCase<TMessageIds, TOptions> | ValidTestCase<TOptions>,
): (InvalidTestCase<TMessageIds, TOptions> | ValidTestCase<TOptions>)[] {
options: InvalidTestCase<MessageIds, Options> | ValidTestCase<Options>,
): (InvalidTestCase<MessageIds, Options> | ValidTestCase<Options>)[] {
// -- eslint counts lines from 1
const lineOffset = options.code.startsWith('\n') ? 2 : 1;
const output =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1060,24 +1060,22 @@ ruleTester.run('prefer-string-starts-ends-with', rule, {
]),
});

type Case<TMessageIds extends string, TOptions extends Readonly<unknown[]>> =
| TSESLint.InvalidTestCase<TMessageIds, TOptions>
| TSESLint.ValidTestCase<TOptions>;
function addOptional<TOptions extends Readonly<unknown[]>>(
cases: (TSESLint.ValidTestCase<TOptions> | string)[],
): TSESLint.ValidTestCase<TOptions>[];
type Case<MessageIds extends string, Options extends Readonly<unknown[]>> =
| TSESLint.InvalidTestCase<MessageIds, Options>
| TSESLint.ValidTestCase<Options>;
function addOptional<Options extends Readonly<unknown[]>>(
cases: (TSESLint.ValidTestCase<Options> | string)[],
): TSESLint.ValidTestCase<Options>[];
function addOptional<
TMessageIds extends string,
TOptions extends Readonly<unknown[]>,
MessageIds extends string,
Options extends Readonly<unknown[]>,
>(
cases: TSESLint.InvalidTestCase<TMessageIds, TOptions>[],
): TSESLint.InvalidTestCase<TMessageIds, TOptions>[];
cases: TSESLint.InvalidTestCase<MessageIds, Options>[],
): TSESLint.InvalidTestCase<MessageIds, Options>[];
function addOptional<
TMessageIds extends string,
TOptions extends Readonly<unknown[]>,
>(
cases: (Case<TMessageIds, TOptions> | string)[],
): Case<TMessageIds, TOptions>[] {
MessageIds extends string,
Options extends Readonly<unknown[]>,
>(cases: (Case<MessageIds, Options> | string)[]): Case<MessageIds, Options>[] {
function makeOptional(code: string): string;
function makeOptional(code: string | null | undefined): string | null;
function makeOptional(code: string | null | undefined): string | null {
Expand All @@ -1093,7 +1091,7 @@ function addOptional<
);
}

return cases.reduce<Case<TMessageIds, TOptions>[]>((acc, c) => {
return cases.reduce<Case<MessageIds, Options>[]>((acc, c) => {
if (typeof c === 'string') {
acc.push({
code: c,
Expand Down
78 changes: 37 additions & 41 deletions packages/rule-tester/src/RuleTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,21 @@ export class RuleTester extends TestFramework {
/**
* Adds the `only` property to a test to run it in isolation.
*/
static only<TOptions extends Readonly<unknown[]>>(
item: ValidTestCase<TOptions> | string,
): ValidTestCase<TOptions>;
static only<Options extends Readonly<unknown[]>>(
item: ValidTestCase<Options> | string,
): ValidTestCase<Options>;
/**
* Adds the `only` property to a test to run it in isolation.
*/
static only<TMessageIds extends string, TOptions extends Readonly<unknown[]>>(
item: InvalidTestCase<TMessageIds, TOptions>,
): InvalidTestCase<TMessageIds, TOptions>;
static only<TMessageIds extends string, TOptions extends Readonly<unknown[]>>(
static only<MessageIds extends string, Options extends Readonly<unknown[]>>(
item: InvalidTestCase<MessageIds, Options>,
): InvalidTestCase<MessageIds, Options>;
static only<MessageIds extends string, Options extends Readonly<unknown[]>>(
item:
| InvalidTestCase<TMessageIds, TOptions>
| ValidTestCase<TOptions>
| InvalidTestCase<MessageIds, Options>
| ValidTestCase<Options>
| string,
): InvalidTestCase<TMessageIds, TOptions> | ValidTestCase<TOptions> {
): InvalidTestCase<MessageIds, Options> | ValidTestCase<Options> {
if (typeof item === 'string') {
return { code: item, only: true };
}
Expand All @@ -176,11 +176,11 @@ export class RuleTester extends TestFramework {
}

#normalizeTests<
TMessageIds extends string,
TOptions extends readonly unknown[],
MessageIds extends string,
Options extends readonly unknown[],
>(
rawTests: RunTests<TMessageIds, TOptions>,
): NormalizedRunTests<TMessageIds, TOptions> {
rawTests: RunTests<MessageIds, Options>,
): NormalizedRunTests<MessageIds, Options> {
/*
Automatically add a filename to the tests to enable type-aware tests to "just work".
This saves users having to verbosely and manually add the filename to every
Expand All @@ -205,11 +205,9 @@ export class RuleTester extends TestFramework {
return filename;
};
const normalizeTest = <
TMessageIds extends string,
TOptions extends readonly unknown[],
T extends
| InvalidTestCase<TMessageIds, TOptions>
| ValidTestCase<TOptions>,
MessageIds extends string,
Options extends readonly unknown[],
T extends InvalidTestCase<MessageIds, Options> | ValidTestCase<Options>,
>(
test: T,
): T => {
Expand Down Expand Up @@ -239,7 +237,7 @@ export class RuleTester extends TestFramework {

// convenience iterator to make it easy to loop all tests without a concat
const allTestsIterator = {
*[Symbol.iterator](): Generator<ValidTestCase<TOptions>, void> {
*[Symbol.iterator](): Generator<ValidTestCase<Options>, void> {
for (const testCase of normalizedTests.valid) {
yield testCase;
}
Expand Down Expand Up @@ -285,9 +283,7 @@ export class RuleTester extends TestFramework {
just disappearing without a trace.
*/
const maybeMarkAsOnly = <
T extends
| InvalidTestCase<TMessageIds, TOptions>
| ValidTestCase<TOptions>,
T extends InvalidTestCase<MessageIds, Options> | ValidTestCase<Options>,
>(
test: T,
): T => {
Expand All @@ -305,10 +301,10 @@ export class RuleTester extends TestFramework {
/**
* Adds a new rule test to execute.
*/
run<TMessageIds extends string, TOptions extends readonly unknown[]>(
run<MessageIds extends string, Options extends readonly unknown[]>(
ruleName: string,
rule: RuleModule<TMessageIds, TOptions>,
test: RunTests<TMessageIds, TOptions>,
rule: RuleModule<MessageIds, Options>,
test: RunTests<MessageIds, Options>,
): void {
const constructor = this.constructor as typeof RuleTester;

Expand Down Expand Up @@ -366,7 +362,7 @@ export class RuleTester extends TestFramework {
ruleName,
Object.assign({}, rule, {
// Create a wrapper rule that freezes the `context` properties.
create(context: RuleContext<TMessageIds, TOptions>) {
create(context: RuleContext<MessageIds, Options>) {
freezeDeeply(context.options);
freezeDeeply(context.settings);
freezeDeeply(context.parserOptions);
Expand All @@ -381,7 +377,7 @@ export class RuleTester extends TestFramework {
const normalizedTests = this.#normalizeTests(test);

function getTestMethod(
test: ValidTestCase<TOptions>,
test: ValidTestCase<Options>,
): 'it' | 'itOnly' | 'itSkip' {
if (test.skip) {
return 'itSkip';
Expand Down Expand Up @@ -437,12 +433,12 @@ export class RuleTester extends TestFramework {
* Use @private instead of #private to expose it for testing purposes
*/
private runRuleForItem<
TMessageIds extends string,
TOptions extends readonly unknown[],
MessageIds extends string,
Options extends readonly unknown[],
>(
ruleName: string,
rule: RuleModule<TMessageIds, TOptions>,
item: InvalidTestCase<TMessageIds, TOptions> | ValidTestCase<TOptions>,
rule: RuleModule<MessageIds, Options>,
item: InvalidTestCase<MessageIds, Options> | ValidTestCase<Options>,
): {
messages: Linter.LintMessage[];
output: string;
Expand Down Expand Up @@ -627,14 +623,14 @@ export class RuleTester extends TestFramework {
* all valid cases go through this
*/
#testValidTemplate<
TMessageIds extends string,
TOptions extends readonly unknown[],
MessageIds extends string,
Options extends readonly unknown[],
>(
ruleName: string,
rule: RuleModule<TMessageIds, TOptions>,
itemIn: ValidTestCase<TOptions> | string,
rule: RuleModule<MessageIds, Options>,
itemIn: ValidTestCase<Options> | string,
): void {
const item: ValidTestCase<TOptions> =
const item: ValidTestCase<Options> =
typeof itemIn === 'object' ? itemIn : { code: itemIn };

assert.ok(
Expand Down Expand Up @@ -669,12 +665,12 @@ export class RuleTester extends TestFramework {
* all invalid cases go through this.
*/
#testInvalidTemplate<
TMessageIds extends string,
TOptions extends readonly unknown[],
MessageIds extends string,
Options extends readonly unknown[],
>(
ruleName: string,
rule: RuleModule<TMessageIds, TOptions>,
item: InvalidTestCase<TMessageIds, TOptions>,
rule: RuleModule<MessageIds, Options>,
item: InvalidTestCase<MessageIds, Options>,
): void {
assert.ok(
typeof item.code === 'string',
Expand Down
Loading

0 comments on commit fa67955

Please sign in to comment.