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

feat(always-return): add ignoreAssignmentVariable option #518

Merged
merged 7 commits into from
Oct 16, 2024

Conversation

gurgunday
Copy link
Contributor

@gurgunday gurgunday commented Jul 26, 2024

Adds ignoreAssignmentVariable as discussed in #518 (comment)

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

PR Compliance Checks Passed!

@gurgunday gurgunday changed the title turn ignorelastcallback to true chore: turn ignorelastcallback to true Jul 26, 2024
@gurgunday gurgunday changed the title chore: turn ignorelastcallback to true chore: always-return, enable ignoreLastCallback by default Jul 26, 2024
@brettz9
Copy link
Member

brettz9 commented Jul 28, 2024

I could still be persuaded (or overruled), but I'm personally a little wary of such a change as this.

I think it's safer for the default to annoy rather than accidentally avoid warnings. In the case of libraries at least, one typically does want to always return.

@gurgunday
Copy link
Contributor Author

I’d simply argue that, since this plugin is not eslint-plugin-n (and therefore not node specific), we should definitely take into account browser usage where these non blocking async globalThis assignments are common

I think the eslint-community plugins’ recommended presets should only error for usages that are incorrect ~100% of the time

For instance the chained .then usage where a .then in the middle does not return anything is most definitely not a correct usage, but for the last one we can argue cases like these, so it’d be more consistent to relax this to not fail here or maybe just warn

@aladdin-add
Copy link
Contributor

just want to point out: it should be a breaking change to change a rule's default option.

@gurgunday
Copy link
Contributor Author

Yeah I wish I'd opened the PR earlier to maybe target the v7 release

@scagood
Copy link
Contributor

scagood commented Jul 31, 2024

I am not sure I agree here, as enabling ignoreLastCallback will void the point of the check in a lot of cases. 🤔

In fact I would prefer to remove the rule from the recommended config, than making this change.


A different option could be allowing assignment to specific variables?
eg ignoreAssignmentVariable: ["global.window"]

Would this work for you?

@gurgunday
Copy link
Contributor Author

A different option could be allowing assignment to specific variables?

This makes a lot of sense and I didn’t know it was possible! Let me take a look

@gurgunday gurgunday marked this pull request as draft August 4, 2024 21:41
@gurgunday gurgunday changed the title chore: always-return, enable ignoreLastCallback by default feat: ignoreAssignmentVariable Aug 12, 2024
@gurgunday gurgunday marked this pull request as ready for review August 12, 2024 05:56
@gurgunday
Copy link
Contributor Author

gurgunday commented Aug 12, 2024

@scagood, @brettz9 @aladdin-add, can you take a look?

Copy link
Member

@brettz9 brettz9 left a comment

Choose a reason for hiding this comment

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

Besides the one suggestion, LGTM...

rules/always-return.js Outdated Show resolved Hide resolved
Copy link
Contributor

@scagood scagood left a comment

Choose a reason for hiding this comment

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

I generally prefer this strategy I think!

rules/always-return.js Outdated Show resolved Hide resolved
rules/always-return.js Outdated Show resolved Hide resolved
rules/always-return.js Outdated Show resolved Hide resolved
@gurgunday
Copy link
Contributor Author

Hey everyone, so I made the option recursive, which means it allows for assignments to nested properties like window.a.b or window["test"][0]

I had to use string manipulation since I'm not familiar with how else to achieve this, but it looks sound to me as a property can either be computed or not, and we take both into account

Copy link

codecov bot commented Aug 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (bbcfcbf) to head (064637a).
Report is 63 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #518   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           25        26    +1     
  Lines          649       719   +70     
  Branches       250       278   +28     
=========================================
+ Hits           649       719   +70     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gurgunday
Copy link
Contributor Author

PTAL @brettz9 @scagood

Copy link
Contributor

@scagood scagood left a comment

Choose a reason for hiding this comment

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

I would love to see some more tests, other than that, I think this is starting to take shape 💪

__tests__/always-return.js Outdated Show resolved Hide resolved
Copy link
Member

@brettz9 brettz9 left a comment

Choose a reason for hiding this comment

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

LGTM...

Just as an idea for a future PR, it would seem to me a possibly even more common use browser case to allow node appending in the last promise, e.g.:

prom.then((newInfo) => {
  document.body.append(toDom(newInfo));
})

But regardless, this PR LGTM...

@gurgunday
Copy link
Contributor Author

gurgunday commented Sep 6, 2024

Okay, so #518 (review) made a good point - we need to take into account both variations (computed or not computed) for each level of the passed variable names

For instance, if ignoreAssignmentVar: ["window.a.b"] is passed, assignments to window.a.b window.['a']['b'] and window.a['b'] should all pass; and the passed variable name itself can have variations as well like ignoreAssignmentVar: ["window.['a']['b']", "window.a.b"]

It's not hard but it requires a little more complex logic or at least we need to determine how the config should behave

To keep the implementation correct, what I propose is that we start with 1 level of nesting in the config - e.g., just window or globalThis - and after discussing the details we make a follow-up PR to lift this restriction

@gurgunday
Copy link
Contributor Author

gurgunday commented Sep 8, 2024

PTAL with the current setting that only allows for simple var names

Copy link
Contributor

@scagood scagood left a comment

Choose a reason for hiding this comment

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

To keep the implementation correct, what I propose is that we start with 1 level of nesting in the config - e.g., just window or globalThis - and after discussing the details we make a follow-up PR to lift this restriction

I agree, I have left comments inline with that 😄

rules/always-return.js Outdated Show resolved Hide resolved
rules/always-return.js Outdated Show resolved Hide resolved
rules/always-return.js Outdated Show resolved Hide resolved
rules/always-return.js Outdated Show resolved Hide resolved
Copy link
Contributor

@scagood scagood left a comment

Choose a reason for hiding this comment

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

I like the shape of this now!
Thank you for the awesome docs too 😀

rules/always-return.js Outdated Show resolved Hide resolved
@scagood
Copy link
Contributor

scagood commented Sep 11, 2024

@brettz9 Do you want to take another look at this PR?

@brettz9
Copy link
Member

brettz9 commented Sep 11, 2024

Sorry, is anyone else available? I've come into other work which is keeping me busy...

@scagood scagood requested a review from a team September 11, 2024 08:09
@gurgunday
Copy link
Contributor Author

Hey everyone, just checking in to see if we can get this merged soon

Doing a lot of webdev these days and I keep bumping into this 🤣

@MichaelDeBoey MichaelDeBoey changed the title feat: ignoreAssignmentVariable feat(always-return): add ignoreAssignmentVariable option Oct 9, 2024
rules/always-return.js Outdated Show resolved Hide resolved
@brettz9 brettz9 merged commit 701279c into eslint-community:main Oct 16, 2024
11 checks passed
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.

5 participants