Skip to content
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

fix(eslint-plugin): [no-shadow] fix static class method generics shadowing class generics #3393

Merged
merged 1 commit into from May 15, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 85 additions & 0 deletions packages/eslint-plugin/src/rules/no-shadow.ts
Expand Up @@ -126,6 +126,84 @@ export default util.createRule<Options, MessageIds>({
return util.isFunctionType(id.parent);
}

function isGenericOfStaticMethod(
variable: TSESLint.Scope.Variable,
): boolean {
if (!('isTypeVariable' in variable)) {
// this shouldn't happen...
return false;
}

if (!variable.isTypeVariable) {
return false;
}

if (variable.identifiers.length === 0) {
return false;
}

const typeParameter = variable.identifiers[0].parent;
if (typeParameter?.type !== AST_NODE_TYPES.TSTypeParameter) {
return false;
}
const typeParameterDecl = typeParameter.parent;
if (
typeParameterDecl?.type !== AST_NODE_TYPES.TSTypeParameterDeclaration
) {
return false;
}
const functionExpr = typeParameterDecl.parent;
if (
!functionExpr ||
(functionExpr.type !== AST_NODE_TYPES.FunctionExpression &&
functionExpr.type !== AST_NODE_TYPES.TSEmptyBodyFunctionExpression)
) {
return false;
}
const methodDefinition = functionExpr.parent;
if (methodDefinition?.type !== AST_NODE_TYPES.MethodDefinition) {
return false;
}
return methodDefinition.static;
}

function isGenericOfClassDecl(variable: TSESLint.Scope.Variable): boolean {
if (!('isTypeVariable' in variable)) {
// this shouldn't happen...
return false;
}

if (!variable.isTypeVariable) {
return false;
}

if (variable.identifiers.length === 0) {
return false;
}

const typeParameter = variable.identifiers[0].parent;
if (typeParameter?.type !== AST_NODE_TYPES.TSTypeParameter) {
return false;
}
const typeParameterDecl = typeParameter.parent;
if (
typeParameterDecl?.type !== AST_NODE_TYPES.TSTypeParameterDeclaration
) {
return false;
}
const classDecl = typeParameterDecl.parent;
return classDecl?.type === AST_NODE_TYPES.ClassDeclaration;
}

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

/**
* Check if variable name is allowed.
* @param variable The variable to check.
Expand Down Expand Up @@ -321,6 +399,13 @@ export default util.createRule<Options, MessageIds>({
continue;
}

// ignore static class method generic shadowing class generic
// this is impossible for the scope analyser to understand
// so we have to handle this manually in this rule
if (isGenericOfAStaticMethodShadow(variable, shadowed)) {
continue;
}

const isESLintGlobal = 'writeable' in shadowed;
if (
(shadowed.identifiers.length > 0 ||
Expand Down
13 changes: 13 additions & 0 deletions packages/eslint-plugin/tests/rules/no-shadow.test.ts
Expand Up @@ -159,6 +159,19 @@ type Fn = (Foo: string) => typeof Foo;
`,
options: [{ ignoreFunctionTypeParameterNameValueShadow: false }],
},
`
export class Wrapper<Wrapped> {
private constructor(private readonly wrapped: Wrapped) {}

unwrap(): Wrapped {
return this.wrapped;
}

static create<Wrapped>(wrapped: Wrapped) {
return new Wrapper<Wrapped>(wrapped);
}
}
`,
],
invalid: [
{
Expand Down