diff --git a/lib/rules/no-extra-bind.js b/lib/rules/no-extra-bind.js index cc0b1f84372..cc5611b1089 100644 --- a/lib/rules/no-extra-bind.js +++ b/lib/rules/no-extra-bind.js @@ -40,6 +40,7 @@ module.exports = { }, create(context) { + const sourceCode = context.getSourceCode(); let scopeInfo = null; /** @@ -71,8 +72,13 @@ module.exports = { return null; } - const firstTokenToRemove = context.getSourceCode() + const firstTokenToRemove = sourceCode .getFirstTokenBetween(node.parent.object, node.parent.property, astUtils.isNotClosingParenToken); + const lastTokenToRemove = sourceCode.getLastToken(node.parent.parent); + + if (sourceCode.commentsExistBetween(firstTokenToRemove, lastTokenToRemove)) { + return null; + } return fixer.removeRange([firstTokenToRemove.range[0], node.parent.parent.range[1]]); } diff --git a/tests/lib/rules/no-extra-bind.js b/tests/lib/rules/no-extra-bind.js index cc2b164e8ec..3bfcda8c5d0 100644 --- a/tests/lib/rules/no-extra-bind.js +++ b/tests/lib/rules/no-extra-bind.js @@ -97,6 +97,68 @@ ruleTester.run("no-extra-bind", rule, { code: "var a = function() {}.bind(b.c)", output: null, errors + }, + + // Should not autofix if it would remove comments + { + code: "var a = function() {}/**/.bind(b)", + output: "var a = function() {}/**/", + errors + }, + { + code: "var a = function() {}/**/['bind'](b)", + output: "var a = function() {}/**/", + errors + }, + { + code: "var a = function() {}//comment\n.bind(b)", + output: "var a = function() {}//comment\n", + errors + }, + { + code: "var a = function() {}./**/bind(b)", + output: null, + errors + }, + { + code: "var a = function() {}[/**/'bind'](b)", + output: null, + errors + }, + { + code: "var a = function() {}.//\nbind(b)", + output: null, + errors + }, + { + code: "var a = function() {}.bind/**/(b)", + output: null, + errors + }, + { + code: "var a = function() {}.bind(\n/**/b)", + output: null, + errors + }, + { + code: "var a = function() {}.bind(b/**/)", + output: null, + errors + }, + { + code: "var a = function() {}.bind(b//\n)", + output: null, + errors + }, + { + code: "var a = function() {}.bind(b\n/**/)", + output: null, + errors + }, + { + code: "var a = function() {}.bind(b)/**/", + output: "var a = function() {}/**/", + errors } ] });