Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 2fa28e2

Browse files
committed
Improve requireSpaceBeforeBinaryOperators rule
Add check for assignment to requireSpaceBeforeBinaryOperators rule
1 parent e6b5635 commit 2fa28e2

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

lib/rules/require-space-before-binary-operators.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,18 @@ module.exports.prototype = {
3838
}
3939
});
4040

41-
// Comma
42-
if (operators[ ',']) {
41+
// Comma and assignment
42+
if (operators[','] || operators['=']) {
4343
file.iterateTokensByType('Punctuator', function(token, i, tokens) {
44-
if (token.value !== ',') {
44+
var operator = token.value;
45+
if (operator !== ',' && operator !== '=') {
4546
return;
4647
}
4748

4849
var prevToken = tokens[i - 1];
4950
if (prevToken && prevToken.range[1] === token.range[0]) {
5051
errors.add(
51-
'Operator ' + token.value + ' should not stick to preceding expression',
52+
'Operator ' + operator + ' should not stick to preceding expression',
5253
token.loc.start
5354
);
5455
}

test/rules/require-space-before-binary-operators.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,13 @@ describe('rules/require-space-before-binary-operators', function() {
4545
checker.configure({ requireSpaceBeforeBinaryOperators: [','] });
4646
assert(checker.checkString('({a: 1, b: 2})').getErrorCount() === 1);
4747
});
48+
it('should not report assignment operator for "a = b"', function() {
49+
checker.configure({ requireSpaceBeforeBinaryOperators: ['='] });
50+
assert(checker.checkString('a = b').isEmpty());
51+
});
52+
it('should report assignment operator for "a=b"', function() {
53+
checker.configure({ requireSpaceBeforeBinaryOperators: ['='] });
54+
assert(checker.checkString('a=b').getErrorCount() === 1);
55+
});
4856

4957
});

0 commit comments

Comments
 (0)