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

Commit c19442a

Browse files
ikokostyamarkelog
authored andcommitted
New Rule: disallowSpacesInsideTemplateStringPlaceholders
Disallows spaces before and after curly brace inside template string placeholders Valid `Hello ${name}!` Invalid `Hello ${ name }!` Closes gh-2125
1 parent 9b59f46 commit c19442a

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

lib/config/configuration.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@ Configuration.prototype.registerDefaultRules = function() {
952952
this.registerRule(require('../rules/disallow-identical-destructuring-names'));
953953
this.registerRule(require('../rules/require-spaces-in-generator'));
954954
this.registerRule(require('../rules/disallow-spaces-in-generator'));
955+
this.registerRule(require('../rules/disallow-spaces-inside-template-string-placeholders'));
955956
this.registerRule(require('../rules/require-object-destructuring'));
956957
this.registerRule(require('../rules/require-enhanced-object-literals'));
957958
this.registerRule(require('../rules/require-array-destructuring'));
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Disallows spaces before and after curly brace inside template string placeholders.
3+
*
4+
* Type: `Boolean`
5+
*
6+
* Value: `true`
7+
*
8+
* #### Example
9+
*
10+
* ```js
11+
* "disallowSpacesInsideTemplateStringPlaceholders": true
12+
* ```
13+
*
14+
* ##### Valid for mode `true`
15+
*
16+
* ```js
17+
* `Hello ${name}!`
18+
* ```
19+
*
20+
* ##### Invalid for mode `true`
21+
*
22+
* ```js
23+
* `Hello ${ name}!`
24+
* `Hello ${name }!`
25+
* `Hello ${ name }!`
26+
* ```
27+
*/
28+
29+
var assert = require('assert');
30+
31+
module.exports = function() {};
32+
33+
module.exports.prototype = {
34+
configure: function(options) {
35+
assert(
36+
options === true,
37+
this.getOptionName() + ' option requires a true value or should be removed'
38+
);
39+
},
40+
41+
getOptionName: function() {
42+
return 'disallowSpacesInsideTemplateStringPlaceholders';
43+
},
44+
45+
check: function(file, errors) {
46+
file.iterateNodesByType('TemplateLiteral', function(node) {
47+
node.childElements
48+
.filter(function(element) {
49+
return element.isToken && element.type === 'Punctuator';
50+
})
51+
.forEach(function(element) {
52+
if (element.value === '${' && element.nextSibling.isWhitespace) {
53+
errors.assert.noWhitespaceBetween({
54+
token: element,
55+
nextToken: element.nextCodeToken,
56+
message: 'Illegal space after "${"'
57+
});
58+
}
59+
if (element.value === '}' && element.previousSibling.isWhitespace) {
60+
errors.assert.noWhitespaceBetween({
61+
token: element.previousCodeToken,
62+
nextToken: element,
63+
message: 'Illegal space before "}"'
64+
});
65+
}
66+
});
67+
});
68+
}
69+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var Checker = require('../../../lib/checker');
2+
var expect = require('chai').expect;
3+
4+
describe('rules/disallow-spaces-inside-template-string-placeholders', function() {
5+
var checker;
6+
7+
beforeEach(function() {
8+
checker = new Checker();
9+
checker.registerDefaultRules();
10+
11+
checker.configure({
12+
disallowSpacesInsideTemplateStringPlaceholders: true
13+
});
14+
});
15+
16+
it('should report with space after opening curly brace', function() {
17+
expect(checker.checkString('`${ 1}`')).to.have.one.validation.error
18+
.from('disallowSpacesInsideTemplateStringPlaceholders');
19+
});
20+
21+
it('should report with space before closing curly brace', function() {
22+
expect(checker.checkString('`${1 }`')).to.have.one.validation.error
23+
.from('disallowSpacesInsideTemplateStringPlaceholders');
24+
});
25+
26+
it('should report with spaces before opening and after closing curly braces', function() {
27+
expect(checker.checkString('`${ 1 }`')).to.have.error.count.equal(2);
28+
});
29+
30+
it('should report with more than one space in placeholder', function() {
31+
expect(checker.checkString('`${ 1}`')).to.have.one.validation.error
32+
.from('disallowSpacesInsideTemplateStringPlaceholders');
33+
});
34+
35+
it('should report with space in second placeholder', function() {
36+
expect(checker.checkString('`${1} + ${ 2}`')).to.have.one.validation.error
37+
.from('disallowSpacesInsideTemplateStringPlaceholders');
38+
});
39+
40+
it('should not report with no spaces in placeholder', function() {
41+
expect(checker.checkString('`${1}`')).to.have.no.errors();
42+
});
43+
44+
it('should work with tagged template string', function () {
45+
expect(checker.checkString('tag`${1}`')).to.have.no.errors();
46+
expect(checker.checkString('tag`${1 }`')).to.have.one.validation.error
47+
.from('disallowSpacesInsideTemplateStringPlaceholders');
48+
expect(checker.checkString('tag`${ 1}`')).to.have.one.validation.error
49+
.from('disallowSpacesInsideTemplateStringPlaceholders');
50+
});
51+
});

0 commit comments

Comments
 (0)