Skip to content

Commit d35cbad

Browse files
committed
feat($compile): throw error when directive name or factory fn is invalid
Closes: angular#15056
1 parent e50e91c commit d35cbad

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/ng/compile.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1087,10 +1087,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
10871087
* @returns {ng.$compileProvider} Self for chaining.
10881088
*/
10891089
this.directive = function registerDirective(name, directiveFactory) {
1090+
assertArg(name, 'name');
10901091
assertNotHasOwnProperty(name, 'directive');
10911092
if (isString(name)) {
10921093
assertValidDirectiveName(name);
1093-
assertArg(directiveFactory, 'directiveFactory');
1094+
assertArgFn(directiveFactory, 'directiveFactory', true);
10941095
if (!hasDirectives.hasOwnProperty(name)) {
10951096
hasDirectives[name] = [];
10961097
$provide.factory(name + Suffix, ['$injector', '$exceptionHandler',

test/ng/compileSpec.js

+28
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ describe('$compile', function() {
214214
});
215215
inject(function($compile) {});
216216
});
217+
217218
it('should throw an exception if a directive name has leading or trailing whitespace', function() {
218219
module(function() {
219220
function assertLeadingOrTrailingWhitespaceInDirectiveName(name) {
@@ -230,6 +231,33 @@ describe('$compile', function() {
230231
inject(function($compile) {});
231232
});
232233

234+
it('should throw an exception if the directive name is not defined', function() {
235+
module(function() {
236+
expect(function() {
237+
directive();
238+
}).toThrowMinErr('ng','areq');
239+
});
240+
inject(function($compile) {});
241+
});
242+
243+
it('should throw an exception if the directive factory is not a function', function() {
244+
module(function() {
245+
expect(function() {
246+
directive('myDir');
247+
}).toThrowMinErr('ng','areq');
248+
});
249+
inject(function($compile) {});
250+
});
251+
252+
it('should accept array notation in the directive factory function', function() {
253+
module(function() {
254+
directive('myDir', ['log', function(log) {
255+
expect(log).toBe(jasmine.any('Function'));
256+
}]);
257+
});
258+
inject(function($compile) {});
259+
});
260+
233261
it('should preserve context within declaration', function() {
234262
module(function() {
235263
directive('ff', function(log) {

0 commit comments

Comments
 (0)