Skip to content

Commit

Permalink
require-space-after-line-comment: add except option
Browse files Browse the repository at this point in the history
according to jscs-dev#592 and jscs-dev#593 we've decided to make allExcept option to this rule to pass there a list of allowed characters

- added allExcept option to pass here exceptions
- fixes initial jscs-dev#592 bug with tabs in line comments
- add validation of allExcept (should be a <String[]>, according to jscs-dev#698)

Closes jscs-dev#696
Fixes jscs-dev#592
  • Loading branch information
Alexej Yaroshevich committed Oct 20, 2014
1 parent 9af1e27 commit 8714272
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions lib/rules/require-space-after-line-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,25 @@ module.exports.prototype = {
configure: function(requireSpaceAfterLineComment) {
assert(
requireSpaceAfterLineComment === true ||
requireSpaceAfterLineComment === 'allowSlash',
'requireSpaceAfterLineComment option requires the value true or `allowSlash`'
requireSpaceAfterLineComment === 'allowSlash' ||
typeof requireSpaceAfterLineComment === 'object',
'requireSpaceAfterLineComment option requires the value `true` ' +
'or an object with String[] `allExcept` property'
);

this._allowSlash = requireSpaceAfterLineComment === 'allowSlash';
// verify first item in `allExcept` property in object (if it's an object)
assert(
typeof requireSpaceAfterLineComment !== 'object' ||
Array.isArray(requireSpaceAfterLineComment.allExcept) &&
typeof requireSpaceAfterLineComment.allExcept[0] === 'string',
'Property `allExcept` in requireSpaceAfterLineComment should be an array of strings'
);

// don't check triple slashed comments, microsoft js doc convention. see #593
// exceptions. see #592
// need to drop allowSlash support in 2.0. Fixes #697
this._allExcept = requireSpaceAfterLineComment === 'allowSlash' ? ['/'] :
requireSpaceAfterLineComment.allExcept || [];
},

getOptionName: function() {
Expand All @@ -20,21 +34,26 @@ module.exports.prototype = {

check: function(file, errors) {
var comments = file.getComments();
var allowSlash = this._allowSlash;
var allExcept = this._allExcept;

comments.forEach(function(comment) {
if (comment.type === 'Line') {
var value = comment.value;

// don't check triple slashed comments, microsoft js doc convention. see #593
if (allowSlash && value[0] === '/') {
value = value.substr(1);
}
// cutout exceptions
allExcept.forEach(function(el) {
if (value.indexOf(el) === 0) {
value = value.substr(el.length);
}
});

if (value.length === 0) {
return;
}

// tabs should be treated as spaces. see #592
value = value.replace(/[\s]/g, ' ');

if (value[0] !== ' ') {
errors.add('Missing space after line comment', comment.loc.start);
}
Expand Down

0 comments on commit 8714272

Please sign in to comment.