Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JoshuaKGoldberg merged 3 commits intotypescript-eslint:mainfromyeonjuan:fix/1224
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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
ContributorAuthor

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

declarevarFoo:{// VariableDeclarationnew<T>(type:T):any;};classBarextendsFoo<string>{}

JoshuaKGoldberg reacted with thumbs up emoji
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();
}
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp