Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

feat(ReactThisBindingIssueRule): allow decorators to bind #534

Merged
merged 9 commits into from
Oct 14, 2018
Merged

feat(ReactThisBindingIssueRule): allow decorators to bind #534

merged 9 commits into from
Oct 14, 2018

Conversation

cyberhck
Copy link
Contributor

@cyberhck cyberhck commented Oct 9, 2018

closes #392

@msftclas
Copy link

msftclas commented Oct 9, 2018

CLA assistant check
All CLA requirements met.

if (!(this.allowedDecorators.length > 0 && node.decorators && node.decorators.length > 0)) {
return false;
}
const bindingDecorators = node.decorators.find((decorator) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

some can be used instead of find.
Then conversion to boolean can be omitted later.

@@ -54,6 +69,9 @@ class ReactThisBindingIssueRuleWalker extends ErrorTolerantWalker {
if (typeof(opt) === 'object') {
this.allowAnonymousListeners = opt['allow-anonymous-listeners'] === true;
}
if (typeof opt === 'string') {
Copy link
Contributor

@IllusionMH IllusionMH Oct 9, 2018

Choose a reason for hiding this comment

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

IMHO, it would be better to add "speaking" property to object, instead of listing all passed strings as decorators. So setup will be like

"react-this-binding-issue": [true, {
    "allow-anonymous-listeners": true,
    "bind-decorators": ["autobind", "custom-bind"]
}]

instead of

"react-this-binding-issue": [true, "autobind", "customBind", { "allow-anonymous-listeners": true}]

But it's up to maintainers.

Copy link
Contributor Author

@cyberhck cyberhck Oct 9, 2018

Choose a reason for hiding this comment

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

This is something I didn't find much examples of or documentation, how do I write a schema? because options is of type any

is the current one correct? tests are passing, but I'm more interested in options schema.

Copy link
Contributor

@IllusionMH IllusionMH Oct 9, 2018

Choose a reason for hiding this comment

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

AFAIK schema is used for validation/suggestions inside of tslint.json file, and not used to type options values inside rules/walkers.

For example you can check schema for rule in core TSLint repo: schema in docs and in code

this.scope = new Scope(null);
super.visitMethodDeclaration(node);
this.scope = null;
}

private isMethodBoundWithDecorators(node: ts.MethodDeclaration): boolean {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

instead of using this.allowedDecorators, if we pass it to this function, then it'd become pure, will change.

@cyberhck
Copy link
Contributor Author

cyberhck commented Oct 9, 2018

@IllusionMH can you verify if everything is correct? Specially the schema part :)

@@ -53,6 +71,14 @@ class ReactThisBindingIssueRuleWalker extends ErrorTolerantWalker {
this.getOptions().forEach((opt: any) => {
if (typeof(opt) === 'object') {
this.allowAnonymousListeners = opt['allow-anonymous-listeners'] === true;
if (opt['bind-decorators']) {
const allowedDecorators: any[] = opt['bind-decorators'];
if (allowedDecorators.constructor !== Array && allowedDecorators.some((decorator) => typeof decorator !== 'string')) {
Copy link
Contributor

@IllusionMH IllusionMH Oct 9, 2018

Choose a reason for hiding this comment

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

Array.isArray should be more readable/convenient.
|| makes more sense here. Because second check should be performed only if opt['bind-decorators'] is an array (otherwise it will be runtime error)

@IllusionMH
Copy link
Contributor

@cyberhck at the first glance scheme looks good and I will check it more closely later.

As for experimentalDecorators - it might be enabled for a single rule later in scope of #489 . Will see if it is possible later this week.

And it is still up to maintainers for make final review and approve everything.

P.S. You can also check Hacktoberfest announcement if you haven't see it already 😃

Copy link

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Generally looks good but for some syntax errors in the test.

I'm noticing now that it'd be a bit more efficient to use Sets to store collected decorators, listeners, etc. in the class. Will file a separate issue for that.

return <div
onClick={this.listener1}>
onMouseOver={this.listener2}>
onMouseDown={this.listener3}>

Choose a reason for hiding this comment

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

You've got a few syntax errors here in the test: > should only come after the last attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, my bad, I copied from existing one and I didn't notice that for some reason 😄 , I'll fix the original file as well :)

@cyberhck
Copy link
Contributor Author

I just rebased, merged, can you please check? I did sign up for hacktoberfest before starting on this :)

if (opt['bind-decorators']) {
const allowedDecorators: any[] = opt['bind-decorators'];
if (
!Array.isArray(allowedDecorators.constructor)
Copy link
Contributor

Choose a reason for hiding this comment

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

Check should be for allowedDecorators not it's constructor.

Error below can be rephrase to mention that bind-decorators should be a list of strings, because now it will fail if string is passed, and not an array.

Most probably that's why test fail now, and removed string can be added back after fix here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahh, I missed the allowedDecorators.constructor part for some reason, my bad, will fix after I wake up now.

@IllusionMH
Copy link
Contributor

👍
No more questions/comments from my side.

@cyberhck
Copy link
Contributor Author

cyberhck commented Oct 13, 2018

Hey maintainers, is there anything I can do to make it better? or get merged? I kinda want this on another project 🙂

@JoshuaKGoldberg
Copy link

Heya @cyberhck sorry for the delay! Looking now.

Copy link

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

👍 great!

@JoshuaKGoldberg JoshuaKGoldberg merged commit 95d4693 into microsoft:master Oct 14, 2018
@cyberhck cyberhck deleted the 392-react-bind-issue branch October 15, 2018 10:05
@cyberhck
Copy link
Contributor Author

No problem, thanks for merging :) (a t-shirt awaits)

@cyberhck
Copy link
Contributor Author

cyberhck commented Feb 7, 2019

I can't believe we missed this: #813

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add detection for @bind decorators in react-this-binding
4 participants