[gjs] coverage: Don't mark empty var declarations executable



commit 023bf89c67fbdd9854e1f17b603451fbce176f23
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Sep 23 11:51:59 2017 -0700

    coverage: Don't mark empty var declarations executable
    
    These never get executed. I'm not sure why they are treated differently
    than empty let declarations, but it's probably to do with scope.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=788166

 installed-tests/js/testCoverage.js |   12 +++++++++++-
 modules/_bootstrap/coverage.js     |   11 +++++++++++
 2 files changed, 22 insertions(+), 1 deletions(-)
---
diff --git a/installed-tests/js/testCoverage.js b/installed-tests/js/testCoverage.js
index df3e032..56d878f 100644
--- a/installed-tests/js/testCoverage.js
+++ b/installed-tests/js/testCoverage.js
@@ -13,7 +13,7 @@ describe('Coverage.expressionLinesForAST', function () {
             "x = (function() {\n" +
             "    return 10;\n" +
             "})();\n",
-            [1, 2, 3],
+            [2, 3],
         ],
 
         'finds lines inside functions': [
@@ -198,6 +198,16 @@ describe('Coverage.expressionLinesForAST', function () {
             }`,
             [2, 3, 4],
         ],
+
+        'does not find lines in empty var declarations': [
+            'var foo;',
+            [],
+        ],
+
+        'finds lines in empty let declarations': [
+            'let foo;',
+            [1],
+        ],
     };
 
     Object.keys(testTable).forEach(testcase => {
diff --git a/modules/_bootstrap/coverage.js b/modules/_bootstrap/coverage.js
index c8c5499..4365899 100644
--- a/modules/_bootstrap/coverage.js
+++ b/modules/_bootstrap/coverage.js
@@ -363,6 +363,17 @@ function expressionLinesForNode(statement) {
                 break;
             expressionLines.push(statement.loc.start.line);
             break;
+        case 'VariableDeclaration':
+            if (statement.kind === 'var') {
+                /* empty 'var foo;' declarations are not executable */
+                let nonEmpty = statement.declarations.filter(decl =>
+                    decl.init !== null);
+                nonEmpty.forEach(decl => {
+                    expressionLines.push(decl.loc.start.line);
+                });
+                break;
+            }
+            /* fall through */
         default:
             expressionLines.push(statement.loc.start.line);
             break;


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