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

Commit

Permalink
requirePaddingNewLinesBeforeLineComments: Allow consecutive comments …
Browse files Browse the repository at this point in the history
…and firstAfterCurly exception

Refs #1103
Closes gh-1102
  • Loading branch information
elicwhite authored and mikesherov committed Mar 2, 2015
1 parent a9e7f09 commit afc670d
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 28 deletions.
63 changes: 54 additions & 9 deletions lib/rules/require-padding-newlines-before-line-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,42 @@
*
* Type: `Boolean`
*
* Values: `true`
*
* #### Example
* Values:
* - `true`: always require a newline before line comments
* - `Object`:
* - `"allExcept"`: `"firstAfterCurly"` Comments may be first line of block without extra padding
*
* #### Examples
* ```js
* "requirePaddingNewLinesBeforeLineComments": true
* "requirePaddingNewLinesBeforeLineComments": { "allExcept": "firstAfterCurly" }
* ```
*
* ##### Valid for `true`
*
* ```js
* var a = 2;
*
* // comment
* return a;
*
* function() {
*
* // comment
* }
* ```
*
* ##### Valid
* ##### Valid for `{ "allExcept": "firstAfterCurly" }`
*
* ```js
* var a = 2;
*
* // comment
* return a;
*
* function() {
* // comment
* }
* ```
*
* ##### Invalid
Expand All @@ -26,6 +47,10 @@
* var a = 2;
* //comment
* return a;
*
* function() {
* // comment
* }
* ```
*/

Expand All @@ -36,24 +61,44 @@ module.exports = function() {};
module.exports.prototype = {

configure: function(value) {
assert(
value === true,
'requirePaddingNewLinesBeforeLineComments option requires true value or should be removed'
);
this._allowFirstAfterCurly = false;

if (typeof value === 'object') {
assert(typeof value.allExcept === 'string' && value.allExcept === 'firstAfterCurly',
'requirePaddingNewLinesBeforeLineComments option requires the "allExcept" ' +
'property to equal "firstAfterCurly"');
this._allowFirstAfterCurly = true;
} else {
assert(value === true,
'requirePaddingNewLinesBeforeLineComments option requires true value or object'
);
}
},

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

check: function(file, errors) {
var allowFirstAfterCurly = this._allowFirstAfterCurly;

file.iterateTokensByType('Line', function(comment) {
if (comment.loc.start.line === 1) {
return;
}

var prevToken = file.getPrevToken(comment, {includeComments: true});

if (prevToken.type === 'Line') {
return;
}

if (allowFirstAfterCurly && prevToken.type === 'Punctuator' && prevToken.value === '{') {
return;
}

errors.assert.linesBetween({
token: file.getPrevToken(comment, {includeComments: true}),
token: prevToken,
nextToken: comment,
atLeast: 2,
message: 'Line comments must be preceded with a blank line'
Expand Down
102 changes: 83 additions & 19 deletions test/rules/require-padding-newlines-before-line-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,98 @@ describe('rules/require-padding-newlines-before-line-comments', function() {
beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
checker.configure({ requirePaddingNewLinesBeforeLineComments: true });
});

it('should report missing padding before line comment', function() {
assert(checker.checkString('var a = 2;\n// comment').getErrorCount() === 1);
});
describe('invalid options', function() {
it('should throw if false', function() {
assert.throws(function() {
checker.configure({ requirePaddingNewLinesBeforeLineComments: false });
});
});

it('should report line comment after block comment', function() {
assert(checker.checkString('var a = 2;\n/* comment */\n// comment').getErrorCount() === 1);
});
it('should throw if array', function() {
assert.throws(function() {
checker.configure({ requirePaddingNewLinesBeforeLineComments: [] });
});
});

it('should not report missing padding if comment is first line', function() {
assert(checker.checkString('// comment\nvar a = 2;').isEmpty());
});
it('should throw if empty object', function() {
assert.throws(function() {
checker.configure({ requirePaddingNewLinesBeforeLineComments: {} });
});
});

it('should not report padding before line comment', function() {
assert(checker.checkString('var a = 2;\n\n// comment').isEmpty());
});
it('should throw if not allExcept object', function() {
assert.throws(function() {
checker.configure({ requirePaddingNewLinesBeforeLineComments: { allBut: false} });
});
});

it('should not report additional padding before line comment', function() {
assert(checker.checkString('var a = 2;\n\n\n// comment').isEmpty());
it('should throw if not allExcept firstAfterCurly', function() {
assert.throws(function() {
checker.configure({ requirePaddingNewLinesBeforeLineComments: { allExcept: 'badOptionName'} });
});
});
});

it('should not report missing padding with block comment', function() {
assert(checker.checkString('var a = 2;\n/* comment */').isEmpty());
describe('value true', function() {
beforeEach(function() {
checker.configure({ requirePaddingNewLinesBeforeLineComments: true });
});

it('should report missing padding before line comment', function() {
assert(checker.checkString('var a = 2;\n// comment').getErrorCount() === 1);
});

it('should report line comment after block comment', function() {
assert(checker.checkString('var a = 2;\n/* comment */\n// comment').getErrorCount() === 1);
});

it('should not report multiple line comments', function() {
assert(checker.checkString('// comment\n//foo').isEmpty());
});

it('should report one error if multiple comments dont have line space', function() {
assert(checker.checkString('var a = 2;\n// comment\n// comment').getErrorCount() === 1);
});

it('should not report missing padding if comment is first line', function() {
assert(checker.checkString('// comment\nvar a = 2;').isEmpty());
});

it('should not report padding before line comment', function() {
assert(checker.checkString('var a = 2;\n\n// comment').isEmpty());
});

it('should not report additional padding before line comment', function() {
assert(checker.checkString('var a = 2;\n\n\n// comment').isEmpty());
});

it('should not report missing padding with block comment', function() {
assert(checker.checkString('var a = 2;\n/* comment */').isEmpty());
});

it('should not report line comment after block comment with padding', function() {
assert(checker.checkString('var a = 2;\n/* comment */\n\n// comment').isEmpty());
});

it('should report error if first line after a curly', function() {
assert(checker.checkString('if (true) {\n// comment\n}').getErrorCount() === 1);
});
});

it('should not report line comment after block comment with padding', function() {
assert(checker.checkString('var a = 2;\n/* comment */\n\n// comment').isEmpty());
describe('value allExcept: firstAfterCurly', function() {
beforeEach(function() {
checker.configure({
requirePaddingNewLinesBeforeLineComments: {
allExcept: 'firstAfterCurly'
}
});
});

it('should not report error if first line after a curly', function() {
assert(checker.checkString('if (true) {\n// comment\n}').isEmpty());
assert(checker.checkString('var a = {\n// comment\n};').isEmpty());
});
});
});

0 comments on commit afc670d

Please sign in to comment.