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

tools: enable array-callback-return ESLint rule #17858

Closed
wants to merge 3 commits into from

Conversation

Trott
Copy link
Member

@Trott Trott commented Dec 25, 2017

For array methods that depend on a callback (such as .filter() or
.map()), require a return value from the callback.

First two commits fix a few issues in our code base that would be flagged by this rule.

Third commit enables the rule itself.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

tools readline test

@Trott Trott added readline Issues and PRs related to the built-in readline module. test Issues and PRs related to the tests. tools Issues and PRs related to the tools directory. labels Dec 25, 2017
@nodejs-github-bot nodejs-github-bot added readline Issues and PRs related to the built-in readline module. tools Issues and PRs related to the tools directory. labels Dec 25, 2017
lib/readline.js Outdated
@@ -501,7 +501,7 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {

// If there is a common prefix to all matches, then apply that portion.
var f = completions.filter(function completionFilter(e) {
Copy link
Member

Choose a reason for hiding this comment

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

this could be reduced to completions.filter(e => e).

Copy link
Member Author

@Trott Trott Dec 25, 2017

Choose a reason for hiding this comment

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

👍

I was being cautious. MDN indicates that the callback should return true or false and makes no mention of truthy or falsy values. But now I've looked at the ECMA-262 spec and it makes it explicit that the callback may return a value that is coercible to a Boolean. I'll change it to your suggestion to minimize the unnecessary changes here.

Copy link
Member Author

Choose a reason for hiding this comment

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

(Might not bother with the change to arrow function just to keep the diff as small as possible. I assume there's no strong preference there.)

Copy link
Member

Choose a reason for hiding this comment

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

I’d go for it. +1 -3 vs. +1 -1 are both really small, and I don’t think this line is interesting enough for people to use git blame on it either way.

lib/readline.js Outdated
@@ -501,7 +501,7 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {

// If there is a common prefix to all matches, then apply that portion.
var f = completions.filter(function completionFilter(e) {
if (e) return e;
return !!e;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: using Boolean(e) here might be more readable

Use construct that always returns a boolean for `filter()` callback.
Two tests were using Array.prototype.map() without returning values in
the callback. In other words, they were using it where a `.forEach()`
was called for. Change to `.forEach()`.
For array methods that depend on a callback (such as `.filter()` or
`.map()`), require a return value from the callback.
@Trott
Copy link
Member Author

Trott commented Dec 27, 2017

@jasnell
Copy link
Member

jasnell commented Dec 28, 2017

CI failures look unrelated.

jasnell pushed a commit that referenced this pull request Dec 28, 2017
Use construct that always returns a boolean for `filter()` callback.

PR-URL: #17858
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
jasnell pushed a commit that referenced this pull request Dec 28, 2017
Two tests were using Array.prototype.map() without returning values in
the callback. In other words, they were using it where a `.forEach()`
was called for. Change to `.forEach()`.

PR-URL: #17858
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
jasnell pushed a commit that referenced this pull request Dec 28, 2017
For array methods that depend on a callback (such as `.filter()` or
`.map()`), require a return value from the callback.

PR-URL: #17858
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@jasnell
Copy link
Member

jasnell commented Dec 28, 2017

Landed in 5c7af90, fe8a5aa, and a3535f3

@jasnell jasnell closed this Dec 28, 2017
MylesBorins pushed a commit that referenced this pull request Jan 8, 2018
Use construct that always returns a boolean for `filter()` callback.

PR-URL: #17858
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request Jan 8, 2018
Two tests were using Array.prototype.map() without returning values in
the callback. In other words, they were using it where a `.forEach()`
was called for. Change to `.forEach()`.

PR-URL: #17858
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request Jan 8, 2018
For array methods that depend on a callback (such as `.filter()` or
`.map()`), require a return value from the callback.

PR-URL: #17858
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request Jan 9, 2018
Use construct that always returns a boolean for `filter()` callback.

PR-URL: #17858
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request Jan 9, 2018
Two tests were using Array.prototype.map() without returning values in
the callback. In other words, they were using it where a `.forEach()`
was called for. Change to `.forEach()`.

PR-URL: #17858
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request Jan 9, 2018
For array methods that depend on a callback (such as `.filter()` or
`.map()`), require a return value from the callback.

PR-URL: #17858
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request Jan 9, 2018
Use construct that always returns a boolean for `filter()` callback.

PR-URL: #17858
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request Jan 9, 2018
Two tests were using Array.prototype.map() without returning values in
the callback. In other words, they were using it where a `.forEach()`
was called for. Change to `.forEach()`.

PR-URL: #17858
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request Jan 9, 2018
For array methods that depend on a callback (such as `.filter()` or
`.map()`), require a return value from the callback.

PR-URL: #17858
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Jan 10, 2018
@gibfahn
Copy link
Member

gibfahn commented Jan 24, 2018

Assuming this doesn't need to be backported, raise a PR if you disagree.

@Trott Trott deleted the return-values branch January 13, 2022 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
readline Issues and PRs related to the built-in readline module. test Issues and PRs related to the tests. tools Issues and PRs related to the tools directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants