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

add requireCapitalizedConstructors rule #119

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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,21 @@ Example configuration:
*/
"requireLineFeedAtFileEnd": true,

/*
Option: requireCapitalizedConstructors
Requires constructors to be capitalized (except for "this")

Valid example:

var a = new B();
var c = new this();

Invalid example:

var d = new e();
*/
"requireCapitalizedConstructors": true,

/*
Option: safeContextKeyword
Option to check "var that = this" expressions
Expand Down
36 changes: 36 additions & 0 deletions lib/rules/require-capitalized-constructors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {

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

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

check: function(file, errors) {
file.iterateNodesByType('NewExpression', function(node) {
if (node.callee.type === 'Identifier' &&
node.callee.name[0].toUpperCase() !== node.callee.name[0]
) {
errors.add(
'Constructor functions should be capitalized',
node.callee.loc.start.line,
node.callee.loc.start.column
);
}
});
}

};
2 changes: 2 additions & 0 deletions lib/string-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ StringChecker.prototype = {
this.registerRule(new (require('./rules/require-spaces-in-function-expression'))());
this.registerRule(new (require('./rules/disallow-spaces-in-function-expression'))());

this.registerRule(new (require('./rules/require-capitalized-constructors'))());

this.registerRule(new (require('./rules/safe-context-keyword'))());

this.registerRule(new (require('./rules/require-dot-notation'))());
Expand Down
28 changes: 28 additions & 0 deletions test/test.require-capitalizated-constructors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var Checker = require('../lib/checker');
var assert = require('assert');

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

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

it('should report uncapitalized construction', function() {
assert(checker.checkString('var x = new y();').getErrorCount() === 1);
});

it('should not report capitalized construction', function() {
assert(checker.checkString('var x = new Y();').isEmpty());
});

it('should not report member expression construction', function() {
assert(checker.checkString('var x = new ns.y();').isEmpty());
});

it('should not report construction with "this" keyword', function() {
assert(checker.checkString('var x = new this();').isEmpty());
});
});