Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Sep 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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