Skip to content

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

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
Oct 27, 2017
Merged
Show file tree
Hide file tree
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
37 changes: 37 additions & 0 deletions tools/tslint-rules/noHostDecoratorInConcreteRule.ts
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a short docstring about what the rule does?

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not completely sure, but you should be able to use this.addFailureAtNode(decorator, ...) instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially tried this, but the starting position for the decorator was the character after the previous node, which made the tslint annotation go crazy.

`The @${matchedDecorator} decorator may only be used in abstract classes. In ` +
`concrete classes use \`host\` in the component definition instead.`);
}
});
});

super.visitClassDeclaration(node);
}
}
9 changes: 6 additions & 3 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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$",
Expand Down