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

Commit

Permalink
Merge 7c5ecc7 into 467a170
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjoelkemp committed Sep 26, 2014
2 parents 467a170 + 7c5ecc7 commit 740564b
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 0 deletions.
78 changes: 78 additions & 0 deletions README.md
Expand Up @@ -2941,6 +2941,84 @@ function a(b, c) {}
function a(b , c) {}
```

### requireCapitalizedComments

Requires the first alphabetical character of a comment to be uppercase

Type: `Boolean`

Value: `true`

#### Example

`requireCapitalizedComments: true`

Valid:

```
// Valid
//Valid
/*
Valid
*/
/**
* Valid
*/
// 123 or any non-alphabetical starting character
```

Invalid:
```
// invalid
//invalid
/** invalid */
/**
* invalid
*/
```

### disallowCapitalizedComments

Requires the first alphabetical character of a comment to be lowercase.

Type: `String`

Value: `true`

#### Example

`disallowCapitalizedComments: true`

Valid:

```
// valid
//valid
/*
valid
*/
/**
* valid
*/
// 123 or any non-alphabetical starting character
```

Invalid:
```
// Invalid
//Invalid
/** Invalid */
/**
* Invalid
*/
```

### ~~validateJSDoc~~

Please use the [JSCS-JSDoc](https://github.com/jscs-dev/jscs-jsdoc) plugin instead.
Expand Down
34 changes: 34 additions & 0 deletions lib/rules/disallow-capitalized-comments.js
@@ -0,0 +1,34 @@
var assert = require('assert');

module.exports = function() {};

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

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

check: function(file, errors) {
var lowerCasePattern = /[a-z]/;
var alphabeticalPattern = /[a-z|A-Z]/;
var _this = this;

file.getComments().forEach(function(comment) {
var stripped = comment.value.replace(/[\n\s\*]/g, '');
var firstChar = stripped[0];

if (alphabeticalPattern.test(firstChar) && !lowerCasePattern.test(firstChar)) {
errors.add(
'Comments must start with a lowercase letter',
comment.loc.start
);
}
});
}
};
34 changes: 34 additions & 0 deletions lib/rules/require-capitalized-comments.js
@@ -0,0 +1,34 @@
var assert = require('assert');

module.exports = function() {};

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

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

check: function(file, errors) {
var upperCasePattern = /[A-Z]/;
var alphabeticalPattern = /[a-z|A-Z]/;
var _this = this;

file.getComments().forEach(function(comment) {
var stripped = comment.value.replace(/[\n\s\*]/g, '');
var firstChar = stripped[0];

if (alphabeticalPattern.test(firstChar) && !upperCasePattern.test(firstChar)) {
errors.add(
'Comments must start with an uppercase letter',
comment.loc.start
);
}
});
}
};
3 changes: 3 additions & 0 deletions lib/string-checker.js
Expand Up @@ -134,6 +134,9 @@ StringChecker.prototype = {

this.registerRule(new (require('./rules/require-function-declarations'))());
this.registerRule(new (require('./rules/disallow-function-declarations'))());

this.registerRule(new (require('./rules/require-capitalized-comments'))());
this.registerRule(new (require('./rules/disallow-capitalized-comments'))());
},

/**
Expand Down
35 changes: 35 additions & 0 deletions test/rules/disallow-capitalized-comments.js
@@ -0,0 +1,35 @@
var Checker = require('../../lib/checker');
var assert = require('assert');

describe('rules/disallow-capitalized-comments', function() {
var checker;

beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
checker.configure({ disallowCapitalizedComments: true });
});

it('should report on an uppercase start of a comment', function() {
assert(checker.checkString('//Invalid').getErrorCount() === 1);
assert(checker.checkString('// Invalid').getErrorCount() === 1);
assert(checker.checkString('/** Invalid */').getErrorCount() === 1);
assert(checker.checkString('/**\n * Invalid\n */').getErrorCount() === 1);
assert(checker.checkString('/* Invalid */').getErrorCount() === 1);
assert(checker.checkString('/*\n Invalid\n */').getErrorCount() === 1);
});

it('should not report on a lowercase start of a comment', function() {
assert(checker.checkString('//valid').isEmpty());
assert(checker.checkString('// valid').isEmpty());
assert(checker.checkString('/** valid */').isEmpty());
});

it('should not report on comments that start with a non-alphabetical character', function() {
assert(checker.checkString('//123').isEmpty());
assert(checker.checkString('// 123').isEmpty());
assert(checker.checkString('/**123*/').isEmpty());
assert(checker.checkString('/**\n @todo: foobar\n */').isEmpty());
assert(checker.checkString('/** 123*/').isEmpty());
});
});
34 changes: 34 additions & 0 deletions test/rules/require-capitalized-comments.js
@@ -0,0 +1,34 @@
var Checker = require('../../lib/checker');
var assert = require('assert');

describe('rules/require-capitalized-comments', function() {
var checker;

beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
checker.configure({ requireCapitalizedComments: true });
});

it('should report on a lowercase start of a comment', function() {
assert(checker.checkString('//invalid').getErrorCount() === 1);
assert(checker.checkString('// invalid').getErrorCount() === 1);
assert(checker.checkString('/** invalid */').getErrorCount() === 1);
assert(checker.checkString('/* invalid */').getErrorCount() === 1);
assert(checker.checkString('/**\n * invalid\n*/').getErrorCount() === 1);
});

it('should not report an uppercase start of a comment', function() {
assert(checker.checkString('//Valid').isEmpty());
assert(checker.checkString('// Valid').isEmpty());
assert(checker.checkString('/** Valid */').isEmpty());
});

it('should not report on comments that start with a non-alphabetical character', function() {
assert(checker.checkString('//123').isEmpty());
assert(checker.checkString('// 123').isEmpty());
assert(checker.checkString('/**123*/').isEmpty());
assert(checker.checkString('/**\n @todo: foobar\n */').isEmpty());
assert(checker.checkString('/** 123*/').isEmpty());
});
});

0 comments on commit 740564b

Please sign in to comment.