chore: create tslint rule to allow @HostListener and @HostBinding in abstract classes (#8036)

This commit is contained in:
Joey Perrott
2017-10-27 10:18:36 -07:00
committed by mmalerba
parent 24984f5e44
commit 473b1dfafd
2 changed files with 43 additions and 3 deletions
@@ -0,0 +1,37 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';
export class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
}
}
class Walker extends Lint.RuleWalker {
visitClassDeclaration(node: ts.ClassDeclaration) {
if (!node.modifiers || !this.getOptions().length) { return; }
// Do not check the class if its abstract.
if (!!node.modifiers.find(modifier => modifier.kind === ts.SyntaxKind.AbstractKeyword)) {
return;
}
node.members
.filter(el => el.decorators)
.map(el => el.decorators!)
.forEach(decorators => {
decorators.forEach(decorator => {
const decoratorText: string = decorator.getChildAt(1).getText();
const matchedDecorator: string = this.getOptions().find(
(item: string) => decoratorText.startsWith(item));
if (!!matchedDecorator) {
this.addFailureFromStartToEnd(decorator.getChildAt(1).pos - 1, decorator.end,
`The @${matchedDecorator} decorator may only be used in abstract classes. In ` +
`concrete classes use \`host\` in the component definition instead.`);
}
});
});
super.visitClassDeclaration(node);
}
}
+6 -3
View File
@@ -75,9 +75,7 @@
["fit"],
["fdescribe"],
["xit"],
["xdescribe"],
{ "name": "HostBinding", "message": "Use `host` in the component definition instead." },
{ "name": "HostListener", "message": "Use `host` in the component definition instead." }
["xdescribe"]
],
// Disallows importing the whole RxJS library. Submodules can be still imported.
"import-blacklist": [true, "rxjs"],
@@ -87,6 +85,11 @@
// Custom Rules
"ts-loader": true,
"no-exposed-todo": true,
"no-host-decorator-in-concrete": [
true,
"HostBinding",
"HostListener"
],
"validate-decorators": [true, {
"Component": {
"encapsulation": "\\.None$",