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: allowVoid option in array-callback-return #17564

Merged
merged 4 commits into from Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/rules/array-callback-return.js
Expand Up @@ -162,6 +162,10 @@ module.exports = {
checkForEach: {
type: "boolean",
default: false
},
allowVoid: {
type: "boolean",
default: false
}
},
additionalProperties: false
Expand All @@ -178,7 +182,7 @@ module.exports = {

create(context) {

const options = context.options[0] || { allowImplicit: false, checkForEach: false };
const options = context.options[0] || { allowImplicit: false, checkForEach: false, allowVoid: false };
const sourceCode = context.sourceCode;

let funcInfo = {
Expand Down Expand Up @@ -211,6 +215,14 @@ module.exports = {
if (options.checkForEach && node.type === "ArrowFunctionExpression" && node.expression) {
messageId = "expectedNoReturnValue";
}

if (options.checkForEach &&
options.allowVoid &&
node.type === "ArrowFunctionExpression" &&
node.body.type === "UnaryExpression" &&
node.body.operator === "void") {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we can make this a bit easier to follow by pulling the common checks up a level.

Suggested change
if (options.checkForEach && node.type === "ArrowFunctionExpression" && node.expression) {
messageId = "expectedNoReturnValue";
}
if (options.checkForEach &&
options.allowVoid &&
node.type === "ArrowFunctionExpression" &&
node.body.type === "UnaryExpression" &&
node.body.operator === "void") {
return;
}
if (options.checkForEach && node.type === "ArrowFunctionExpression") {
if (node.expression) {
messageId = "expectedNoReturnValue";
}
if (options.allowVoid &&
node.body.type === "UnaryExpression" &&
node.body.operator === "void") {
return;
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!
I think node.expression is true for both the conditions

} else {
if (node.body.type === "BlockStatement" && isAnySegmentReachable(funcInfo.currentSegments)) {
messageId = funcInfo.hasReturn ? "expectedAtEnd" : "expectedInside";
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/array-callback-return.js
Expand Up @@ -24,6 +24,8 @@ const checkForEachOptions = [{ checkForEach: true }];

const allowImplicitCheckForEach = [{ allowImplicit: true, checkForEach: true }];

const checkForEachAllowVoid = [{ checkForEach: true, allowVoid: true }];

ruleTester.run("array-callback-return", rule, {
valid: [

Expand Down Expand Up @@ -114,6 +116,10 @@ ruleTester.run("array-callback-return", rule, {
{ code: "foo.every(function() { try { bar(); } finally { return 1; } })", options: checkForEachOptions },
{ code: "foo.every(function() { return; })", options: allowImplicitCheckForEach },

// options: { checkForEach: true, allowVoid: true }
{ code: "foo.forEach((x) => void x)", options: checkForEachAllowVoid, parserOptions: { ecmaVersion: 6 } },
{ code: "foo.forEach((x) => void bar(x))", options: checkForEachAllowVoid, parserOptions: { ecmaVersion: 6 } },

"Arrow.from(x, function() {})",
"foo.abc(function() {})",
"every(function() {})",
Expand Down Expand Up @@ -217,6 +223,8 @@ ruleTester.run("array-callback-return", rule, {
{ code: "foo.filter(function foo() {})", options: checkForEachOptions, errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.filter" } }] },
{ code: "foo.filter(function foo() { return; })", options: checkForEachOptions, errors: [{ messageId: "expectedReturnValue", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.filter" } }] },
{ code: "foo.every(cb || function() {})", options: checkForEachOptions, errors: ["Array.prototype.every() expects a return value from function."] },
{ code: "foo.forEach(() => void x)", options: checkForEachOptions, parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "expectedNoReturnValue" }] },
{ code: "foo.forEach(() => void bar(x))", options: checkForEachOptions, parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "expectedNoReturnValue" }] },

// full location tests
{
Expand Down