From ddb95b26531f4b8e58744f5357b4200c81b2750b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Mon, 3 Nov 2014 16:00:13 +0100 Subject: [PATCH] Warn when a query returns 0 results in timeline --- app/node_views/results.html | 8 ++- app/public/styles/less/results.less | 3 + app/public/styles/results.css | 3 + phantomas_custom/core/scopeYLT/scopeYLT.js | 12 +++- phantomas_custom/modules/domQYLT/domQYLT.js | 59 +++++++++++++++---- .../modules/eventListYLT/eventListYLT.js | 10 +++- phantomas_custom/modules/jQYLT/jQYLT.js | 22 +++---- 7 files changed, 88 insertions(+), 29 deletions(-) diff --git a/app/node_views/results.html b/app/node_views/results.html index 11938f1..8b34f1d 100644 --- a/app/node_views/results.html +++ b/app/node_views/results.html @@ -767,8 +767,8 @@
First one is: {{node.data.callDetails.context.firstElementPath}}

+

+ The query returned 0 results. It looks like unused or dead code! +

+

Backtrace

diff --git a/app/public/styles/less/results.less b/app/public/styles/less/results.less index 7749027..fbd9d64 100644 --- a/app/public/styles/less/results.less +++ b/app/public/styles/less/results.less @@ -371,6 +371,9 @@ input.textFilter { color: #f1c40f; cursor: pointer; } +.table .details .icon-warning { + cursor: pointer; +} .detailsOverlay { position: absolute; diff --git a/app/public/styles/results.css b/app/public/styles/results.css index e771846..05a0ac7 100644 --- a/app/public/styles/results.css +++ b/app/public/styles/results.css @@ -343,6 +343,9 @@ input.textFilter { color: #f1c40f; cursor: pointer; } +.table .details .icon-warning { + cursor: pointer; +} .detailsOverlay { position: absolute; right: 3em; diff --git a/phantomas_custom/core/scopeYLT/scopeYLT.js b/phantomas_custom/core/scopeYLT/scopeYLT.js index 7bead6b..9ed515a 100644 --- a/phantomas_custom/core/scopeYLT/scopeYLT.js +++ b/phantomas_custom/core/scopeYLT/scopeYLT.js @@ -62,7 +62,7 @@ exports.module = function(phantomas) { // After if (enabled && callbackAfter) { - callbackAfter.apply(this, arguments); + callbackAfter.call(this, result); } } @@ -115,9 +115,17 @@ exports.module = function(phantomas) { } // Save given data in the current context and jump change current context to its parent - function leaveContext() { + function leaveContext(moreData) { if (depth === 1 || deepAnalysis) { currentContext.data.time = Date.now() - currentContext.data.timestamp - responseEndTime; + + // Merge previous data with moreData (ovewrites if exists) + if (moreData) { + for (var key in moreData) { + currentContext.data[key] = moreData[key]; + } + } + var parent = currentContext.parent; if (parent === null) { console.error('Error: trying to close root context in ContextTree'); diff --git a/phantomas_custom/modules/domQYLT/domQYLT.js b/phantomas_custom/modules/domQYLT/domQYLT.js index 9c80e22..362e6c6 100644 --- a/phantomas_custom/modules/domQYLT/domQYLT.js +++ b/phantomas_custom/modules/domQYLT/domQYLT.js @@ -39,7 +39,12 @@ exports.module = function(phantomas) { backtrace: phantomas.getBacktrace() }); - }, phantomas.leaveContext); + }, function(result) { + var moreData = { + resultsNumber : result ? 1 : 0 + }; + phantomas.leaveContext(moreData); + }); // selectors by class name function selectorClassNameSpyBefore(className) { @@ -63,8 +68,15 @@ exports.module = function(phantomas) { }); } - phantomas.spy(Document.prototype, 'getElementsByClassName', selectorClassNameSpyBefore, phantomas.leaveContext); - phantomas.spy(Element.prototype, 'getElementsByClassName', selectorClassNameSpyBefore, phantomas.leaveContext); + function selectorClassNameAfter(result) { + var moreData = { + resultsNumber : (result && result.length > 0) ? result.length : 0 + }; + phantomas.leaveContext(moreData); + } + + phantomas.spy(Document.prototype, 'getElementsByClassName', selectorClassNameSpyBefore, selectorClassNameAfter); + phantomas.spy(Element.prototype, 'getElementsByClassName', selectorClassNameSpyBefore, selectorClassNameAfter); // selectors by tag name function selectorTagNameSpyBefore(tagName) { @@ -88,8 +100,15 @@ exports.module = function(phantomas) { }); } - phantomas.spy(Document.prototype, 'getElementsByTagName', selectorTagNameSpyBefore, phantomas.leaveContext); - phantomas.spy(Element.prototype, 'getElementsByTagName', selectorTagNameSpyBefore, phantomas.leaveContext); + function selectorTagNameSpyAfter(result) { + var moreData = { + resultsNumber : (result && result.length > 0) ? result.length : 0 + }; + phantomas.leaveContext(moreData); + } + + phantomas.spy(Document.prototype, 'getElementsByTagName', selectorTagNameSpyBefore, selectorTagNameSpyAfter); + phantomas.spy(Element.prototype, 'getElementsByTagName', selectorTagNameSpyBefore, selectorTagNameSpyAfter); // selector queries function selectorQuerySpy(selector, context) { @@ -116,6 +135,13 @@ exports.module = function(phantomas) { }); } + function selectorQuerySpyAfter(result) { + var moreData = { + resultsNumber : result ? 1 : 0 + }; + phantomas.leaveContext(moreData); + } + function selectorAllQuerySpyBefore(selector) { /*jshint validthis: true */ @@ -134,10 +160,17 @@ exports.module = function(phantomas) { }); } - phantomas.spy(Document.prototype, 'querySelector', selectorQuerySpyBefore, phantomas.leaveContext); - phantomas.spy(Document.prototype, 'querySelectorAll', selectorAllQuerySpyBefore, phantomas.leaveContext); - phantomas.spy(Element.prototype, 'querySelector', selectorQuerySpyBefore, phantomas.leaveContext); - phantomas.spy(Element.prototype, 'querySelectorAll', selectorAllQuerySpyBefore, phantomas.leaveContext); + function selectorAllQuerySpryAfter(result) { + var moreData = { + resultsNumber : (result && result.length > 0) ? result.length : 0 + }; + phantomas.leaveContext(moreData); + } + + phantomas.spy(Document.prototype, 'querySelector', selectorQuerySpyBefore, selectorQuerySpyAfter); + phantomas.spy(Document.prototype, 'querySelectorAll', selectorAllQuerySpyBefore, selectorAllQuerySpryAfter); + phantomas.spy(Element.prototype, 'querySelector', selectorQuerySpyBefore, selectorQuerySpyAfter); + phantomas.spy(Element.prototype, 'querySelectorAll', selectorAllQuerySpyBefore, selectorAllQuerySpryAfter); // count DOM inserts @@ -199,8 +232,12 @@ exports.module = function(phantomas) { }); } - phantomas.spy(Node.prototype, 'appendChild', appendChildSpyBefore, phantomas.leaveContext); - phantomas.spy(Node.prototype, 'insertBefore', insertBeforeSpyBefore, phantomas.leaveContext); + phantomas.spy(Node.prototype, 'appendChild', appendChildSpyBefore, function(result) { + phantomas.leaveContext(); + }); + phantomas.spy(Node.prototype, 'insertBefore', insertBeforeSpyBefore, function(result) { + phantomas.leaveContext(); + }); })(window.__phantomas); }); }); diff --git a/phantomas_custom/modules/eventListYLT/eventListYLT.js b/phantomas_custom/modules/eventListYLT/eventListYLT.js index 3efbb0d..c1620fc 100644 --- a/phantomas_custom/modules/eventListYLT/eventListYLT.js +++ b/phantomas_custom/modules/eventListYLT/eventListYLT.js @@ -35,9 +35,13 @@ exports.module = function(phantomas) { }); } - phantomas.spy(Element.prototype, 'addEventListener', eventSpyBefore, phantomas.leaveContext); - phantomas.spy(Document.prototype, 'addEventListener', eventSpyBefore, phantomas.leaveContext); - phantomas.spy(window, 'addEventListener', eventSpyBefore, phantomas.leaveContext); + function eventSpyAfter(result) { + phantomas.leaveContext(); + } + + phantomas.spy(Element.prototype, 'addEventListener', eventSpyBefore, eventSpyAfter); + phantomas.spy(Document.prototype, 'addEventListener', eventSpyBefore, eventSpyAfter); + phantomas.spy(window, 'addEventListener', eventSpyBefore, eventSpyAfter); })(window.__phantomas); }); }); diff --git a/phantomas_custom/modules/jQYLT/jQYLT.js b/phantomas_custom/modules/jQYLT/jQYLT.js index b15e362..149fce0 100644 --- a/phantomas_custom/modules/jQYLT/jQYLT.js +++ b/phantomas_custom/modules/jQYLT/jQYLT.js @@ -16,7 +16,6 @@ exports.module = function(phantomas) { phantomas.setMetric('jQueryOnDOMReadyFunctions'); // @desc number of functions bound to onDOMReady event phantomas.setMetric('jQuerySizzleCalls'); // @desc number of calls to Sizzle (including those that will be resolved using querySelectorAll) phantomas.setMetric('jQuerySizzleCallsDuplicated'); // @desc number of calls on the same Sizzle request - phantomas.setMetric('jQueryBindOnMultipleElements'); //@desc number of calls to jQuery bind function on 2 or more elements phantomas.setMetric('jQueryDifferentVersions'); //@desc number of different jQuery versions loaded on the page (not counting iframes) var jQueryFunctions = [ @@ -176,7 +175,12 @@ exports.module = function(phantomas) { backtrace: phantomas.getBacktrace() }); - }, phantomas.leaveContext) || phantomas.log('jQuery: can not measure jQuerySizzleCalls (jQuery used on the page is too old)!'); + }, function(result) { + var moreData = { + resultsNumber : (result && result.length) ? result.length : 0 + }; + phantomas.leaveContext(moreData); + }) || phantomas.log('jQuery: can not measure jQuerySizzleCalls (jQuery used on the page is too old)!'); // $().bind - jQuery.bind @@ -196,14 +200,8 @@ exports.module = function(phantomas) { backtrace: phantomas.getBacktrace() }); - }, function(eventTypes, func) { - phantomas.leaveContext(); - - if (this.length > 1) { - phantomas.incrMetric('jQueryBindOnMultipleElements'); - phantomas.addOffender('jQueryBindOnMultipleElements', '%s (%s on %d elements)', this.selector, eventTypes, this.length); - } - + }, function(result) { + phantomas.leaveContext(); }) || phantomas.log('jQuery: can not measure jQueryBindCalls (jQuery used on the page is too old)!'); @@ -283,7 +281,9 @@ exports.module = function(phantomas) { backtrace: phantomas.getBacktrace() }); - }, phantomas.leaveContext) || phantomas.log('jQuery: can not track jQuery - ' + capitalizedName + ' (this version of jQuery doesn\'t support it)'); + }, function(result) { + phantomas.leaveContext(); + }) || phantomas.log('jQuery: can not track jQuery - ' + capitalizedName + ' (this version of jQuery doesn\'t support it)'); });