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

Commit 9d842a1

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 - Refactor according to CR
1 parent fb7db4a commit 9d842a1

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
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 assertValidDirectiveName(name) {
783+
var letter = name.charAt(0);
784+
if (letter && (letter !== lowercase(letter))) {
785+
throw ngMinErr('badname', "Directive '{0}' is invalid. The first letter of a directive must be a lowercase letter");
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+
assertValidDirectiveName(name);
800809
assertArg(directiveFactory, 'directiveFactory');
801810
if (!hasDirectives.hasOwnProperty(name)) {
802811
hasDirectives[name] = [];

test/ng/compileSpec.js

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

148148

149149
describe('configuration', function() {
150+
151+
it('should fail to register a directive that does not start with a lowercase letter', function() {
152+
module(function($provide, $compileProvider) {
153+
expect($compileProvider.directive('BadDirectiveName', function() {
154+
return {};
155+
})).toThrowMinErr('badname', "Directive 'BadDirectiveName' is invalid. The first letter of a directive must be a lowercase letter");
156+
});
157+
});
158+
150159
it('should register a directive', function() {
151160
module(function() {
152161
directive('div', function(log) {
@@ -7429,4 +7438,4 @@ describe('$compile', function() {
74297438
expect(element.hasClass('fire')).toBe(true);
74307439
}));
74317440
});
7432-
});
7441+
});

0 commit comments

Comments
 (0)