From 2275d72e59fc37a76acf46a9b1a3247b4d474a71 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 21 Aug 2016 23:20:57 +0200 Subject: [PATCH] SonarQube JavaScript custom plugin, improvements in description --- .../rules/custom/GUJS0001_javascript.html | 23 ++++++++++++---------- .../rules/custom/GUJS0001_javascript.json | 5 +++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Sonar/Plugins/sonar-custom-javascript-plugin/src/main/resources/de/example/l10n/javascript/rules/custom/GUJS0001_javascript.html b/Sonar/Plugins/sonar-custom-javascript-plugin/src/main/resources/de/example/l10n/javascript/rules/custom/GUJS0001_javascript.html index ab6882f..16fffe2 100644 --- a/Sonar/Plugins/sonar-custom-javascript-plugin/src/main/resources/de/example/l10n/javascript/rules/custom/GUJS0001_javascript.html +++ b/Sonar/Plugins/sonar-custom-javascript-plugin/src/main/resources/de/example/l10n/javascript/rules/custom/GUJS0001_javascript.html @@ -1,18 +1,21 @@ -

Custom rule description.

+

AngularJS Controllers are instantiated every time a web page is loaded. $rootScope.$on do not automatically release the pointer to +the Controller where it was called and $rootScope is an AngularJS singleton. After a couple of web page refreshes we end up having +many instantiated Controllers that will not be used and they can not be reclaimed by the garbage collector because there is a reference to them +from $rootScope. As a good practice we will not use $rootScope.$on in AngularJS Controllers.

Noncompliant Code Example

-class MyClass {
-	
-	int foo1(int value) { return 0; }
-		
-	MyClass foo2(MyClass value) { return null; }
-	
-	...
- 
+function MyController($rootScope, USERS) {
+    var vm = this;
+
+    $rootScope.$on(USERS.ROOTSCOPE.BROADCAST, usersChildOnRootBroadcast); //$rootScope is a singleton, it is not released and either MyController
+        
+    function usersChildOnRootBroadcast(events, broadcastUser) {
+        vm.broadcastUser = broadcastUser;
+    }
 }
 

See

diff --git a/Sonar/Plugins/sonar-custom-javascript-plugin/src/main/resources/de/example/l10n/javascript/rules/custom/GUJS0001_javascript.json b/Sonar/Plugins/sonar-custom-javascript-plugin/src/main/resources/de/example/l10n/javascript/rules/custom/GUJS0001_javascript.json index 928a6af..82d0ab8 100644 --- a/Sonar/Plugins/sonar-custom-javascript-plugin/src/main/resources/de/example/l10n/javascript/rules/custom/GUJS0001_javascript.json +++ b/Sonar/Plugins/sonar-custom-javascript-plugin/src/main/resources/de/example/l10n/javascript/rules/custom/GUJS0001_javascript.json @@ -1,12 +1,13 @@ { - "title": "Function names should comply with a naming convention", + "title": "Do not use $rootScope.$on in AngularJS Controllers", "status": "ready", "remediation": { "func": "Constant\/Issue", "constantCost": "5min" }, "tags": [ - "bug" + "bug", + "angularjs" ], "defaultSeverity": "Major" } -- 2.1.4