diff --git a/lib/rules/space-before-function-paren.js b/lib/rules/space-before-function-paren.js index ca20dbc4abf..3a6d430f362 100644 --- a/lib/rules/space-before-function-paren.js +++ b/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({ diff --git a/tests/lib/rules/space-before-function-paren.js b/tests/lib/rules/space-before-function-paren.js index 5bcfd7be16c..961d6895adc 100644 --- a/tests/lib/rules/space-before-function-paren.js +++ b/tests/lib/rules/space-before-function-paren.js @@ -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() {}",