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

Commit

Permalink
Merge 8772e4b into 13d888b
Browse files Browse the repository at this point in the history
  • Loading branch information
qfox committed Oct 18, 2014
2 parents 13d888b + 8772e4b commit 82b754f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2606,23 +2606,29 @@ if (1 == a) {

### requireSpaceAfterLineComment

Requires that a line comment (`//`) be followed by a space or slash space (`/// `).
Requires that a line comment (`//`) be followed by a space.

Type: `Boolean` or `String`
Type: `Boolean` or `Object` or `String`

Values: `true` or `'allowSlash'`
Values:
- `true`
- `"allowSlash"` (*deprecated* use `{ "except": ['/'] }`) allows `/// ` format
- `Object`:
- `except`: (required) array of allowed strings before space `//(here) `

#### Example

```js
"requireSpaceAfterLineComment": true
"requireSpaceAfterLineComment": { except: ['#', '='] }
```

##### Valid

```js
// A comment
/*A comment*/
//# sourceURL=filename.js
//= require something
```

##### Invalid
Expand Down
25 changes: 17 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,18 @@ 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 `except` property with array of strings'
);

this._allowSlash = requireSpaceAfterLineComment === 'allowSlash';
// 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._allowed = requireSpaceAfterLineComment === 'allowSlash' ? ['/'] :
Array.isArray(requireSpaceAfterLineComment.except) ?
requireSpaceAfterLineComment.except : [];
},

getOptionName: function() {
Expand All @@ -20,16 +27,18 @@ module.exports.prototype = {

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

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
allowed.forEach(function(el) {
if (value.indexOf(el) === 0) {
value = value.substr(el.length);
}
});

if (value.length === 0) {
return;
Expand Down
29 changes: 29 additions & 0 deletions test/rules/require-space-after-line-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ describe('rules/require-space-after-line-comment', function() {
it('should report triple slashed comments', function() {
assert(checker.checkString('if (true) {abc();} /// something').getErrorCount() === 1);
});

it('should report sharped line comments', function() {
assert(checker.checkString('if (true) {abc();} //# something').getErrorCount() === 1);
});
});

describe('option value allowSlash', function() {
Expand All @@ -54,4 +58,29 @@ describe('rules/require-space-after-line-comment', function() {
.isEmpty());
});
});

describe('exceptions #, --, (xsharp)', function() {
beforeEach(function() {
checker.configure({ requireSpaceAfterLineComment: { except: ['#', '--', '(xsharp)'] } });
});

it('should not report sharped comment', function() {
assert(checker.checkString('function area() {\n //# require something.js\n}')
.isEmpty());
});

it('should not report (xsharp) line comment', function() {
assert(checker.checkString('function area() {\n //(xsharp) special comment\n}')
.isEmpty());
});

it('should not report line comment with custom substrings', function() {
assert(checker.checkString('function area() {\n' +
' //(xsharp) sourceURL=filename.js\n' +
' //-- require something-else.js\n' +
' return res;\n' +
'}')
.isEmpty());
});
});
});

0 comments on commit 82b754f

Please sign in to comment.