Skip to content

build: don't flag classes with empty constructors in undecorated class lint rule #17829

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

Merged
merged 1 commit into from
Nov 28, 2019
Merged
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
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