-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not completely sure, but you should be able to use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
There was a problem hiding this comment.
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?