浏览代码

Merge phantomas 1.7.0 new functionnalities

Gaël Métais 10 年之前
父节点
当前提交
aa34f60ce9
共有 3 个文件被更改,包括 13 次插入22 次删除
  1. 2 2
      app/node_views/results.html
  2. 1 13
      app/public/scripts/resultsCtrl.js
  3. 10 7
      phantomas_custom/modules/domQYLT/domQYLT.js

+ 2 - 2
app/node_views/results.html

@@ -132,10 +132,10 @@
                                     </modal-dialog>
                                 </div>
                             </div>
-                            <div ng-if="duplicatedQueriesCountAll" ng-class="{'warning': duplicatedQueriesCountAll > 500}">
+                            <div ng-if="phantomasResults.metrics.DOMqueriesAvoidable" ng-class="{'warning': phantomasResults.metrics.DOMqueriesAvoidable > 500}">
                                 <div class="label">Avoidable queries</div>
                                 <div class="result">
-                                    {{duplicatedQueriesCountAll}}
+                                    {{phantomasResults.metrics.DOMqueriesAvoidable}}
                                     <show-offenders modal-title="Duplicated DOM queries" metric-name="DOMqueriesDuplicated" phantomas-results="phantomasResults"></show-offenders>
                                 </div>
                                 <div class="info">

+ 1 - 13
app/public/scripts/resultsCtrl.js

@@ -64,18 +64,6 @@ app.controller('ResultsCtrl', function ($scope) {
             }
         });
 
-        // Read all the duplicated queries and calculate a more appropriated score
-        $scope.duplicatedQueriesCountAll = 0;
-        if ($scope.phantomasResults.offenders.DOMqueriesDuplicated) {
-            var regex = /\): *(\d+) queries$/;
-            $scope.phantomasResults.offenders.DOMqueriesDuplicated.forEach(function(query) {
-                var regexResult = regex.exec(query);
-                if (regexResult) {
-                    $scope.duplicatedQueriesCountAll += parseInt(regexResult[1], 10) - 1;
-                }
-            });
-        }
-
         // Grab the notes
         $scope.notations = {
             domComplexity: getDomComplexityScore(),
@@ -192,7 +180,7 @@ app.controller('ResultsCtrl', function ($scope) {
         var note = 'A';
         var score = $scope.phantomasResults.metrics.DOMinserts * 2 +
                     $scope.phantomasResults.metrics.DOMqueries +
-                    $scope.duplicatedQueriesCountAll * 2 +
+                    $scope.phantomasResults.metrics.DOMqueriesAvoidable * 2 +
                     $scope.phantomasResults.metrics.eventsBound;
         if (score > 300) {
             note = 'B';

+ 10 - 7
phantomas_custom/modules/domQYLT/domQYLT.js

@@ -3,7 +3,7 @@
  */
 /* global Element: true, Document: true, Node: true, window: true */
 
-exports.version = '0.7.a';
+exports.version = '0.9.a';
 
 exports.module = function(phantomas) {
     'use strict';
@@ -14,7 +14,8 @@ exports.module = function(phantomas) {
     phantomas.setMetric('DOMqueriesByTagName'); // @desc number of document.getElementsByTagName calls
     phantomas.setMetric('DOMqueriesByQuerySelectorAll'); // @desc number of document.querySelector(All) calls
     phantomas.setMetric('DOMinserts'); // @desc number of DOM nodes inserts
-    phantomas.setMetric('DOMqueriesDuplicated'); // @desc number of duplicated DOM queries
+    phantomas.setMetric('DOMqueriesDuplicated'); // @desc number of DOM queries called more than once
+    phantomas.setMetric('DOMqueriesAvoidable'); // @desc number of repeated uses of a duplicated query
 
     // fake native DOM functions
     phantomas.once('init', function() {
@@ -26,6 +27,7 @@ exports.module = function(phantomas) {
 
                 phantomas.spy(Document.prototype, 'getElementById', function(id) {
                     phantomas.incrMetric('DOMqueriesById');
+                    phantomas.addOffender('DOMqueriesById', '#%s (in %s)', id, '#document');
                     querySpy('id', '#' + id, 'getElementById', '#document');
 
                     phantomas.enterContext({
@@ -53,7 +55,7 @@ exports.module = function(phantomas) {
                     var context = phantomas.getDOMPath(this);
 
                     phantomas.incrMetric('DOMqueriesByClassName');
-                    phantomas.addOffender('DOMqueriesByClassName', '.' + className);
+                    phantomas.addOffender('DOMqueriesByClassName', '.%s (in %s)', className, context);
                     querySpy('class', '.' + className, 'getElementsByClassName', context);
 
                     phantomas.enterContext({
@@ -85,7 +87,7 @@ exports.module = function(phantomas) {
                     var context = phantomas.getDOMPath(this);
 
                     phantomas.incrMetric('DOMqueriesByTagName');
-                    phantomas.addOffender('DOMqueriesByTagName', tagName);
+                    phantomas.addOffender('DOMqueriesByTagName', '%s (in %s)', tagName, context);
                     querySpy('tag name', tagName.toLowerCase(), 'getElementsByTagName', context);
 
                     phantomas.enterContext({
@@ -113,8 +115,8 @@ exports.module = function(phantomas) {
                 // selector queries
                 function selectorQuerySpy(selector, context) {
                     phantomas.incrMetric('DOMqueriesByQuerySelectorAll');
-                    phantomas.addOffender('DOMqueriesByQuerySelectorAll', selector);
-                    querySpy('selector', selector, 'querySelectorAll');
+                    phantomas.addOffender('DOMqueriesByQuerySelectorAll', '%s (in %s)', selector, context);
+                    querySpy('selector', selector, 'querySelectorAll', context);
                 }
 
                 function selectorQuerySpyBefore(selector) {
@@ -248,7 +250,7 @@ exports.module = function(phantomas) {
         DOMqueries = new Collection();
 
     phantomas.on('domQuery', function(type, query, fnName, context) {
-        //phantomas.log('DOM query: by %s - "%s" (using %s on context %s)', type, query, fnName, context);
+        phantomas.log('DOM query: by %s - "%s" (using %s) in %s', type, query, fnName, context);
         phantomas.incrMetric('DOMqueries');
 
         if (context && context.indexOf('DocumentFragment') === -1) {
@@ -260,6 +262,7 @@ exports.module = function(phantomas) {
         DOMqueries.sort().forEach(function(query, cnt) {
             if (cnt > 1) {
                 phantomas.incrMetric('DOMqueriesDuplicated');
+                phantomas.incrMetric('DOMqueriesAvoidable', cnt - 1);
                 phantomas.addOffender('DOMqueriesDuplicated', '%s: %d queries', query, cnt);
             }
         });