Skip to content

Commit 0e68677

Browse files
mdjermanovicplatinumazure
authored andcommitted
Fix: no-extra-bind autofix removes comments (#12293)
1 parent 6ad7e86 commit 0e68677

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

lib/rules/no-extra-bind.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = {
4040
},
4141

4242
create(context) {
43+
const sourceCode = context.getSourceCode();
4344
let scopeInfo = null;
4445

4546
/**
@@ -71,8 +72,13 @@ module.exports = {
7172
return null;
7273
}
7374

74-
const firstTokenToRemove = context.getSourceCode()
75+
const firstTokenToRemove = sourceCode
7576
.getFirstTokenBetween(node.parent.object, node.parent.property, astUtils.isNotClosingParenToken);
77+
const lastTokenToRemove = sourceCode.getLastToken(node.parent.parent);
78+
79+
if (sourceCode.commentsExistBetween(firstTokenToRemove, lastTokenToRemove)) {
80+
return null;
81+
}
7682

7783
return fixer.removeRange([firstTokenToRemove.range[0], node.parent.parent.range[1]]);
7884
}

tests/lib/rules/no-extra-bind.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,68 @@ ruleTester.run("no-extra-bind", rule, {
9797
code: "var a = function() {}.bind(b.c)",
9898
output: null,
9999
errors
100+
},
101+
102+
// Should not autofix if it would remove comments
103+
{
104+
code: "var a = function() {}/**/.bind(b)",
105+
output: "var a = function() {}/**/",
106+
errors
107+
},
108+
{
109+
code: "var a = function() {}/**/['bind'](b)",
110+
output: "var a = function() {}/**/",
111+
errors
112+
},
113+
{
114+
code: "var a = function() {}//comment\n.bind(b)",
115+
output: "var a = function() {}//comment\n",
116+
errors
117+
},
118+
{
119+
code: "var a = function() {}./**/bind(b)",
120+
output: null,
121+
errors
122+
},
123+
{
124+
code: "var a = function() {}[/**/'bind'](b)",
125+
output: null,
126+
errors
127+
},
128+
{
129+
code: "var a = function() {}.//\nbind(b)",
130+
output: null,
131+
errors
132+
},
133+
{
134+
code: "var a = function() {}.bind/**/(b)",
135+
output: null,
136+
errors
137+
},
138+
{
139+
code: "var a = function() {}.bind(\n/**/b)",
140+
output: null,
141+
errors
142+
},
143+
{
144+
code: "var a = function() {}.bind(b/**/)",
145+
output: null,
146+
errors
147+
},
148+
{
149+
code: "var a = function() {}.bind(b//\n)",
150+
output: null,
151+
errors
152+
},
153+
{
154+
code: "var a = function() {}.bind(b\n/**/)",
155+
output: null,
156+
errors
157+
},
158+
{
159+
code: "var a = function() {}.bind(b)/**/",
160+
output: "var a = function() {}/**/",
161+
errors
100162
}
101163
]
102164
});

0 commit comments

Comments
 (0)