Skip to content

Commit 9091330

Browse files
crisbetojelbourn
authored andcommitted
build: don't flag classes with empty constructors in undecorated class lint rule (#17829)
If a class constructor doesn't have parameters it can't be using DI so we don't need to flag it as something that needs to be decorated.
1 parent d990243 commit 9091330

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

tools/tslint-rules/noUndecoratedBaseClassDiRule.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,21 @@ class Walker extends Lint.RuleWalker {
2525
}
2626

2727
visitClassDeclaration(node: ts.ClassDeclaration) {
28-
if (!this.hasDirectiveDecorator(node)) {
29-
return;
30-
}
31-
32-
// If the class already has an explicit constructor, it's not required
33-
// for base classes to be decorated.
34-
if (this.hasExplicitConstructor(node)) {
28+
// If the class isn't decorated or it has an explicit constructor we don't need to check it.
29+
if (!this.hasDirectiveDecorator(node) || this.hasExplicitConstructor(node)) {
3530
return;
3631
}
3732

3833
const baseClass = this.getConstructorBaseClass(node);
3934
if (baseClass && !this.hasDirectiveDecorator(baseClass)) {
40-
this.addFailureAtNode(node, RULE_FAILURE);
35+
const constructor = baseClass.members.find(ts.isConstructorDeclaration);
36+
37+
// If the base class constructor doesn't have parameters we don't need to flag it, because
38+
// it can't be using DI. Note that technically we know the constructor exists because of
39+
// `getConstructorBaseClass`, but we null check it to keep the compiler happy.
40+
if (!constructor || constructor.parameters.length > 0) {
41+
this.addFailureAtNode(node, RULE_FAILURE);
42+
}
4143
}
4244
}
4345

0 commit comments

Comments
 (0)