Skip to content

Commit

Permalink
New: Add allowImplicit option to array-callback-return (fixes eslint#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescdavis committed Sep 23, 2017
1 parent 2ff6fb6 commit c581d1d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
15 changes: 15 additions & 0 deletions docs/rules/array-callback-return.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ var foo = Array.from(nodes, function(node) {
var bar = foo.map(node => node.getAttribute("id"));
```

## Options

This rule has an object option:

* `"allowImplicit": false` (default) disallows implicitly returning undefined with a return; statement.

Examples of **correct** code for the `{ "allowImplicit": true }` option:

```js
/*eslint array-callback-return: ["error", { allowImplicit: true }]*/
var undefAllTheThings = myArray.map(function(item) {
return;
});
```

## Known Limitations

This rule checks callback functions of methods with the given names, *even if* the object which has the method is *not* an array.
Expand Down
18 changes: 16 additions & 2 deletions lib/rules/array-callback-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,23 @@ module.exports = {
recommended: false
},

schema: []
schema: [
{
type: "object",
properties: {
allowImplicit: {
type: "boolean"
}
},
additionalProperties: false
}
]
},

create(context) {

const options = context.options[0] || { allowImplicit: false };

let funcInfo = {
upper: null,
codePath: null,
Expand Down Expand Up @@ -208,7 +221,8 @@ module.exports = {
if (funcInfo.shouldCheck) {
funcInfo.hasReturn = true;

if (!node.argument) {
// if allowImplicit: false, should also check node.argument
if (!options.allowImplicit && !node.argument) {
context.report({
node,
message: "{{name}} expected a return value.",
Expand Down
33 changes: 33 additions & 0 deletions tests/lib/rules/array-callback-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,22 @@ const rule = require("../../../lib/rules/array-callback-return"),

const ruleTester = new RuleTester();

const options = [{ allowImplicit: true }];

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

// options: { allowImplicit: false }
"Array.from(x, function() { return true; })",
"Int32Array.from(x, function() { return true; })",

// options: { allowImplicit: true }
{ code: "Array.from(x, function() { return; })", options },
{ code: "Int32Array.from(x, function() { return; })", options },

"Arrow.from(x, function() {})",

// options: { allowImplicit: false }
"foo.every(function() { return true; })",
"foo.filter(function() { return true; })",
"foo.find(function() { return true; })",
Expand All @@ -28,17 +39,39 @@ ruleTester.run("array-callback-return", rule, {
"foo.reduceRight(function() { return true; })",
"foo.some(function() { return true; })",
"foo.sort(function() { return 0; })",

// options: { allowImplicit: true }
{ code: "foo.every(function() { return; })", options },
{ code: "foo.filter(function() { return; })", options },
{ code: "foo.find(function() { return; })", options },
{ code: "foo.findIndex(function() { return; })", options },
{ code: "foo.map(function() { return; })", options },
{ code: "foo.reduce(function() { return; })", options },
{ code: "foo.reduceRight(function() { return; })", options },
{ code: "foo.some(function() { return; })", options },
{ code: "foo.sort(function() { return; })", options },

"foo.abc(function() {})",
"every(function() {})",
"foo[every](function() {})",
"var every = function() {}",
{ code: "foo[`${every}`](function() {})", parserOptions: { ecmaVersion: 6 } },
{ code: "foo.every(() => true)", parserOptions: { ecmaVersion: 6 } },

// options: { allowImplicit: false }
{ code: "foo.every(() => { return true; })", parserOptions: { ecmaVersion: 6 } },
"foo.every(function() { if (a) return true; else return false; })",
"foo.every(function() { switch (a) { case 0: bar(); default: return true; } })",
"foo.every(function() { try { bar(); return true; } catch (err) { return false; } })",
"foo.every(function() { try { bar(); } finally { return true; } })",

// options: { allowImplicit: true }
{ code: "foo.every(() => { return; })", options, parserOptions: { ecmaVersion: 6 } },
{ code: "foo.every(function() { if (a) return; else return; })", options },
{ code: "foo.every(function() { switch (a) { case 0: bar(); default: return; } })", options },
{ code: "foo.every(function() { try { bar(); return; } catch (err) { return; } })", options },
{ code: "foo.every(function() { try { bar(); } finally { return; } })", options },

"foo.every(function(){}())",
"foo.every(function(){ return function() { return true; }; }())",
"foo.every(function(){ return function() { return; }; })",
Expand Down

0 comments on commit c581d1d

Please sign in to comment.