Skip to content

Commit

Permalink
Fix: space-before-function-paren autofix removes comments (fixes #12259)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Sep 12, 2019
1 parent 460c5ad commit 1dd4f82
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/rules/space-before-function-paren.js
Expand Up @@ -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({
Expand Down
86 changes: 86 additions & 0 deletions tests/lib/rules/space-before-function-paren.js
Expand Up @@ -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 () {} };",
Expand All @@ -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"] },
{
Expand Down Expand Up @@ -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() {}",
Expand Down

0 comments on commit 1dd4f82

Please sign in to comment.