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

Commit

Permalink
New Rule: disallowSpacesInsideTemplateStringPlaceholders
Browse files Browse the repository at this point in the history
Disallows spaces before and after curly brace inside
template string placeholders

Valid
`Hello ${name}!`

Invalid
`Hello ${ name }!`

Closes gh-2125
  • Loading branch information
ikokostya authored and markelog committed Feb 10, 2016
1 parent 9b59f46 commit c19442a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ Configuration.prototype.registerDefaultRules = function() {
this.registerRule(require('../rules/disallow-identical-destructuring-names'));
this.registerRule(require('../rules/require-spaces-in-generator'));
this.registerRule(require('../rules/disallow-spaces-in-generator'));
this.registerRule(require('../rules/disallow-spaces-inside-template-string-placeholders'));
this.registerRule(require('../rules/require-object-destructuring'));
this.registerRule(require('../rules/require-enhanced-object-literals'));
this.registerRule(require('../rules/require-array-destructuring'));
Expand Down
69 changes: 69 additions & 0 deletions lib/rules/disallow-spaces-inside-template-string-placeholders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Disallows spaces before and after curly brace inside template string placeholders.
*
* Type: `Boolean`
*
* Value: `true`
*
* #### Example
*
* ```js
* "disallowSpacesInsideTemplateStringPlaceholders": true
* ```
*
* ##### Valid for mode `true`
*
* ```js
* `Hello ${name}!`
* ```
*
* ##### Invalid for mode `true`
*
* ```js
* `Hello ${ name}!`
* `Hello ${name }!`
* `Hello ${ name }!`
* ```
*/

var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {
configure: function(options) {
assert(
options === true,
this.getOptionName() + ' option requires a true value or should be removed'
);
},

getOptionName: function() {
return 'disallowSpacesInsideTemplateStringPlaceholders';
},

check: function(file, errors) {
file.iterateNodesByType('TemplateLiteral', function(node) {
node.childElements
.filter(function(element) {
return element.isToken && element.type === 'Punctuator';
})
.forEach(function(element) {
if (element.value === '${' && element.nextSibling.isWhitespace) {
errors.assert.noWhitespaceBetween({
token: element,
nextToken: element.nextCodeToken,
message: 'Illegal space after "${"'
});
}
if (element.value === '}' && element.previousSibling.isWhitespace) {
errors.assert.noWhitespaceBetween({
token: element.previousCodeToken,
nextToken: element,
message: 'Illegal space before "}"'
});
}
});
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var Checker = require('../../../lib/checker');
var expect = require('chai').expect;

describe('rules/disallow-spaces-inside-template-string-placeholders', function() {
var checker;

beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();

checker.configure({
disallowSpacesInsideTemplateStringPlaceholders: true
});
});

it('should report with space after opening curly brace', function() {
expect(checker.checkString('`${ 1}`')).to.have.one.validation.error
.from('disallowSpacesInsideTemplateStringPlaceholders');
});

it('should report with space before closing curly brace', function() {
expect(checker.checkString('`${1 }`')).to.have.one.validation.error
.from('disallowSpacesInsideTemplateStringPlaceholders');
});

it('should report with spaces before opening and after closing curly braces', function() {
expect(checker.checkString('`${ 1 }`')).to.have.error.count.equal(2);
});

it('should report with more than one space in placeholder', function() {
expect(checker.checkString('`${ 1}`')).to.have.one.validation.error
.from('disallowSpacesInsideTemplateStringPlaceholders');
});

it('should report with space in second placeholder', function() {
expect(checker.checkString('`${1} + ${ 2}`')).to.have.one.validation.error
.from('disallowSpacesInsideTemplateStringPlaceholders');
});

it('should not report with no spaces in placeholder', function() {
expect(checker.checkString('`${1}`')).to.have.no.errors();
});

it('should work with tagged template string', function () {
expect(checker.checkString('tag`${1}`')).to.have.no.errors();
expect(checker.checkString('tag`${1 }`')).to.have.one.validation.error
.from('disallowSpacesInsideTemplateStringPlaceholders');
expect(checker.checkString('tag`${ 1}`')).to.have.one.validation.error
.from('disallowSpacesInsideTemplateStringPlaceholders');
});
});

0 comments on commit c19442a

Please sign in to comment.