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
Update: allow continue instead of if wrap in guard-for-in (fixes #7567) #9796
Conversation
Could this be behind an option? I want to preserve the current behavior (disallowing continue statements). |
@ljharb I don't think that'd be appropriate here. This rule is not stylistic. It intends to protect you from a potential hazard, and either pattern is safe. |
I agree this should probably be behind an option. Let's discuss in the issue. |
Since the rule isn't checking what the guard is doing - ie, it's not enforcing a hasOwnProperty check - it's not actually protecting against a hazard. I've seen plenty of people accidentally bypass this rule by checking something besides hasOwnProperty. |
I agree @ljharb, but I think that should be discussed as a separate issue.
This is a rule that came from JSHint originally, and they had the same
limitation there. (We could certainly improve the rule to actually be
useful in this regard! I just wanted to explain the historical reason for
its current state.)
…On Jan 1, 2018 9:50 PM, "Jordan Harband" ***@***.***> wrote:
Since the rule isn't checking *what* the guard is doing - ie, it's not
enforcing a hasOwnProperty check - it's not actually protecting against a
hazard. I've seen plenty of people accidentally bypass this rule by
checking something besides hasOwnProperty.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#9796 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AARWenqMRIIhuYsBhxNw9Dq_pSYQr1t4ks5tGadtgaJpZM4RQOcf>
.
|
@platinumazure |
@ljharb Could you use |
@platinumazure if we want to ban |
I still think that an option here is inappropriate for the reasons I mentioned above. This rule is listed in the Best Practices section, not the Stylistic Issues section. Using it to enforce a particular style is an abuse of this rule. And without an option, it still always protects you from the possible hazard. |
I do agree with @michaelficarra here. This rule is in a different category then things like |
"styleguide use" covers all categories of rules; my issue with adding it without an option is that i want the entire body of the for loop to be surrounded by the same single condition, and i want the extra nesting, so that the existence of the condition is clear. It is not a "best practice" to early-continue a for loop under a guard (nor is it a bad practice). In fact, it's simply @michaelficarra's preferred style here. In terms of protecting against a hazard, this rule does not actually protect against the hazard - it simply requires any condition.
The unexpected behavior is when you encounter an inherited property; filtering the results isn't a generic solution to that problem. |
I don't see why we couldn't add an option here-- after all, @michaelficarra could certainly use the option in his own setup. Can someone convince me why adding an option would be a bad idea? |
It makes the rule slightly more complicated for users to configure, and also slightly more complicated for us to maintain. Additionally, the option seems to be tangential to the main purpose of the rule. In this case I don't think it's worthwhile. |
@ljharb I'd love for the rule to be stricter about the condition of the guard, but I think that change is inappropriate for this PR. Also, "best practice" (probably not the best term) is defined on that page as
and I believe that both approaches allowed by this rule accomplish that goal. edit: To emphasise my point, if there was a third way to reliably and generally protect against the enumerable prototype properties hazard, I would want this rule to allow that pattern as well, even if I did not personally plan to use that pattern. |
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.
Looks good to me, thanks!
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, thanks for contributing!
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[X] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What changes did you make? (Give an overview)
Is there anything you'd like reviewers to focus on?