Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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'; | ||
@@ -111,8 +112,12 @@ export default createRule<[], MessageIds>({ | ||
return { | ||
TSTypeParameterInstantiation(node): void { | ||
const expression = services.esTreeNodeToTSNodeMap.get(node); | ||
const typeParameters = getTypeParametersFromNode( | ||
node, | ||
expression, | ||
checker, | ||
); | ||
if (typeParameters) { | ||
checkTSArgsAndParameters(node, typeParameters); | ||
} | ||
@@ -122,29 +127,31 @@ export default createRule<[], MessageIds>({ | ||
}); | ||
function getTypeParametersFromNode( | ||
node: TSESTree.TSTypeParameterInstantiation, | ||
tsNode: ParameterCapableTSNode, | ||
checker: ts.TypeChecker, | ||
): readonly ts.TypeParameterDeclaration[] | undefined { | ||
if (ts.isExpressionWithTypeArguments(tsNode)) { | ||
return getTypeParametersFromType(node, tsNode.expression, checker); | ||
} | ||
if (ts.isTypeReferenceNode(tsNode)) { | ||
return getTypeParametersFromType(node, tsNode.typeName, checker); | ||
} | ||
if ( | ||
ts.isCallExpression(tsNode) || | ||
ts.isNewExpression(tsNode) || | ||
ts.isTaggedTemplateExpression(tsNode) | ||
) { | ||
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 { | ||
@@ -160,24 +167,36 @@ function getTypeParametersFromType( | ||
return 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>{} | ||
return undefined; | ||
}); | ||
} | ||
function getTypeParametersFromCall( | ||
node: TSESTree.TSTypeParameterInstantiation, | ||
tsNode: ts.CallExpression | ts.NewExpression | ts.TaggedTemplateExpression, | ||
checker: ts.TypeChecker, | ||
): readonly ts.TypeParameterDeclaration[] | undefined { | ||
const sig = checker.getResolvedSignature(tsNode); | ||
const sigDecl = sig?.getDeclaration(); | ||
if (!sigDecl) { | ||
return ts.isNewExpression(tsNode) | ||
? getTypeParametersFromType(node, tsNode.expression, checker) | ||
: undefined; | ||
} | ||
@@ -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(); | ||
} |
Uh oh!
There was an error while loading.Please reload this page.