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

Commit 4a6491c

Browse files
committed
Improve requireSpaceAfterBinaryOperators rule
Catch nodes with "VariableDeclarator" type
1 parent e8887af commit 4a6491c

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,36 @@ module.exports.prototype = {
7171

7272
// For everything else
7373
file.iterateNodesByType(
74-
['BinaryExpression', 'AssignmentExpression', 'LogicalExpression'],
74+
['BinaryExpression', 'AssignmentExpression', 'VariableDeclarator', 'LogicalExpression'],
7575
function(node) {
76-
if (operators[node.operator]) {
77-
var indent;
78-
var range = node.right.range[0];
79-
80-
if (tokenHelper.isTokenParenthesis(file, range - 1, true)) {
81-
indent = node.operator.length + 1;
82-
} else {
83-
indent = node.operator.length;
84-
}
85-
86-
var part = tokenHelper.getTokenByRangeStartIfPunctuator(
87-
file,
88-
range - indent,
89-
node.operator,
90-
true
91-
);
76+
var isDec = node.type === 'VariableDeclarator';
77+
var operator = isDec ? '=' : node.operator;
78+
79+
if (!operators[operator] || node.init === null) {
80+
return;
81+
}
82+
83+
var range = (isDec ? node.init : node.right).range[0];
9284

93-
if (part) {
94-
var loc = part.loc.start;
85+
var indent = tokenHelper.isTokenParenthesis(file, range - 1, true) ?
86+
operator.length + 1 :
87+
operator.length;
9588

96-
errors.add(
97-
'Operator ' + node.operator + ' should not stick to following expression',
98-
loc.line,
99-
tokenHelper.getPointerEntities(loc.column, node.operator.length)
100-
);
101-
}
89+
var part = tokenHelper.getTokenByRangeStartIfPunctuator(
90+
file,
91+
range - indent,
92+
operator,
93+
true
94+
);
95+
96+
if (part) {
97+
var loc = part.loc.start;
98+
99+
errors.add(
100+
'Operator ' + operator + ' should not stick to following expression',
101+
loc.line,
102+
tokenHelper.getPointerEntities(loc.column, operator.length)
103+
);
102104
}
103105
}
104106
);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,20 @@ describe('rules/require-space-after-binary-operators', function() {
7373
checker.configure({ requireSpaceAfterBinaryOperators: [','] });
7474
assert(checker.checkString('function test(a,b){}').getErrorCount() === 1);
7575
});
76+
it('should report for assignment expression', function() {
77+
checker.configure({ requireSpaceAfterBinaryOperators: ['='] });
78+
assert(checker.checkString('var x=1').getErrorCount() === 1);
79+
});
80+
it('should report for assignment expressions', function() {
81+
checker.configure({ requireSpaceAfterBinaryOperators: ['='] });
82+
assert(checker.checkString('var x=1, t=2').getErrorCount() === 2);
83+
});
84+
it('should not report for assignment expressions without "=" sign', function() {
85+
checker.configure({ requireSpaceAfterBinaryOperators: ['='] });
86+
assert(checker.checkString('var x,z;').isEmpty());
87+
});
88+
it('should not report for assignment expressions if "=" is not specified', function() {
89+
checker.configure({ requireSpaceAfterBinaryOperators: [','] });
90+
assert(checker.checkString('var x=1;').isEmpty());
91+
});
7692
});

0 commit comments

Comments
 (0)