Duplicate dependencyΒΆ
ID: js/angular/duplicate-dependency
Kind: problem
Security severity:
Severity: warning
Precision: very-high
Tags:
- quality
- maintainability
- readability
- frameworks/angularjs
Query suites:
- javascript-security-and-quality.qls
Click to see the query in the CodeQL repository
Dependency injection in AngularJS is done by providing the names of the desired dependencies. Providing the same name multiple times is redundant since the AngularJS injector uses a cache for instantiated dependencies.
RecommendationΒΆ
Only include the name of each dependency once.
ExampleΒΆ
The following example shows an AngularJS controller with $cookies
as a duplicate dependency.
angular.module('myModule', [])
.controller('MyController', ['$scope',
'$cookies',
'$cookies', // REDUNDANT
function($scope, , $cookies1, $cookies2) {
// ...
});
This is problematic, since the programmer could be led to believe that the two parameters $cookies1
and $cookies2
are different instances, which they are not.
Instead, the dependency should only be listed once:
angular.module('myModule', [])
.controller('MyController', ['$scope',
'$cookies',
function($scope, $cookies) {
// ...
});
ReferencesΒΆ
AngularJS Developer Guide: Dependency Injection.