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 FP S6647 (no-useless-constructor): Ignore decorated classes and classes inheriting protected constructors #4546

Merged
merged 4 commits into from
Feb 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions its/ruling/src/test/expected/jsts/ag-grid/typescript-S6647.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"fireface:src/app-header/app-header.component.ts": [
15
],
"fireface:src/common/avatar.model.ts": [
38
]
Expand Down

This file was deleted.

32 changes: 0 additions & 32 deletions its/ruling/src/test/expected/jsts/rxjs/typescript-S6647.json

This file was deleted.

23 changes: 23 additions & 0 deletions packages/jsts/src/rules/S6647/cb.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Lambda } from 'lambda';

class Foo {
private constructor() {
// this is ok
Expand All @@ -19,3 +21,24 @@ class SubClass extends SuperClass {
super();
}
}

@Decorator()
class Decorated {
constructor() {}
}

class Alpha {
protected constructor() {}
}

class Beta extends Alpha {
constructor() {
super();
}
}

class Gamma extends Lambda {
constructor() {
super();
}
}
47 changes: 46 additions & 1 deletion packages/jsts/src/rules/S6647/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { TSESTree } from '@typescript-eslint/utils';
import { Rule } from 'eslint';
import { eslintRules } from '../core';
import { decorate } from './decorator';
import { getVariableFromName } from '../helpers';

/**
* Check if method with accessibility is not useless
Expand Down Expand Up @@ -52,6 +53,48 @@ function checkParams(node: TSESTree.MethodDefinition): boolean {
);
}

/**
* Check if the enclosing class is not decorated.
*/
function checkDecorator(node: TSESTree.MethodDefinition): boolean {
return !(
node.parent.parent?.type === 'ClassDeclaration' && node.parent.parent.decorators?.length > 0
);
}

/**
* Check if the enclosing class does not inherit a protected constructor.
*/
function checkInheritance(node: TSESTree.MethodDefinition, context: Rule.RuleContext): boolean {
if (
node.parent.type === 'ClassBody' &&
'superClass' in node.parent.parent &&
node.parent.parent.superClass
) {
const superClass = node.parent.parent.superClass as TSESTree.Identifier;
const variable = getVariableFromName(context, superClass.name);
for (const def of variable?.defs ?? []) {
if (def.type === 'ImportBinding') {
return false;
}
if (def.node.type === 'ClassDeclaration') {
const decl = def.node as TSESTree.ClassDeclaration;
if (
decl.body.body.some(
member =>
member.type === 'MethodDefinition' &&
member.kind === 'constructor' &&
member.accessibility === 'protected',
)
) {
return false;
}
}
}
}
return true;
}

const eslintNoUselessConstructor = eslintRules['no-useless-constructor'];

const originalRule: Rule.RuleModule = {
Expand All @@ -68,7 +111,9 @@ const originalRule: Rule.RuleModule = {
node.value.type === 'FunctionExpression' &&
node.kind === 'constructor' &&
checkAccessibility(node as TSESTree.MethodDefinition) &&
checkParams(node as TSESTree.MethodDefinition)
checkParams(node as TSESTree.MethodDefinition) &&
checkDecorator(node as TSESTree.MethodDefinition) &&
checkInheritance(node as TSESTree.MethodDefinition, context)
) {
rules.MethodDefinition!(node);
}
Expand Down
Loading