diff --git a/docs/rules/newline-before-return.md b/docs/rules/newline-before-return.md index 4a45c2ee5c9..34e972dfb6c 100644 --- a/docs/rules/newline-before-return.md +++ b/docs/rules/newline-before-return.md @@ -38,18 +38,6 @@ 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; @@ -57,12 +45,6 @@ function foo(bar) { return bar; } -function foo() { - - // comment - return; -} - function foo(bar) { if (!bar) { return; @@ -82,6 +64,11 @@ function foo() { return; } +function foo() { + + return; +} + function foo(bar) { if (!bar) return; } @@ -103,6 +90,19 @@ function foo(bar) { return bar; } + +function foo(bar) { + if (!bar) { + + return; + } +} + +function foo() { + + // comment + return; +} ``` ## When Not To Use It diff --git a/lib/rules/newline-before-return.js b/lib/rules/newline-before-return.js index 77f3aedaa80..5c8a139358c 100644 --- a/lib/rules/newline-before-return.js +++ b/lib/rules/newline-before-return.js @@ -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." + }); } } }; diff --git a/tests/lib/rules/newline-before-return.js b/tests/lib/rules/newline-before-return.js index ccfb622dfb3..bfa197166c8 100644 --- a/tests/lib/rules/newline-before-return.js +++ b/tests/lib/rules/newline-before-return.js @@ -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}", @@ -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 } }, @@ -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."]