Skip to content

Commit

Permalink
Update: make fixer a bit smarter and add more tests (refs eslint#5958)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorbal committed Sep 6, 2016
1 parent 0c352e2 commit a68f4a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/rules/newline-before-return.js
Expand Up @@ -138,7 +138,6 @@ module.exports = {
function hasNewlineBefore(node) {
const lineNumNode = node.loc.start.line;
const lineNumTokenBefore = getLineNumberOfTokenBefore(node);

const commentLines = calcCommentLines(node, lineNumTokenBefore);

return (lineNumNode - lineNumTokenBefore - commentLines) > 1;
Expand Down Expand Up @@ -191,7 +190,10 @@ module.exports = {
message: "Expected newline before return statement.",
fix(fixer) {
if (canFix(node)) {
return fixer.insertTextBefore(node, "\n");
const tokenBefore = sourceCode.getTokenBefore(node);
const newlines = node.loc.start.line === tokenBefore.loc.end.line ? "\n\n" : "\n";

return fixer.insertTextBefore(node, newlines);
}
return null;
}
Expand Down
23 changes: 22 additions & 1 deletion tests/lib/rules/newline-before-return.js
Expand Up @@ -113,7 +113,7 @@ ruleTester.run("newline-before-return", rule, {
{
code: "function a() {\nvar b; return;\n}",
errors: ["Expected newline before return statement."],
output: "function a() {\nvar b; \nreturn;\n}"
output: "function a() {\nvar b; \n\nreturn;\n}"
},
{
code: "function a() {\nvar b;\nreturn;\n}",
Expand All @@ -125,6 +125,11 @@ ruleTester.run("newline-before-return", rule, {
errors: ["Expected newline before return statement."],
output: "function a() {\nif (b) return b;\nelse if (c) return c;\nelse {\ne();\n\nreturn d;\n}\n}"
},
{
code: "function a() {\nif (b) return b;\nelse if (c) return c;\nelse {\ne(); return d;\n}\n}",
errors: ["Expected newline before return statement."],
output: "function a() {\nif (b) return b;\nelse if (c) return c;\nelse {\ne(); \n\nreturn d;\n}\n}"
},
{
code: "function a() {\n while (b) {\nc();\nreturn;\n}\n}",
errors: ["Expected newline before return statement."],
Expand Down Expand Up @@ -187,11 +192,22 @@ ruleTester.run("newline-before-return", rule, {
errors: ["Expected newline before return statement."],
output: "var a;\n\nreturn;"
},
{
code: "var a; return;",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: ["Expected newline before return statement."],
output: "var a; \n\nreturn;"
},
{
code: "function a() {\n{\n//comment\n}\nreturn\n}",
errors: ["Expected newline before return statement."],
output: "function a() {\n{\n//comment\n}\n\nreturn\n}"
},
{
code: "function a() {\n{\n//comment\n} return\n}",
errors: ["Expected newline before return statement."],
output: "function a() {\n{\n//comment\n} \n\nreturn\n}"
},
{
code: "function a() {\nvar c;\nwhile (b) {\n c = d; //comment\n}\nreturn c;\n}",
errors: ["Expected newline before return statement."],
Expand Down Expand Up @@ -254,6 +270,11 @@ ruleTester.run("newline-before-return", rule, {
code: "function a() {\nvar b;\nreturn; //comment\n}",
errors: ["Expected newline before return statement."],
output: "function a() {\nvar b;\n\nreturn; //comment\n}"
},
{
code: "function a() {\nvar b; return; //comment\n}",
errors: ["Expected newline before return statement."],
output: "function a() {\nvar b; \n\nreturn; //comment\n}"
}
]
});

0 comments on commit a68f4a1

Please sign in to comment.