Skip to content

fix(eslint-plugin): [no-unnecessary-type-arguments] handle type/value context #10503

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
merged 3 commits into from
Dec 21, 2024
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
98 changes: 78 additions & 20 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-arguments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils';

import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';

Expand Down Expand Up @@ -111,8 +112,12 @@ export default createRule<[], MessageIds>({
return {
TSTypeParameterInstantiation(node): void {
const expression = services.esTreeNodeToTSNodeMap.get(node);
const typeParameters = getTypeParametersFromNode(
node,
expression,
checker,
);

const typeParameters = getTypeParametersFromNode(expression, checker);
if (typeParameters) {
checkTSArgsAndParameters(node, typeParameters);
}
Expand All @@ -122,29 +127,31 @@ export default createRule<[], MessageIds>({
});

function getTypeParametersFromNode(
node: ParameterCapableTSNode,
node: TSESTree.TSTypeParameterInstantiation,
tsNode: ParameterCapableTSNode,
checker: ts.TypeChecker,
): readonly ts.TypeParameterDeclaration[] | undefined {
if (ts.isExpressionWithTypeArguments(node)) {
return getTypeParametersFromType(node.expression, checker);
if (ts.isExpressionWithTypeArguments(tsNode)) {
return getTypeParametersFromType(node, tsNode.expression, checker);
}

if (ts.isTypeReferenceNode(node)) {
return getTypeParametersFromType(node.typeName, checker);
if (ts.isTypeReferenceNode(tsNode)) {
return getTypeParametersFromType(node, tsNode.typeName, checker);
}

if (
ts.isCallExpression(node) ||
ts.isNewExpression(node) ||
ts.isTaggedTemplateExpression(node)
ts.isCallExpression(tsNode) ||
ts.isNewExpression(tsNode) ||
ts.isTaggedTemplateExpression(tsNode)
) {
return getTypeParametersFromCall(node, checker);
return getTypeParametersFromCall(node, tsNode, checker);
}

return undefined;
}

function getTypeParametersFromType(
node: TSESTree.TSTypeParameterInstantiation,
type: ts.ClassDeclaration | ts.EntityName | ts.Expression,
checker: ts.TypeChecker,
): readonly ts.TypeParameterDeclaration[] | undefined {
Expand All @@ -160,24 +167,36 @@ function getTypeParametersFromType(
return undefined;
}

return findFirstResult(declarations, decl =>
ts.isClassLike(decl) ||
ts.isTypeAliasDeclaration(decl) ||
ts.isInterfaceDeclaration(decl)
? decl.typeParameters
: undefined,
const sortedDeclaraions = sortDeclarationsByTypeValueContext(
node,
declarations,
);
return findFirstResult(sortedDeclaraions, decl => {
if (
ts.isTypeAliasDeclaration(decl) ||
ts.isInterfaceDeclaration(decl) ||
ts.isClassLike(decl)
) {
return decl.typeParameters;
}
if (ts.isVariableDeclaration(decl)) {
return getConstructSignatureDeclaration(symAtLocation, checker)
?.typeParameters;
}
Comment on lines +182 to +185
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added it to handle the following cases

declare var Foo: { // VariableDeclaration
  new <T>(type: T): any;
};
class Bar extends Foo<string> {}

return undefined;
});
}

function getTypeParametersFromCall(
node: ts.CallExpression | ts.NewExpression | ts.TaggedTemplateExpression,
node: TSESTree.TSTypeParameterInstantiation,
tsNode: ts.CallExpression | ts.NewExpression | ts.TaggedTemplateExpression,
checker: ts.TypeChecker,
): readonly ts.TypeParameterDeclaration[] | undefined {
const sig = checker.getResolvedSignature(node);
const sig = checker.getResolvedSignature(tsNode);
const sigDecl = sig?.getDeclaration();
if (!sigDecl) {
return ts.isNewExpression(node)
? getTypeParametersFromType(node.expression, checker)
return ts.isNewExpression(tsNode)
? getTypeParametersFromType(node, tsNode.expression, checker)
: undefined;
}

Expand All @@ -192,3 +211,42 @@ function getAliasedSymbol(
? checker.getAliasedSymbol(symbol)
: symbol;
}

function isInTypeContext(node: TSESTree.TSTypeParameterInstantiation) {
return (
node.parent.type === AST_NODE_TYPES.TSInterfaceHeritage ||
node.parent.type === AST_NODE_TYPES.TSTypeReference ||
node.parent.type === AST_NODE_TYPES.TSClassImplements
);
}

function isTypeContextDeclaration(decl: ts.Declaration) {
return ts.isTypeAliasDeclaration(decl) || ts.isInterfaceDeclaration(decl);
}

function typeFirstCompare(declA: ts.Declaration, declB: ts.Declaration) {
const aIsType = isTypeContextDeclaration(declA);
const bIsType = isTypeContextDeclaration(declB);

return Number(bIsType) - Number(aIsType);
}

function sortDeclarationsByTypeValueContext(
node: TSESTree.TSTypeParameterInstantiation,
declarations: ts.Declaration[],
) {
const sorted = [...declarations].sort(typeFirstCompare);
if (isInTypeContext(node)) {
return sorted;
}
return sorted.reverse();
}

function getConstructSignatureDeclaration(
symbol: ts.Symbol,
checker: ts.TypeChecker,
): ts.SignatureDeclaration | undefined {
const type = checker.getTypeOfSymbol(symbol);
const sig = type.getConstructSignatures();
return sig.at(0)?.getDeclaration();
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@ type A = Map<string, string>;
type B<T = A> = T;
type C2 = B<Map<string, number>>;
`,
`
interface Foo<T = string> {}
declare var Foo: {
new <T>(type: T): any;
};
class Bar extends Foo<string> {}
`,
`
interface Foo<T = string> {}
class Foo<T> {}
class Bar extends Foo<string> {}
`,
`
class Foo<T = string> {}
interface Foo<T> {}
class Bar implements Foo<string> {}
`,
`
class Foo<T> {}
namespace Foo {
export class Bar {}
}
class Bar extends Foo<string> {}
`,
],
invalid: [
{
Expand Down Expand Up @@ -452,5 +476,89 @@ type E<T = B> = T;
type F = E;
`,
},
{
code: `
interface Foo {}
declare var Foo: {
new <T = string>(type: T): any;
};
class Bar extends Foo<string> {}
`,
errors: [
{
line: 6,
messageId: 'unnecessaryTypeParameter',
},
],
output: `
interface Foo {}
declare var Foo: {
new <T = string>(type: T): any;
};
class Bar extends Foo {}
`,
},
{
code: `
declare var Foo: {
new <T = string>(type: T): any;
};
interface Foo {}
class Bar extends Foo<string> {}
`,
errors: [
{
line: 6,
messageId: 'unnecessaryTypeParameter',
},
],
output: `
declare var Foo: {
new <T = string>(type: T): any;
};
interface Foo {}
class Bar extends Foo {}
`,
},
{
code: `
class Foo<T> {}
interface Foo<T = string> {}
class Bar implements Foo<string> {}
`,
errors: [
{
line: 4,
messageId: 'unnecessaryTypeParameter',
},
],
output: `
class Foo<T> {}
interface Foo<T = string> {}
class Bar implements Foo {}
`,
},
{
code: `
class Foo<T = string> {}
namespace Foo {
export class Bar {}
}
class Bar extends Foo<string> {}
`,
errors: [
{
line: 6,
messageId: 'unnecessaryTypeParameter',
},
],
output: `
class Foo<T = string> {}
namespace Foo {
export class Bar {}
}
class Bar extends Foo {}
`,
},
],
});
Loading