Skip to content

Commit

Permalink
Fix: no-extra-bind autofix removes comments (#12293)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and platinumazure committed Sep 29, 2019
1 parent 6ad7e86 commit 0e68677
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/rules/no-extra-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode();
let scopeInfo = null;

/**
Expand Down Expand Up @@ -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]]);
}
Expand Down
62 changes: 62 additions & 0 deletions tests/lib/rules/no-extra-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
});

0 comments on commit 0e68677

Please sign in to comment.