This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Rule: disallowSpacesInsideTemplateStringPlaceholders
Disallows spaces before and after curly brace inside template string placeholders Valid `Hello ${name}!` Invalid `Hello ${ name }!` Closes gh-2125
- Loading branch information
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
lib/rules/disallow-spaces-inside-template-string-placeholders.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "}"' | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
}; |
51 changes: 51 additions & 0 deletions
51
test/specs/rules/disallow-spaces-inside-template-string-placeholders.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |