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

Commit e8887af

Browse files
committed
Improve disallowSpaceAfterBinaryOperators rule
Catch nodes with "VariableDeclarator" type
1 parent 102e584 commit e8887af

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

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

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,6 @@ module.exports.prototype = {
3030
check: function(file, errors) {
3131
var operators = this._operatorIndex;
3232

33-
// 2 + 2, 2 == 2
34-
file.iterateNodesByType(
35-
['BinaryExpression', 'AssignmentExpression', 'LogicalExpression'],
36-
function(node) {
37-
if (operators[node.operator]) {
38-
var indent;
39-
var range = node.right.range[0];
40-
41-
if (tokenHelper.isTokenParenthesis(file, range - 1, true)) {
42-
indent = node.operator.length + 1;
43-
} else {
44-
indent = node.operator.length;
45-
}
46-
47-
var part = tokenHelper.getTokenByRangeStartIfPunctuator(
48-
file,
49-
range - indent,
50-
node.operator,
51-
true
52-
);
53-
54-
if (!part) {
55-
var loc = tokenHelper.findOperatorByRangeStart(
56-
file, node.right.range[0], node.operator, true
57-
).loc.start;
58-
59-
errors.add(
60-
'Operator ' + node.operator + ' should stick to following expression',
61-
loc.line,
62-
tokenHelper.getPointerEntities(loc.column, node.operator.length)
63-
);
64-
}
65-
}
66-
}
67-
);
68-
6933
function errorIfApplicable(token, i, tokens, operator) {
7034
var nextToken = tokens[i + 1];
7135

@@ -80,7 +44,7 @@ module.exports.prototype = {
8044
}
8145
}
8246

83-
// ":" for object property only but not for ternar
47+
// ":" for object property only but not for ternar
8448
if (operators[':']) {
8549
file.iterateNodesByType(['ObjectExpression'], function(node) {
8650
node.properties.forEach(function(prop) {
@@ -103,6 +67,44 @@ module.exports.prototype = {
10367
errorIfApplicable(token, i, tokens, operator);
10468
});
10569
}
70+
71+
// For everything else
72+
file.iterateNodesByType(
73+
['BinaryExpression', 'AssignmentExpression', 'VariableDeclarator', 'LogicalExpression'],
74+
function(node) {
75+
var isDec = node.type === 'VariableDeclarator';
76+
var operator = isDec ? '=' : node.operator;
77+
78+
if (!operators[operator] || node.init === null) {
79+
return;
80+
}
81+
82+
var range = (isDec ? node.init : node.right).range[0];
83+
84+
var indent = tokenHelper.isTokenParenthesis(file, range - 1, true) ?
85+
operator.length + 1 :
86+
operator.length;
87+
88+
var part = tokenHelper.getTokenByRangeStartIfPunctuator(
89+
file,
90+
range - indent,
91+
operator,
92+
true
93+
);
94+
95+
if (!part) {
96+
var loc = tokenHelper.findOperatorByRangeStart(
97+
file, range, operator, true
98+
).loc.start;
99+
100+
errors.add(
101+
'Operator ' + operator + ' should stick to following expression',
102+
loc.line,
103+
tokenHelper.getPointerEntities(loc.column, operator.length)
104+
);
105+
}
106+
}
107+
);
106108
}
107109

108110
};

test/rules/disallow-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/disallow-space-after-binary-operators', function() {
7373
checker.configure({ disallowSpaceAfterBinaryOperators: [','] });
7474
assert(checker.checkString('function test(a, b){}').getErrorCount() === 1);
7575
});
76+
it('should report for assignment expression', function() {
77+
checker.configure({ disallowSpaceAfterBinaryOperators: ['='] });
78+
assert(checker.checkString('x = 1').getErrorCount() === 1);
79+
});
80+
it('should report for assignment expressions', function() {
81+
checker.configure({ disallowSpaceAfterBinaryOperators: ['='] });
82+
assert(checker.checkString('var x = 1, t = 2').getErrorCount() === 2);
83+
});
84+
it('should not report for assignment expressions if "=" is not specified', function() {
85+
checker.configure({ disallowSpaceAfterBinaryOperators: [','] });
86+
assert(checker.checkString('var x = 1;').isEmpty());
87+
});
88+
it('should not report empty assignment expression', function() {
89+
checker.configure({ disallowSpaceAfterBinaryOperators: ['='] });
90+
assert(checker.checkString('var x').isEmpty());
91+
});
7692
});

0 commit comments

Comments
 (0)