[gjs/wip/ptomato/develop: 4/11] coverage: Walk AST in more cases



commit 87f4346342c548eb19705738fc7057fe1bcf58f1
Author: Philip Chimento <philip chimento gmail com>
Date:   Fri Sep 22 23:08:01 2017 -0700

    coverage: Walk AST in more cases
    
    - The switch expression of a switch-case statement
    - Default parameters to a function
    
    https://bugzilla.gnome.org/show_bug.cgi?id=788166

 installed-tests/js/testCoverage.js |   23 +++++++++++++++++++++++
 modules/_bootstrap/coverage.js     |    7 ++++---
 2 files changed, 27 insertions(+), 3 deletions(-)
---
diff --git a/installed-tests/js/testCoverage.js b/installed-tests/js/testCoverage.js
index f68a1d0..f2b723c 100644
--- a/installed-tests/js/testCoverage.js
+++ b/installed-tests/js/testCoverage.js
@@ -535,6 +535,29 @@ describe('Coverage.functionsForAST', function () {
                 { key: '(anonymous):2:0', line: 2, n_params: 0 },
             ],
         ],
+
+        'finds functions inside switch expression': [
+            'switch (function () {}) {}',
+            [
+                { key: '(anonymous):1:0', line: 1, n_params: 0 },
+            ],
+        ],
+
+        'finds functions inside function default arguments': [
+            'function foo(bar=function () {}) {}',
+            [
+                { key: 'foo:1:1', line: 1, n_params: 1 },
+                { key: '(anonymous):1:0', line: 1, n_params: 0 },
+            ],
+        ],
+
+        'finds functions inside function expression default arguments': [
+            'void function foo(bar=function () {}) {}',
+            [
+                { key: 'foo:1:1', line: 1, n_params: 1 },
+                { key: '(anonymous):1:0', line: 1, n_params: 0 },
+            ],
+        ],
     };
 
     Object.keys(testTable).forEach(testcase => {
diff --git a/modules/_bootstrap/coverage.js b/modules/_bootstrap/coverage.js
index bd1cf14..fea755d 100644
--- a/modules/_bootstrap/coverage.js
+++ b/modules/_bootstrap/coverage.js
@@ -28,8 +28,6 @@ function getSubNodesForNode(node) {
     /* These statements have a single body */
     case 'LabeledStatement':
     case 'WithStatement':
-    case 'FunctionDeclaration':
-    case 'FunctionExpression':
     case 'CatchClause':
     case 'ClassMethod':
         subNodes.push(node.body);
@@ -95,8 +93,10 @@ function getSubNodesForNode(node) {
                 subNodes.push(elem);
         });
         break;
+    case 'FunctionDeclaration':
+    case 'FunctionExpression':
     case 'ArrowFunctionExpression':
-        Array.prototype.push.apply(subNodes, node.defaults);
+        subNodes.push(...node.defaults);
         subNodes.push(node.body);
         break;
     case 'SequenceExpression':
@@ -142,6 +142,7 @@ function getSubNodesForNode(node) {
             subNodes.push(node.finalizer);
         break;
     case 'SwitchStatement':
+        subNodes.push(node.discriminant);
         for (let caseClause of node.cases) {
             caseClause.consequent.forEach(function(expression) {
                 subNodes.push(expression);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]