[gjs/gnome-3-26] coverage: Don't mark empty var declarations executable



commit d7d9c7cbebf5225b944d6122e6eef54a39dd96c8
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/coverage.js                | 11 +++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
---
diff --git a/installed-tests/js/testCoverage.js b/installed-tests/js/testCoverage.js
index 338c69e..8fa4e9f 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/coverage.js b/modules/coverage.js
index c8c5499..4365899 100644
--- a/modules/coverage.js
+++ b/modules/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]