Skip to content

fix(eslint-plugin): [no-unnecessary-type-parameters] clarify message #9737

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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
description: 'Disallow type parameters that only appear once.'
description: "Disallow type parameters that aren't used multiple times."
---

import Tabs from '@theme/Tabs';
Expand All @@ -9,10 +9,10 @@ import TabItem from '@theme/TabItem';
>
> See **https://typescript-eslint.io/rules/no-unnecessary-type-parameters** for documentation.

This rule forbids type parameters that only appear once in a function, method, or class definition.
This rule forbids type parameters that aren't used multiple times in a function, method, or class definition.

Type parameters relate two types.
If a type parameter only appears once, then it is not relating anything.
If a type parameter is only used once, then it is not relating anything.
It can usually be replaced with explicit types such as `unknown`.

At best unnecessary type parameters make code harder to read.
Expand Down
101 changes: 56 additions & 45 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,69 @@ export default createRule({
defaultOptions: [],
meta: {
docs: {
description: 'Disallow type parameters that only appear once',
description: "Disallow type parameters that aren't used multiple times",
requiresTypeChecking: true,
recommended: 'strict',
},
messages: {
sole: 'Type parameter {{name}} is used only once.',
sole: 'Type parameter {{name}} is {{uses}} in the {{descriptor}} signature.',
},
schema: [],
type: 'problem',
},
name: 'no-unnecessary-type-parameters',
create(context) {
const parserServices = getParserServices(context);

function checkNode(node: TSESTree.FunctionLike, descriptor: string): void {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(
node,
) as NodeWithTypeParameters;

const checker = parserServices.program.getTypeChecker();
let counts: Map<ts.Identifier, number> | undefined;

for (const typeParameter of tsNode.typeParameters) {
const esTypeParameter =
parserServices.tsNodeToESTreeNodeMap.get<TSESTree.TSTypeParameter>(
typeParameter,
);
const scope = context.sourceCode.getScope(esTypeParameter);

// 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,
node.body?.range[0] ?? node.returnType?.range[1],
)
) {
continue;
}

// For any inferred types, we have to dip into type checking.
counts ??= countTypeParameterUsage(checker, tsNode);
const identifierCounts = counts.get(typeParameter.name);
if (!identifierCounts || identifierCounts > 2) {
continue;
}

context.report({
data: {
descriptor,
uses: identifierCounts === 1 ? 'never used' : 'used only once',
name: typeParameter.name.text,
},
node: esTypeParameter,
messageId: 'sole',
});
}
}

return {
[[
'ArrowFunctionExpression[typeParameters]',
'ClassDeclaration[typeParameters]',
'ClassExpression[typeParameters]',
'FunctionDeclaration[typeParameters]',
'FunctionExpression[typeParameters]',
'TSCallSignatureDeclaration[typeParameters]',
Expand All @@ -43,47 +88,13 @@ export default createRule({
'TSFunctionType[typeParameters]',
'TSMethodSignature[typeParameters]',
].join(', ')](node: TSESTree.FunctionLike): void {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(
node,
) as NodeWithTypeParameters;

const checker = parserServices.program.getTypeChecker();
let counts: Map<ts.Identifier, number> | undefined;

for (const typeParameter of tsNode.typeParameters) {
const esTypeParameter =
parserServices.tsNodeToESTreeNodeMap.get<TSESTree.TSTypeParameter>(
typeParameter,
);
const scope = context.sourceCode.getScope(esTypeParameter);

// 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,
node.body?.range[0] ?? node.returnType?.range[1],
)
) {
continue;
}

// For any inferred types, we have to dip into type checking.
counts ??= countTypeParameterUsage(checker, tsNode);
const identifierCounts = counts.get(typeParameter.name);
if (!identifierCounts || identifierCounts > 2) {
continue;
}

context.report({
data: {
name: typeParameter.name.text,
},
node: esTypeParameter,
messageId: 'sole',
});
}
checkNode(node, 'function');
},
[[
'ClassDeclaration[typeParameters]',
'ClassExpression[typeParameters]',
].join(', ')](node: TSESTree.FunctionLike): void {
checkNode(node, 'class');
},
};
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
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