Skip to content

Commit

Permalink
Merge pull request #1504 from eslint/issue1493
Browse files Browse the repository at this point in the history
Fix: Make consistent-return work with arrow functions (fixes #1493)
  • Loading branch information
nzakas committed Nov 26, 2014
2 parents b26c4e2 + e99ad62 commit 94231b4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/rules/consistent-return.js
Expand Up @@ -16,10 +16,21 @@ module.exports = function(context) {
// Helpers
//--------------------------------------------------------------------------

/**
* Marks entrance into a function by pushing a new object onto the functions
* stack.
* @returns {void}
* @private
*/
function enterFunction() {
functions.push({});
}

/**
* Marks exit of a function by popping off the functions stack.
* @returns {void}
* @private
*/
function exitFunction() {
functions.pop();
}
Expand All @@ -33,8 +44,10 @@ module.exports = function(context) {

"FunctionDeclaration": enterFunction,
"FunctionExpression": enterFunction,
"ArrowFunctionExpression": enterFunction,
"FunctionDeclaration:exit": exitFunction,
"FunctionExpression:exit": exitFunction,
"ArrowFunctionExpression:exit": exitFunction,

"ReturnStatement": function(node) {

Expand Down
23 changes: 22 additions & 1 deletion tests/lib/rules/consistent-return.js
Expand Up @@ -24,7 +24,8 @@ eslintTester.addRuleTest("lib/rules/consistent-return", {
"f(function() { if (true) return; else return; })",
"f(function() { if (true) return true; else return false; })",
"function foo() { function bar() { return true; } return; }",
"function foo() { function bar() { return; } return false; }"
"function foo() { function bar() { return; } return false; }",
{ code: "var x = () => { return {}; };", settings: { ecmascript: 6 } }
],

invalid: [
Expand All @@ -37,6 +38,16 @@ eslintTester.addRuleTest("lib/rules/consistent-return", {
}
]
},
{
code: "var foo = () => { if (true) return true; else return; }",
settings: { ecmascript: 6 },
errors: [
{
message: "Expected a return value.",
type: "ReturnStatement"
}
]
},
{
code: "function foo() { if (true) return; else return false; }",
errors: [
Expand All @@ -63,6 +74,16 @@ eslintTester.addRuleTest("lib/rules/consistent-return", {
type: "ReturnStatement"
}
]
},
{
code: "f(a => { if (true) return; else return false; })",
settings: { ecmascript: 6 },
errors: [
{
message: "Expected no return value.",
type: "ReturnStatement"
}
]
}
]
});

0 comments on commit 94231b4

Please sign in to comment.