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

Conversation

josephperrott
Copy link
Member

No description provided.

import * as Lint from 'tslint';

export class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
Copy link
Member

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) {
Copy link
Member

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,
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.

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Oct 26, 2017
Copy link
Member

@crisbeto crisbeto left a 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 {
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?

@crisbeto crisbeto added pr: lgtm action: merge The PR is ready for merge by the caretaker and removed pr: needs review labels Oct 26, 2017
@mmalerba mmalerba merged commit 473b1df into angular:master Oct 27, 2017
@josephperrott josephperrott deleted the tslint branch October 31, 2017 18:55
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants