Skip to content
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

Add new rule: jsx-no-bind #85

Merged
merged 1 commit into from
Apr 13, 2017
Merged

Add new rule: jsx-no-bind #85

merged 1 commit into from
Apr 13, 2017

Conversation

lvillani
Copy link
Contributor

References #44

@palantirtech
Copy link
Member

Thanks for your interest in palantir/tslint-react, @lvillani! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

Copy link
Contributor

@adidahiya adidahiya left a comment

Choose a reason for hiding this comment

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

nice, this looks pretty good as a simple syntactic check. it's prone to some rare false positives because it doesn't use the type checker, but I'm ok with that.

if you have time to refactor the walker for performance, that would be great. if not, we could clean it up in a future PR.

}

private reportFailure(node: ts.Node) {
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
Copy link
Contributor

Choose a reason for hiding this comment

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

can you use this.addFailureAtNode instead?

}
}

class JsxNoBindWalker extends Lint.RuleWalker {
Copy link
Contributor

Choose a reason for hiding this comment

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

would you mind refactoring to use WalkContext and applyWithFunction? see https://palantir.github.io/tslint/develop/custom-rules/performance-tips.html and https://palantir.github.io/tslint/develop/custom-rules/walker-design.html. the script in this repo (./scripts/newRule.js) ought to scaffold this up for you.

private propertyAccessCount = 0;

protected visitNode(node: ts.Node) {
console.log("kind:", ts.SyntaxKind[node.kind], "text:", node.getText());
Copy link
Contributor

Choose a reason for hiding this comment

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

remove console statements please

@adidahiya
Copy link
Contributor

also please enable CircleCI for your fork so that status can be reported here.

return;
}

if (!expression.getText(ctx.sourceFile).includes(".bind")) {
Copy link
Contributor Author

@lvillani lvillani Apr 12, 2017

Choose a reason for hiding this comment

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

@adidahiya I reworked the check to use a walk function as you suggested. I'm not really sure about this line though.

It seems to work in our project without false positives, but it looks brittle to me. Is there a better way to do this?

Copy link
Contributor

Choose a reason for hiding this comment

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

minor: can you use node.text instead of getText()? perhaps using some part of the expression subtree here?

overall this seems fine; we can only really hope to do better if we used the type checker (I think its costs outweigh its benefits here)

Copy link
Contributor

Choose a reason for hiding this comment

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

also maybe we should limit ourselves to only linting .bind(this)? are there any other cases this rule should lint for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I addressed the latter request by changing the code to search for .bind(this) but I can't seem to be able to use node.text or expression.text anywhere since I get a compilation error.

@adidahiya adidahiya merged commit 84759ad into palantir:master Apr 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants