Skip to content

fix(eslint-plugin): [no-unnecessary-type-parameters] skip checking function bodies for AST references #9738

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
20 changes: 17 additions & 3 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ export default createRule({

// Quick path: if the type parameter is used multiple times in the AST,
// we don't need to dip into types to know it's repeated.
if (isTypeParameterRepeatedInAST(esTypeParameter, scope.references)) {
if (
isTypeParameterRepeatedInAST(
esTypeParameter,
scope.references,
node.body?.range[0] ?? node.returnType?.range[1],
)
) {
continue;
}

Expand All @@ -86,18 +92,24 @@ export default createRule({
function isTypeParameterRepeatedInAST(
node: TSESTree.TSTypeParameter,
references: Reference[],
startOfBody = Infinity,
): boolean {
let total = 0;

for (const reference of references) {
// References inside the type parameter's definition don't count.
// References inside the type parameter's definition don't count...
if (
reference.identifier.range[0] < node.range[1] &&
reference.identifier.range[1] > node.range[0]
) {
continue;
}

// ...nor references that are outside the declaring signature.
if (reference.identifier.range[0] > startOfBody) {
continue;
}

// Neither do references that aren't to the same type parameter,
// namely value-land (non-type) identifiers of the type parameter's type,
// and references to different type parameters or values.
Expand All @@ -110,7 +122,9 @@ function isTypeParameterRepeatedInAST(

// If the type parameter is being used as a type argument, then we
// know the type parameter is being reused and can't be reported.
if (reference.identifier.parent.type === AST_NODE_TYPES.TSTypeReference) {
if (
reference.identifier.parent.type === AST_NODE_TYPES.TSTypeReference
) {
const grandparent = skipConstituentsUpward(
reference.identifier.parent.parent,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,46 @@ ruleTester.run('no-unnecessary-type-parameters', rule, {
{ messageId: 'sole', data: { name: 'B' } },
],
},
{
code: `
function foo<T>(_: T) {
const x: T = null!;
const y: T = null!;
}
`,
errors: [{ messageId: 'sole', data: { name: 'T' } }],
},
{
code: `
function foo<T>(_: T): void {
const x: T = null!;
const y: T = null!;
}
`,
errors: [{ messageId: 'sole', data: { name: 'T' } }],
},
{
code: `
function foo<T>(_: T): <T>(input: T) => T {
const x: T = null!;
const y: T = null!;
}
`,
errors: [{ messageId: 'sole', data: { name: 'T' } }],
},
{
code: `
function foo<T>(_: T) {
function withX(): T {
return null!;
}
function withY(): T {
return null!;
}
}
`,
errors: [{ messageId: 'sole', data: { name: 'T' } }],
},
{
code: `
function parseYAML<T>(input: string): T {
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy