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

Commit ed03250

Browse files
committed
requireTemplateStrings: should not report string to binary
For `{"allExcept": ["stringConcatenation"]}` Fixes #2050
1 parent b8fce17 commit ed03250

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

lib/rules/require-template-strings.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,26 @@ module.exports.prototype = {
9393
);
9494
}
9595

96+
var i = 0;
9697
file.iterateNodesByType('BinaryExpression', function(node) {
9798
if (node.operator !== '+') {
9899
return;
99100
}
100101

101-
var leftIsString = typeof node.left.value === 'string' ||
102-
node.left.type === 'TemplateLiteral';
103-
var rightIsString = typeof node.right.value === 'string' ||
104-
node.right.type === 'TemplateLiteral';
102+
var leftIsString = node.left;
103+
var rightIsString = node.right;
104+
105+
// Left side could also be binary expression (See gh-2050),
106+
// but not the right one
107+
while (leftIsString.type === 'BinaryExpression') {
108+
leftIsString = leftIsString.left;
109+
}
110+
111+
leftIsString = typeof leftIsString.value === 'string' ||
112+
leftIsString.type === 'TemplateLiteral'
113+
114+
rightIsString = typeof rightIsString.value === 'string' ||
115+
rightIsString.type === 'TemplateLiteral';
105116

106117
if (allowStringConcatenation && leftIsString && rightIsString) {
107118
return;

test/specs/rules/require-template-strings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ describe('rules/require-template-strings', function() {
153153
it('should not report the use of string concatenation with two template strings', function() {
154154
expect(checker.checkString('`a` + `a`')).to.have.no.errors();
155155
});
156+
157+
it('should not report string with binary concatination', function() {
158+
expect(checker.checkString('"a" + "b" + "c";')).to.have.no.errors();
159+
});
160+
161+
it('should not report string with binary concatination for three operands', function() {
162+
expect(checker.checkString('"a" + "b" + "c";')).to.have.no.errors();
163+
});
164+
165+
it('should not report string with binary concatination for four operands', function() {
166+
expect(checker.checkString('"a" + "b" + "c" + "d";')).to.have.no.errors();
167+
});
156168
});
157169

158170
describe('invalid options', function() {

0 commit comments

Comments
 (0)