Skip to content

Commit

Permalink
build: don't flag classes with empty constructors in undecorated clas…
Browse files Browse the repository at this point in the history
…s 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.
  • Loading branch information
crisbeto authored and jelbourn committed Nov 28, 2019
1 parent d990243 commit 9091330
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions tools/tslint-rules/noUndecoratedBaseClassDiRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ class Walker extends Lint.RuleWalker {
}

visitClassDeclaration(node: ts.ClassDeclaration) {
if (!this.hasDirectiveDecorator(node)) {
return;
}

// If the class already has an explicit constructor, it's not required
// for base classes to be decorated.
if (this.hasExplicitConstructor(node)) {
// If the class isn't decorated or it has an explicit constructor we don't need to check it.
if (!this.hasDirectiveDecorator(node) || this.hasExplicitConstructor(node)) {
return;
}

const baseClass = this.getConstructorBaseClass(node);
if (baseClass && !this.hasDirectiveDecorator(baseClass)) {
this.addFailureAtNode(node, RULE_FAILURE);
const constructor = baseClass.members.find(ts.isConstructorDeclaration);

// If the base class constructor doesn't have parameters we don't need to flag it, because
// it can't be using DI. Note that technically we know the constructor exists because of
// `getConstructorBaseClass`, but we null check it to keep the compiler happy.
if (!constructor || constructor.parameters.length > 0) {
this.addFailureAtNode(node, RULE_FAILURE);
}
}
}

Expand Down

0 comments on commit 9091330

Please sign in to comment.