Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 6389355

Browse files
committed
feat($compile): Throw error on incorrect casing
- Adds error message for when the first letter of the directive name is not capitalized in the definition
1 parent fb7db4a commit 6389355

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/ng/compile.js

+9
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
779779
return bindings;
780780
}
781781

782+
function assertFirstLetterIsLowerCased(name) {
783+
var letter = name.charAt(0);
784+
if (letter !== letter.toLowerCase()) {
785+
throw ngMinErr('badname', "Directive '{0}' is not a valid name. Make sure the first letter is lowercase");
786+
}
787+
return name;
788+
}
789+
782790
/**
783791
* @ngdoc method
784792
* @name $compileProvider#directive
@@ -797,6 +805,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
797805
this.directive = function registerDirective(name, directiveFactory) {
798806
assertNotHasOwnProperty(name, 'directive');
799807
if (isString(name)) {
808+
assertFirstLetterIsLowerCased(name);
800809
assertArg(directiveFactory, 'directiveFactory');
801810
if (!hasDirectives.hasOwnProperty(name)) {
802811
hasDirectives[name] = [];

test/ng/compileSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ describe('$compile', function() {
147147

148148

149149
describe('configuration', function() {
150+
150151
it('should register a directive', function() {
151152
module(function() {
152153
directive('div', function(log) {
@@ -7430,3 +7431,19 @@ describe('$compile', function() {
74307431
}));
74317432
});
74327433
});
7434+
7435+
describe('$compile', function() {
7436+
describe('directive', function() {
7437+
it('should fail to register a directive', function() {
7438+
try {
7439+
module(provideLog, function($provide, $compileProvider) {
7440+
$compileProvider.directive('BadDirectiveName', function(log) {
7441+
return {};
7442+
});
7443+
});
7444+
} catch (e) {
7445+
expect(e).toBe("Directive 'BadDirectiveName' is not a valid name. Make sure the first letter is lowercase")
7446+
}
7447+
});
7448+
});
7449+
});

0 commit comments

Comments
 (0)