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

add requireCamelCaseOrUpperCaseIdentifiers rule #121

Merged
merged 1 commit into from
Dec 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,26 @@ Example configuration:
*/
"disallowImplicitTypeConversion": ["numeric", "boolean", "binary", "string"],

/*
Option: requireCamelCaseOrUpperCaseIdentifiers
Requires identifiers to be camelCased or UPPERCASE_WITH_UNDERSCORES

Valid example:

var camelCase = 0;
var CamelCase = 1;
var _camelCase = 2;
var camelCase_ = 3;
var UPPER_CASE = 4;

Invalid examples:

var lower_case = 1;
var Mixed_case = 2;
var mixed_Case = 3;
*/
"requireCamelCaseOrUpperCaseIdentifiers": true,

/*
Option: disallowKeywords
Disallows usage of specified keywords.
Expand Down Expand Up @@ -617,7 +637,7 @@ Example configuration:

/*
Option: additionalRules
Pluggable rules
Pluggable rules
*/
"additionalRules": ["project-rules/*.js"],

Expand Down
40 changes: 40 additions & 0 deletions lib/rules/require-camelcase-or-uppercase-identifiers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {

configure: function(requireCamelCaseOrUpperCaseIdentifiers) {
assert(
typeof requireCamelCaseOrUpperCaseIdentifiers === 'boolean',
'requireCamelCaseOrUpperCaseIdentifiers option requires boolean value'
);
assert(
requireCamelCaseOrUpperCaseIdentifiers === true,
'requireCamelCaseOrUpperCaseIdentifiers option requires true value or should be removed'
);
},

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

check: function(file, errors) {
var tokens = file.getTokens();
errors = errors;
for (var i = 0, l = tokens.length; i < l; i++) {
var token = tokens[i];
if (token.type === 'Identifier') {
var value = token.value;
if (value.replace(/^_+|_+$/g, '').indexOf('_') > -1 && value.toUpperCase() !== value) {
errors.add(
'All identifiers must be camelCase or UPPER_CASE',
token.loc.start.line,
token.loc.start.column
);
}
}
}
}

};
1 change: 1 addition & 0 deletions lib/string-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ StringChecker.prototype = {
/* deprecated rules (end) */

this.registerRule(new (require('./rules/disallow-implicit-type-conversion'))());
this.registerRule(new (require('./rules/require-camelcase-or-uppercase-identifiers'))());
this.registerRule(new (require('./rules/disallow-keywords'))());
this.registerRule(new (require('./rules/disallow-multiple-line-breaks'))());
this.registerRule(new (require('./rules/validate-line-breaks'))());
Expand Down
44 changes: 44 additions & 0 deletions test/test.require-camelcase-or-uppercase-identifiers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var Checker = require('../lib/checker');
var assert = require('assert');

describe('rules/require-camelcase-or-uppercase-identifiers', function() {
var checker;

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

it('should report inner all-lowercase underscores', function() {
assert(checker.checkString('var x_y = "x";').getErrorCount() === 1);
});

it('should report inner some-lowercase underscores', function() {
assert(checker.checkString('var X_y = "x";').getErrorCount() === 1);
});

it('should not report inner all-uppercase underscores', function() {
assert(checker.checkString('var X_Y = "x";').isEmpty());
});

it('should not report no underscores', function() {
assert(checker.checkString('var xy = "x";').isEmpty());
});

it('should not report leading underscores', function() {
assert(checker.checkString('var _x = "x", __y = "y";').isEmpty());
});

it('should report trailing underscores', function() {
assert(checker.checkString('var x_ = "x", y__ = "y";').isEmpty());
});

it('should not report underscore.js', function() {
assert(checker.checkString('var extend = _.extend;').isEmpty());
});

it('should not report node globals', function() {
assert(checker.checkString('var a = __dirname + __filename;').isEmpty());
});
});