-
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
Conversation
import * as Lint from 'tslint'; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { |
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.
It looks like the indentation of the whole file is too large (seems like it's a 4, but it should be 2).
} | ||
|
||
node.members.forEach(el => { | ||
if (el.decorators) { |
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.
This block could be a bit more concise with a filter
and a map
:
node.members
.filter(el => !!el.decorators)
.map(el => el.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.`);
}
});
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 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.
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.
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.
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.
LGTM
} | ||
} | ||
|
||
class Walker extends Lint.RuleWalker { |
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?
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
No description provided.