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

Commit f0a278e

Browse files
ikokostyamarkelog
authored andcommitted
disallowSpacesInsideTemplateStringPlaceholders: check template literal
Don't check space out side template literal Fixes #2137 Closes gh-2139
1 parent 7706e43 commit f0a278e

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed

lib/rules/disallow-spaces-inside-template-string-placeholders.js

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,52 @@ module.exports.prototype = {
4545
check: function(file, errors) {
4646
file.iterateNodesByType('TemplateLiteral', function(node) {
4747
var first = file.getFirstNodeToken(node);
48-
var nextFist = file.getNextToken(first, {includeWhitespace: true});
4948
var last = file.getLastNodeToken(node);
50-
var prevLast = file.getPrevToken(last, {includeWhitespace: true});
5149

52-
if (nextFist.isWhitespace) {
53-
errors.assert.noWhitespaceBetween({
54-
token: first,
55-
nextToken: file.getNextToken(first),
56-
message: 'Illegal space after "${"'
57-
});
58-
}
50+
// Iterate over tokens inside TemplateLiteral node.
51+
for (var token = first; ; token = file.getNextToken(token)) {
52+
if (containsPlaceholderStart(token)) {
53+
var nextFist = file.getNextToken(token, {includeWhitespace: true});
54+
if (nextFist.isWhitespace) {
55+
errors.assert.noWhitespaceBetween({
56+
token: token,
57+
nextToken: file.getNextToken(token),
58+
message: 'Illegal space after "${"'
59+
});
60+
}
61+
}
62+
63+
if (containsPlaceholderEnd(token)) {
64+
var prevLast = file.getPrevToken(token, {includeWhitespace: true});
65+
if (prevLast.isWhitespace) {
66+
errors.assert.noWhitespaceBetween({
67+
token: file.getPrevToken(token),
68+
nextToken: token,
69+
message: 'Illegal space before "}"'
70+
});
71+
}
72+
}
5973

60-
if (prevLast.isWhitespace) {
61-
errors.assert.noWhitespaceBetween({
62-
token: file.getPrevToken(last),
63-
nextToken: last,
64-
message: 'Illegal space before "}"'
65-
});
74+
if (token === last) {
75+
return;
76+
}
6677
}
6778
});
6879
}
6980
};
81+
82+
/**
83+
* @param {Object} token
84+
* @returns {Boolean}
85+
*/
86+
function containsPlaceholderStart(token) {
87+
return token.type === 'Template' && /\${$/.test(token.value);
88+
}
89+
90+
/**
91+
* @param {Object} token
92+
* @returns {Boolean}
93+
*/
94+
function containsPlaceholderEnd(token) {
95+
return token.type === 'Template' && /^}/.test(token.value);
96+
}

test/specs/rules/disallow-spaces-inside-template-string-placeholders.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,31 @@ describe('rules/disallow-spaces-inside-template-string-placeholders', function()
3232
.from('disallowSpacesInsideTemplateStringPlaceholders');
3333
});
3434

35-
// Enabled in 3.0 but kinda hard to implement it in 2.x
36-
it.skip('should report with space in second placeholder', function() {
35+
it('should report with space in second placeholder', function() {
3736
expect(checker.checkString('`${1} + ${ 2}`')).to.have.one.validation.error
3837
.from('disallowSpacesInsideTemplateStringPlaceholders');
3938
});
4039

40+
it('should report with spaces in both placeholders', function() {
41+
expect(checker.checkString('`${ 1 } + ${ 2 }`')).to.have.error.count.equal(4);
42+
});
43+
44+
it('should report with spaces in placeholder with expression', function() {
45+
expect(checker.checkString('`${ foo() }`')).to.have.error.count.equal(2);
46+
});
47+
48+
it('should not report with spaces before and after template string', function() {
49+
expect(checker.checkString('x = `` ')).to.have.no.errors();
50+
});
51+
52+
it('should not report with space before template string with closing curly brace', function() {
53+
expect(checker.checkString('x = `}`')).to.have.no.errors();
54+
});
55+
56+
it('should not report with spaces inside curly braces without dollar sign', function() {
57+
expect(checker.checkString('`{ x }`')).to.have.no.errors();
58+
});
59+
4160
it('should not report with no spaces in placeholder', function() {
4261
expect(checker.checkString('`${1}`')).to.have.no.errors();
4362
});

0 commit comments

Comments
 (0)