From a7051ace5d03056e789de4bffc9ee0632c27ac1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Mon, 11 May 2015 18:28:11 +0200 Subject: [PATCH] Add metrics queriesWithoutResults, jQueryCalls, jQueryCallsOnEmptyObject, jQueryNotDelegatedEvent --- .../src/js/directives/offendersDirectives.js | 6 +++-- lib/tools/jsExecutionTransformer.js | 24 +++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/front/src/js/directives/offendersDirectives.js b/front/src/js/directives/offendersDirectives.js index 9ef94b2..c147520 100644 --- a/front/src/js/directives/offendersDirectives.js +++ b/front/src/js/directives/offendersDirectives.js @@ -670,8 +670,10 @@ if (node.data.callDetails.context && node.data.callDetails.context.length === 0) { html += '

Called on 0 jQuery element

Useless function call, as the jQuery object is empty.

'; - } else if (node.data.type === 'jQuery - bind' && node.data.callDetails.context.length > 5) { - html += '

The .bind() method attaches the event listener to each jQuery element one by one. Using the .on() method is preferable if available (from v1.7).

'; + } else if (node.data.type === 'jQuery - bind' && node.data.callDetails.context.length > 3) { + html += '

The .bind() method attaches the event listener to each jQuery element one by one. Using the .on() method with event delegation is preferable if available (from v1.7).

'; + } else if (node.data.type === 'jQuery - on' && node.data.callDetails.context.length > 3) { + html += '

The .on() method used this way attaches the event listener to each jQuery element one by one. Using the event delegation version of the method is preferable if available (from v1.7).

'; } if (node.data.resultsNumber === 0) { diff --git a/lib/tools/jsExecutionTransformer.js b/lib/tools/jsExecutionTransformer.js index 188623e..577d255 100644 --- a/lib/tools/jsExecutionTransformer.js +++ b/lib/tools/jsExecutionTransformer.js @@ -10,10 +10,11 @@ var jsExecutionTransformer = function() { var metrics = { DOMaccesses: 0, + DOMaccessesOnScroll: 0, queriesWithoutResults: 0, jQueryCalls: 0, jQueryCallsOnEmptyObject: 0, - DOMaccessesOnScroll: 0 + jQueryNotDelegatedEvent: 0 }; try { @@ -24,14 +25,27 @@ var jsExecutionTransformer = function() { if (javascriptExecutionTree.children) { javascriptExecutionTree.children.forEach(function(node) { - // Mark abnormal things with a warning flag var contextLenght = (node.data.callDetails && node.data.callDetails.context) ? node.data.callDetails.context.length : null; - if ((node.data.type === 'jQuery - bind' && contextLenght > 5) || - node.data.resultsNumber === 0 || - contextLenght === 0) { + + if ((node.data.type === 'jQuery - bind' || node.data.type === 'jQuery - on') && contextLenght > 3) { + metrics.jQueryNotDelegatedEvent += contextLenght - 1; node.warning = true; } + if (node.data.resultsNumber === 0) { + metrics.queriesWithoutResults ++; + node.warning = true; + } + + if (contextLenght === 0) { + metrics.jQueryCallsOnEmptyObject ++; + node.warning = true; + } + + if (node.data.type.indexOf('jQuery - ') === 0) { + metrics.jQueryCalls ++; + } + // Mark errors with an error flag if (node.data.type === 'error' || node.data.type === 'jQuery version change') { node.error = true;