Skip to content

Commit

Permalink
fixup! fix(eslint-plugin): [prefer-return-this-type] handle generics …
Browse files Browse the repository at this point in the history
…properly in fixer
  • Loading branch information
rafaelss95 committed Sep 21, 2021
1 parent b9d0f5e commit a937297
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions packages/eslint-plugin/src/rules/prefer-return-this-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ export default createRule({
function tryGetNameInType(
name: string,
typeNode: TSESTree.TypeNode,
): TSESTree.Identifier | undefined {
): TSESTree.TSTypeReference | undefined {
if (
typeNode.type === AST_NODE_TYPES.TSTypeReference &&
typeNode.typeName.type === AST_NODE_TYPES.Identifier &&
typeNode.typeName.name === name
) {
return typeNode.typeName;
return typeNode;
}

if (typeNode.type === AST_NODE_TYPES.TSUnionType) {
Expand Down Expand Up @@ -130,27 +130,19 @@ export default createRule({
originalClass: ClassLikeDeclaration,
): void {
const className = originalClass.id?.name;
if (!className) {
if (!className || !originalFunc.returnType) {
return;
}

if (!originalFunc.returnType) {
return;
}

const classNameRef = tryGetNameInType(
const node = tryGetNameInType(
className,
originalFunc.returnType.typeAnnotation,
);
if (!classNameRef) {
if (!node) {
return;
}

if (isFunctionReturningThis(originalFunc, originalClass)) {
const node =
classNameRef.parent?.type === AST_NODE_TYPES.TSTypeReference
? classNameRef.parent
: classNameRef;
context.report({
node,
messageId: 'useThisType',
Expand Down

0 comments on commit a937297

Please sign in to comment.