mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
chore: create tslint rule to allow @HostListener and @HostBinding in abstract classes (#8036)
This commit is contained in:
@@ -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
@@ -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$",
|
||||
|
||||
Reference in New Issue
Block a user