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

Update: array-callback-return checks Array.forEach (fixes #12551) #12646

Merged
merged 1 commit into from Feb 4, 2020

Conversation

gabrieldrs
Copy link
Contributor

@gabrieldrs gabrieldrs commented Dec 5, 2019

What is the purpose of this pull request? (put an "X" next to item)
[x] Changes an existing rule

What rule do you want to change?
array-callback-return

Does this change cause the rule to produce more or fewer warnings?
more

How will the change be implemented? (New option, new default behavior, etc.)?
new option checkForEach created, defaulted to false. when true, rule will also check if Array.forEach callback has a return statement and warn if it does

Please provide some example code that this change will affect:

Examples of incorrect code for the { "checkForEach": true } option:

/*eslint array-callback-return: ["error", { checkForEach: true }]*/
myArray.forEach(function(item) {
    return handleItem(item)
});

Examples of correct code for the { "checkForEach": true } option:

/*eslint array-callback-return: ["error", { checkForEach: true }]*/
myArray.forEach(function(item) {
    handleItem(item)
});
myArray.forEach(function(item) {
    if (item < 0) {
        return;
    }
    handleItem(item);
});

What does the rule currently do for this code?

doesn't check

What will the rule do after it's changed?

check if Array.forEach callback has a return statement and warn if it does

What changes did you make? (Give an overview)

added checkForEach option which enables/disables the new rule check

Is there anything you'd like reviewers to focus on?

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Dec 5, 2019
@gabrieldrs
Copy link
Contributor Author

updated PR description with required information

@kaicataldo kaicataldo added accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Dec 7, 2019
Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

@gabrieldrs thanks for the PR! Left a couple of notes where I believe we should change the approach.

In general, this option should report any return value from forEach. I believe it should also report implicit returns from arrow functions.

Fill free to ask if you have any questions!

docs/rules/array-callback-return.md Outdated Show resolved Hide resolved
tests/lib/rules/array-callback-return.js Outdated Show resolved Hide resolved
lib/rules/array-callback-return.js Outdated Show resolved Hide resolved
lib/rules/array-callback-return.js Outdated Show resolved Hide resolved
Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

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

Can we document/test how the two options interact? Or should they be mutually exclusive? i.e. what happens when the rule is configured as { checkForEach: true, allowImplicit: false }?

docs/rules/array-callback-return.md Outdated Show resolved Hide resolved
docs/rules/array-callback-return.md Outdated Show resolved Hide resolved
@gabrieldrs
Copy link
Contributor Author

gabrieldrs commented Dec 10, 2019

I think they shouldn't be mutually exclusive, and, at the same time, one should not effect the other, in the sense that setting allowImplicit should not change the behaviour of checkForEach, and vice versa.

I will create tests with both options set to ensure this.

@kaicataldo
Copy link
Member

@gabrieldrs That makes sense to me! I think we'll just need to update the documentation for allowImplicit to clarify which Array.prototype methods it applies to.

@mdjermanovic
Copy link
Member

mdjermanovic commented Dec 10, 2019

That makes sense to me, too.

I think that this rule shouldn't disallow return; in forEach callback. That kind of restriction might require the user to make a serious refactoring. It might be a separate generic rule to allow return only at the end of functions (though, I'm not sure how popular that style is).

Also, "allowImplicit": true shouldn't allow x => x arrow return in forEach (this also looks like something 'implicit'). This might be an additional option in the future.

lib/rules/array-callback-return.js Outdated Show resolved Hide resolved
lib/rules/array-callback-return.js Outdated Show resolved Hide resolved
lib/rules/array-callback-return.js Outdated Show resolved Hide resolved
@gabrieldrs gabrieldrs force-pushed the array-callback-return branch 6 times, most recently from ed33f8e to 8b3dd07 Compare January 2, 2020 00:11
Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

Thanks for the changes, this looks good in general! I left some notes, mostly about the documentation and tests.

lib/rules/array-callback-return.js Outdated Show resolved Hide resolved
lib/rules/array-callback-return.js Outdated Show resolved Hide resolved
docs/rules/array-callback-return.md Outdated Show resolved Hide resolved
tests/lib/rules/array-callback-return.js Outdated Show resolved Hide resolved
docs/rules/array-callback-return.md Outdated Show resolved Hide resolved
docs/rules/array-callback-return.md Outdated Show resolved Hide resolved
@aladdin-add
Copy link
Member

IMHO, no need to add the option, just make it as default behavior -- a breaking change in ESLint v7. thoughts? @mdjermanovic @kaicataldo

@ljharb
Copy link
Sponsor Contributor

ljharb commented Jan 14, 2020

It's always better imo to add an option in a minor, and then flip the default in a major - that also gives users a super easy way to bypass the breakages and more easily upgrade.

@aladdin-add
Copy link
Member

emm...yes, a tradeoff! the dark side is: it added complexity, but I didn't see how it's useful -- returning a value in Array.forEach is mostly a mistake.

@ljharb
Copy link
Sponsor Contributor

ljharb commented Jan 14, 2020

There are many people who intentionally omit the curly braces in forEach, knowing the return value is ignored, and would not want warnings there. I'm not one of those people, nor hopefully is anyone touching any code I have to work with, but these people do exist :-)

@mdjermanovic
Copy link
Member

If someone doesn't want this for some reason (e.g., because of implicit arrow functions or something else may appear), the only choice for them would be to completely turn off the rule and thus lose the current default behavior.

So it might be safe to start with an option and see from the feedback is there a need for additional options to tweak this option (i.e. options of the option), and those options would stay when we eventually remove this one.

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

Looks great! Left just a couple of notes.

docs/rules/array-callback-return.md Outdated Show resolved Hide resolved
docs/rules/array-callback-return.md Outdated Show resolved Hide resolved
docs/rules/array-callback-return.md Outdated Show resolved Hide resolved
tests/lib/rules/array-callback-return.js Show resolved Hide resolved
tests/lib/rules/array-callback-return.js Show resolved Hide resolved
tests/lib/rules/array-callback-return.js Show resolved Hide resolved
Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

Just a couple of minor details, otherwise this looks really great!

lib/rules/array-callback-return.js Outdated Show resolved Hide resolved
docs/rules/array-callback-return.md Outdated Show resolved Hide resolved
array-callback-return rule now checks if Array.forEach returns a value, and, if it does, reports an error.
This feature is being added disabled by default, and can be enabled by setting the option "checkForEach" to true.

Signed-off-by: Gabriel R Sezefredo <g@briel.dev>
Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

LGTM! All details are covered well (and that includes details from the previous behavior since this change required a redesign of the existing code).

Thanks for the great work, @gabrieldrs!

Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for sticking with all the feedback!

@kaicataldo kaicataldo merged commit 439c833 into eslint:master Feb 4, 2020
@kaicataldo
Copy link
Member

Thanks for contributing to ESLint!

montmanu pushed a commit to montmanu/eslint that referenced this pull request Mar 4, 2020
… (eslint#12646)

array-callback-return rule now checks if Array.forEach returns a value, and, if it does, reports an error.
This feature is being added disabled by default, and can be enabled by setting the option "checkForEach" to true.

Signed-off-by: Gabriel R Sezefredo <g@briel.dev>
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Aug 4, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Aug 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants