Skip to content

Commit 8be4f95

Browse files
wesleychonetman92
authored andcommitted
fix($compile): throw error on invalid directive name
Directive names must start with a lower case letter. Previously the compiler would quietly fail. This change adds an assertion that fails if this is not the case. Closes angular#11281 Closes angular#11109
1 parent 083d6f1 commit 8be4f95

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@ngdoc error
2+
@name $compile:baddir
3+
@fullName Invalid Directive Name
4+
@description
5+
6+
This error occurs when the name of a directive is not valid.
7+
8+
Directives must start with a lowercase character.

src/ng/compile.js

+9
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
790790
return bindings;
791791
}
792792

793+
function assertValidDirectiveName(name) {
794+
var letter = name.charAt(0);
795+
if (!letter || letter !== lowercase(letter)) {
796+
throw $compileMinErr('baddir', "Directive name '{0}' is invalid. The first character must be a lowercase letter", name);
797+
}
798+
return name;
799+
}
800+
793801
/**
794802
* @ngdoc method
795803
* @name $compileProvider#directive
@@ -808,6 +816,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
808816
this.directive = function registerDirective(name, directiveFactory) {
809817
assertNotHasOwnProperty(name, 'directive');
810818
if (isString(name)) {
819+
assertValidDirectiveName(name);
811820
assertArg(directiveFactory, 'directiveFactory');
812821
if (!hasDirectives.hasOwnProperty(name)) {
813822
hasDirectives[name] = [];

test/ng/compileSpec.js

+10
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) {
@@ -201,6 +202,15 @@ describe('$compile', function() {
201202
});
202203
inject(function($compile) {});
203204
});
205+
206+
it('should throw an exception if a directive name starts with a non-lowercase letter', function() {
207+
module(function() {
208+
expect(function() {
209+
directive('BadDirectiveName', function() { });
210+
}).toThrowMinErr('$compile','baddir', "Directive name 'BadDirectiveName' is invalid. The first character must be a lowercase letter");
211+
});
212+
inject(function($compile) {});
213+
});
204214
});
205215

206216

0 commit comments

Comments
 (0)