Skip to content
Permalink
Browse files

Fix: space-before-function-paren autofix removes comments (fixes #12259

…) (#12264)
  • Loading branch information
mdjermanovic authored and mysticatea committed Sep 14, 2019
1 parent 01da7d0 commit 985c9e5eba351965a8a1491a41dbdcc78154b8f4
Showing with 98 additions and 1 deletion.
  1. +12 −1 lib/rules/space-before-function-paren.js
  2. +86 −0 tests/lib/rules/space-before-function-paren.js
@@ -124,7 +124,18 @@ module.exports = {
node,
loc: leftToken.loc.end,
message: "Unexpected space before function parentheses.",
fix: fixer => fixer.removeRange([leftToken.range[1], rightToken.range[0]])
fix(fixer) {
const comments = sourceCode.getCommentsBefore(rightToken);

// Don't fix anything if there's a single line comment between the left and the right token
if (comments.some(comment => comment.type === "Line")) {
return null;
}
return fixer.replaceTextRange(
[leftToken.range[1], rightToken.range[0]],
comments.reduce((text, comment) => text + sourceCode.getText(comment), "")
);
}
});
} else if (!hasSpacing && functionConfig === "always") {
context.report({
@@ -24,6 +24,11 @@ ruleTester.run("space-before-function-paren", rule, {
"function foo () {}",
"var foo = function () {}",
"var bar = function foo () {}",
"var bar = function foo/**/ () {}",
"var bar = function foo /**/() {}",
"var bar = function foo/**/\n() {}",
"var bar = function foo\n/**/() {}",
"var bar = function foo//\n() {}",
"var obj = { get foo () {}, set foo (val) {} };",
{
code: "var obj = { foo () {} };",
@@ -34,6 +39,9 @@ ruleTester.run("space-before-function-paren", rule, {

{ code: "function foo() {}", options: ["never"] },
{ code: "var foo = function() {}", options: ["never"] },
{ code: "var foo = function/**/() {}", options: ["never"] },
{ code: "var foo = function/* */() {}", options: ["never"] },
{ code: "var foo = function/* *//* */() {}", options: ["never"] },
{ code: "var bar = function foo() {}", options: ["never"] },
{ code: "var obj = { get foo() {}, set foo(val) {} };", options: ["never"] },
{
@@ -226,6 +234,84 @@ ruleTester.run("space-before-function-paren", rule, {
}
]
},
{
code: "function foo /* */ () {}",
output: "function foo/* */() {}",
options: ["never"],
errors: [
{
type: "FunctionDeclaration",
message: "Unexpected space before function parentheses.",
line: 1,
column: 13
}
]
},
{
code: "function foo/* block comment */ () {}",
output: "function foo/* block comment */() {}",
options: ["never"],
errors: [
{
type: "FunctionDeclaration",
message: "Unexpected space before function parentheses.",
line: 1,
column: 13
}
]
},
{
code: "function foo/* 1 */ /* 2 */ \n /* 3 */\n/* 4 */ () {}",
output: "function foo/* 1 *//* 2 *//* 3 *//* 4 */() {}",
options: ["never"],
errors: [
{
type: "FunctionDeclaration",
message: "Unexpected space before function parentheses.",
line: 1,
column: 13
}
]
},
{
code: "function foo//\n() {}",
output: null,
options: ["never"],
errors: [
{
type: "FunctionDeclaration",
message: "Unexpected space before function parentheses.",
line: 1,
column: 13
}
]
},
{
code: "function foo // line comment \n () {}",
output: null,
options: ["never"],
errors: [
{
type: "FunctionDeclaration",
message: "Unexpected space before function parentheses.",
line: 1,
column: 13
}
]
},
{
code: "function foo\n//\n() {}",
output: null,
options: ["never"],
errors: [
{
type: "FunctionDeclaration",
message: "Unexpected space before function parentheses.",
line: 1,
column: 13
}
]
},
{
code: "var foo = function () {}",
output: "var foo = function() {}",

0 comments on commit 985c9e5

Please sign in to comment.
You can’t perform that action at this time.