Skip to content

Commit

Permalink
Fix: newline-before-return shouldn't disallow newlines (fixes #6176) (
Browse files Browse the repository at this point in the history
  • Loading branch information
alberto authored and nzakas committed May 23, 2016
1 parent d4f5526 commit ee0cd58
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 154 deletions.
36 changes: 18 additions & 18 deletions docs/rules/newline-before-return.md
Expand Up @@ -38,31 +38,13 @@ Examples of **incorrect** code for this rule:
```js
/*eslint newline-before-return: "error"*/

function foo() {

return;
}

function foo(bar) {
if (!bar) {

return;
}
}

function foo(bar) {
if (!bar) {
return;
}
return bar;
}

function foo() {

// comment
return;
}

function foo(bar) {
if (!bar) {
return;
Expand All @@ -82,6 +64,11 @@ function foo() {
return;
}

function foo() {

return;
}

function foo(bar) {
if (!bar) return;
}
Expand All @@ -103,6 +90,19 @@ function foo(bar) {

return bar;
}

function foo(bar) {
if (!bar) {

return;
}
}

function foo() {

// comment
return;
}
```

## When Not To Use It
Expand Down
25 changes: 5 additions & 20 deletions lib/rules/newline-before-return.js
Expand Up @@ -133,32 +133,17 @@ module.exports = {
return (lineNumNode - lineNumTokenBefore - commentLines) > 1;
}

/**
* Reports expected/unexpected newline before return statement
* @param {ASTNode} node - the node to report in the event of an error
* @param {boolean} isExpected - whether the newline is expected or not
* @returns {void}
* @private
*/
function reportError(node, isExpected) {
var expected = isExpected ? "Expected" : "Unexpected";

context.report({
node: node,
message: expected + " newline before return statement."
});
}

//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------

return {
ReturnStatement: function(node) {
if (isFirstNode(node) && hasNewlineBefore(node)) {
reportError(node, false);
} else if (!isFirstNode(node) && !hasNewlineBefore(node)) {
reportError(node, true);
if (!isFirstNode(node) && !hasNewlineBefore(node)) {
context.report({
node: node,
message: "Expected newline before return statement."
});
}
}
};
Expand Down
155 changes: 39 additions & 116 deletions tests/lib/rules/newline-before-return.js
Expand Up @@ -20,12 +20,16 @@ var ruleTester = new RuleTester();
ruleTester.run("newline-before-return", rule, {
valid: [
"function a() {\nreturn;\n}",
"function a() {\n\nreturn;\n}",
"function a() {\nvar b;\n\nreturn;\n}",
"function a() {\nif (b) return;\n}",
"function a() {\nif (b) { return; }\n}",
"function a() {\nif (b) {\nreturn;\n}\n}",
"function a() {\nif (b) {\n\nreturn;\n}\n}",
"function a() {\nif (b) {\nreturn;\n}\n\nreturn c;\n}",
"function a() {\nif (b) {\n\nreturn;\n}\n\nreturn c;\n}",
"function a() {\nif (!b) {\nreturn;\n} else {\nreturn b;\n}\n}",
"function a() {\nif (!b) {\nreturn;\n} else {\n\nreturn b;\n}\n}",
"function a() {\nif (b) {\nreturn b;\n} else if (c) {\nreturn c;\n}\n}",
"function a() {\nif (b) {\nreturn b;\n} else if (c) {\nreturn c;\n} else {\nreturn d;\n}\n}",
"function a() {\nif (b) {\nreturn b;\n} else if (c) {\nreturn c;\n} else {\nreturn d;\n}\n\nreturn a;\n}",
Expand Down Expand Up @@ -54,13 +58,16 @@ ruleTester.run("newline-before-return", rule, {
{
code: "function a() {\nfor (b of c) return;\n}",
parserOptions: { ecmaVersion: 6 }
}, {
},
{
code: "function a() {\nfor (b of c)\nreturn;\n}",
parserOptions: { ecmaVersion: 6 }
}, {
},
{
code: "function a() {\nfor (b of c) {\nreturn;\n}\n}",
parserOptions: { ecmaVersion: 6 }
}, {
},
{
code: "function a() {\nfor (b of c) {\nd();\n\nreturn;\n}\n}",
parserOptions: { ecmaVersion: 6 }
},
Expand Down Expand Up @@ -101,147 +108,63 @@ ruleTester.run("newline-before-return", rule, {

invalid: [
{
code: "function a() {\n\nreturn;\n}",
errors: ["Unexpected newline before return statement."]
}, {
code: "function a() {\nvar b;\nreturn;\n}",
errors: ["Expected newline before return statement."]
}, {
code: "function a() {\nif (b) {\n\nreturn;\n}\n}",
errors: ["Unexpected newline before return statement."]
}, {
code: "function a() {\nif (b) {\n\nreturn;\n}\nreturn c;\n}",
errors: ["Unexpected newline before return statement.", "Expected newline before return statement."]
}, {
code: "function a() {\nif (!b) {\nreturn;\n} else {\n\nreturn b;\n}\n}",
errors: ["Unexpected newline before return statement."]
}, {
code: "function a() {\nif (b) {\nreturn b;\n} else if (c) {\n\nreturn c;\n}\n}",
errors: ["Unexpected newline before return statement."]
}, {
code: "function a() {\nif (b) {\nreturn b;\n} else if (c) {\n\nreturn c;\n} else {\n\nreturn d;\n}\n}",
errors: ["Unexpected newline before return statement.", "Unexpected newline before return statement."]
}, {
code: "function a() {\nif (b) {\n\nreturn b;\n} else {\nreturn d;\n}\nreturn a;\n}",
errors: ["Unexpected newline before return statement.", "Expected newline before return statement."]
}, {
code: "function a() {\nif (b) return b;\nelse if (c) return c;\nelse {\n\nreturn d;\n}\n}",
errors: ["Unexpected newline before return statement."]
}, {
},
{
code: "function a() {\nif (b) return b;\nelse if (c) return c;\nelse {\ne();\nreturn d;\n}\n}",
errors: ["Expected newline before return statement."]
}, {
code: "function a() {\n while (b) \n\nreturn;\n}",
errors: ["Unexpected newline before return statement."]
}, {
code: "function a() {\n while (b) {\n\nreturn;\n}\n}",
errors: ["Unexpected newline before return statement."]
}, {
},
{
code: "function a() {\n while (b) {\nc();\nreturn;\n}\n}",
errors: ["Expected newline before return statement."]
}, {
code: "function a() {\ndo \n\nreturn;\nwhile (b);\n}",
errors: ["Unexpected newline before return statement."]
}, {
code: "function a() {\ndo {\n\nreturn;\n} while (b);\n}",
errors: ["Unexpected newline before return statement."]
}, {
},
{
code: "function a() {\ndo {\nc();\nreturn;\n} while (b);\n}",
errors: ["Expected newline before return statement."]
}, {
code: "function a() {\nfor (var b; b < c; b++)\n\nreturn;\n}",
errors: ["Unexpected newline before return statement."]
}, {
code: "function a() {\nfor (var b; b < c; b++) {\n\nreturn;\n}\n}",
errors: ["Unexpected newline before return statement."]
}, {
},
{
code: "function a() {\nfor (var b; b < c; b++) {\nc();\nreturn;\n}\n}",
errors: ["Expected newline before return statement."]
}, {
code: "function a() {\nfor (b in c)\n\nreturn;\n}",
errors: ["Unexpected newline before return statement."]
}, {
code: "function a() {\nfor (b in c) {\n\nreturn;\n}\n}",
errors: ["Unexpected newline before return statement."]
}, {
},
{
code: "function a() {\nfor (b in c) {\nd();\nreturn;\n}\n}",
errors: ["Expected newline before return statement."]
}, {
code: "function a() {\nfor (b of c)\n\nreturn;\n}",
parserOptions: { ecmaVersion: 6 },
errors: ["Unexpected newline before return statement."]
}, {
code: "function a() {\nfor (b of c) {\n\nreturn;\n}\n}",
parserOptions: { ecmaVersion: 6 },
errors: ["Unexpected newline before return statement."]
}, {
},
{
code: "function a() {\nfor (b of c) {\nd();\nreturn;\n}\n}",
parserOptions: { ecmaVersion: 6 },
errors: ["Expected newline before return statement."]
}, {
code: "function a() {\nswitch (b) {\ncase 'b':\n\nreturn;\n}\n}",
errors: ["Unexpected newline before return statement."]
}, {
code: "function a() {\nswitch (b) {\ncase 'b': {\n\nreturn;\n}\n}\n}",
errors: ["Unexpected newline before return statement."]
}, {
code: "function a() {\n//comment\n\nreturn b;\n}",
errors: ["Unexpected newline before return statement."]
}, {
},
{
code: "function a() {\nif (b) {\nc();\n}\n//comment\nreturn b;\n}",
errors: ["Expected newline before return statement."]
}, {
},
{
code: "function a() {\nif (b) {\nc();\n}\n//comment\nreturn b;\n}",
errors: ["Expected newline before return statement."]
}, {
code: "function a() {\n/*comment\ncomment*/\n//comment\n\nreturn b;\n}",
errors: ["Unexpected newline before return statement."]
}, {
code: "function a() {\n//comment\nif (b) {\n/*comment\ncomment*/\n\nreturn;\n}\n}",
errors: ["Unexpected newline before return statement."]
}, {
},
{
code: "function a() {\n/*comment\ncomment*/\nif (b) {\nc();\nreturn b;\n} else {\n//comment\n\nreturn d;\n}\n/*multi-line\ncomment*/\nreturn e;\n}",
errors: ["Expected newline before return statement.", "Unexpected newline before return statement.", "Expected newline before return statement."]
}, {
code: "function a() {\nif (b) { //comment\n\nreturn;\n}\n\nreturn c;\n}",
errors: ["Unexpected newline before return statement."]
}, {
errors: ["Expected newline before return statement.", "Expected newline before return statement."]
},
{
code: "function a() {\nif (b) { return; } //comment\nreturn c;\n}",
errors: ["Expected newline before return statement."]
}, {
},
{
code: "function a() {\nif (b) { return; } /*multi-line\ncomment*/\nreturn c;\n}",
errors: ["Expected newline before return statement."]
}, {
},
{
code: "function a() {\nif (b) { return; }\n/*multi-line\ncomment*/ return c;\n}",
errors: ["Expected newline before return statement."]
}, {
},
{
code: "function a() {\nif (b) { return; } /*multi-line\ncomment*/ return c;\n}",
errors: ["Expected newline before return statement."]
}, {
code: "\nreturn;",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: ["Unexpected newline before return statement."]
}, {
code: "\n\nreturn;",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: ["Unexpected newline before return statement."]
}, {
code: "\n// comment\nreturn;",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: ["Unexpected newline before return statement."]
}, {
code: "\n/* comment */\nreturn;",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: ["Unexpected newline before return statement."]
}, {
code: "\n/* multi-line\ncomment */\nreturn;",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: ["Unexpected newline before return statement."]
}, {
code: "/* comment */\n\nreturn;",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: ["Unexpected newline before return statement."]
}, {
},
{
code: "var a;\nreturn;",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: ["Expected newline before return statement."]
Expand Down

0 comments on commit ee0cd58

Please sign in to comment.