Skip to content

Commit

Permalink
fix(eslint-plugin): [no-shadow] fix static class generics for class e…
Browse files Browse the repository at this point in the history
…xpressions (#7724)

* Add test for factory generating class functionn with generic type

* Rename isGenericOfClassDecl to isGenericOfClass, check for class expression in function

* Apply eslint fixer on no shadow test class expression test case

* Apply yarn lint-fix
  • Loading branch information
peanutenthusiast committed Oct 11, 2023
1 parent ac397f1 commit e5ea1d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/eslint-plugin/src/rules/no-shadow.ts
Expand Up @@ -199,7 +199,7 @@ export default createRule<Options, MessageIds>({
return methodDefinition.static;
}

function isGenericOfClassDecl(variable: TSESLint.Scope.Variable): boolean {
function isGenericOfClass(variable: TSESLint.Scope.Variable): boolean {
if (!('isTypeVariable' in variable)) {
// this shouldn't happen...
return false;
Expand All @@ -224,16 +224,17 @@ export default createRule<Options, MessageIds>({
return false;
}
const classDecl = typeParameterDecl.parent;
return classDecl?.type === AST_NODE_TYPES.ClassDeclaration;
return (
classDecl?.type === AST_NODE_TYPES.ClassDeclaration ||
classDecl?.type === AST_NODE_TYPES.ClassExpression
);
}

function isGenericOfAStaticMethodShadow(
variable: TSESLint.Scope.Variable,
shadowed: TSESLint.Scope.Variable,
): boolean {
return (
isGenericOfStaticMethod(variable) && isGenericOfClassDecl(shadowed)
);
return isGenericOfStaticMethod(variable) && isGenericOfClass(shadowed);
}

function isImportDeclaration(
Expand Down
11 changes: 11 additions & 0 deletions packages/eslint-plugin/tests/rules/no-shadow/no-shadow.test.ts
Expand Up @@ -220,6 +220,17 @@ export class Wrapper<Wrapped> {
static create<Wrapped>(wrapped: Wrapped) {
return new Wrapper<Wrapped>(wrapped);
}
}
`,
`
function makeA() {
return class A<T> {
constructor(public value: T) {}
static make<T>(value: T) {
return new A<T>(value);
}
};
}
`,
{
Expand Down

0 comments on commit e5ea1d0

Please sign in to comment.