[gjs: 1/4] JS: migrate from the global `window` to `globalThis`



commit b246b6ef06881fd1281d0de4e391a00260ee4495
Author: Andy Holmes <andrew g r holmes gmail com>
Date:   Thu Apr 23 19:04:05 2020 -0700

    JS: migrate from the global `window` to `globalThis`
    
    As of mozjs68 (gjs-1.64) `globalThis` is recommended over `window` and
    it makes more sense in this context anyways. Migrate the few instances
    of `window` we use and adjust the eslint configuration.
    
    `window` will continue to resolve to `globalThis`, so this won't affect
    extensions or other downstream users.

 .eslintrc.yml                         |  2 +-
 installed-tests/js/minijasmine.js     | 24 ++++++++++++------------
 installed-tests/js/testImporter.js    |  4 ++--
 modules/script/_bootstrap/coverage.js |  2 +-
 modules/script/_bootstrap/default.js  |  2 +-
 modules/script/package.js             |  8 ++++----
 6 files changed, 21 insertions(+), 21 deletions(-)
---
diff --git a/.eslintrc.yml b/.eslintrc.yml
index fec07a40..3fa6b86b 100644
--- a/.eslintrc.yml
+++ b/.eslintrc.yml
@@ -218,12 +218,12 @@ globals:
   ARGV: readonly
   Debugger: readonly
   GIRepositoryGType: readonly
+  globalThis: readonly
   imports: readonly
   Intl: readonly
   log: readonly
   logError: readonly
   print: readonly
   printerr: readonly
-  window: readonly
 parserOptions:
   ecmaVersion: 2019
diff --git a/installed-tests/js/minijasmine.js b/installed-tests/js/minijasmine.js
index e99060d9..a4246803 100644
--- a/installed-tests/js/minijasmine.js
+++ b/installed-tests/js/minijasmine.js
@@ -30,20 +30,20 @@ function _clearTimeoutInternal(id) {
 }
 
 // Install the browser setTimeout/setInterval API on the global object
-window.setTimeout = _setTimeoutInternal.bind(undefined, GLib.SOURCE_REMOVE);
-window.setInterval = _setTimeoutInternal.bind(undefined, GLib.SOURCE_CONTINUE);
-window.clearTimeout = window.clearInterval = _clearTimeoutInternal;
+globalThis.setTimeout = _setTimeoutInternal.bind(undefined, GLib.SOURCE_REMOVE);
+globalThis.setInterval = _setTimeoutInternal.bind(undefined, GLib.SOURCE_CONTINUE);
+globalThis.clearTimeout = globalThis.clearInterval = _clearTimeoutInternal;
 
 let jasmineRequire = imports.jasmine.getJasmineRequireObj();
 let jasmineCore = jasmineRequire.core(jasmineRequire);
-window._jasmineEnv = jasmineCore.getEnv();
+globalThis._jasmineEnv = jasmineCore.getEnv();
 
-window._jasmineMain = GLib.MainLoop.new(null, false);
-window._jasmineRetval = 0;
+globalThis._jasmineMain = GLib.MainLoop.new(null, false);
+globalThis._jasmineRetval = 0;
 
 // Install Jasmine API on the global object
-let jasmineInterface = jasmineRequire.interface(jasmineCore, window._jasmineEnv);
-Object.assign(window, jasmineInterface);
+let jasmineInterface = jasmineRequire.interface(jasmineCore, globalThis._jasmineEnv);
+Object.assign(globalThis, jasmineInterface);
 
 // Reporter that outputs according to the Test Anything Protocol
 // See http://testanything.org/tap-specification.html
@@ -65,12 +65,12 @@ class TapReporter {
             });
         });
 
-        window._jasmineMain.quit();
+        globalThis._jasmineMain.quit();
     }
 
     suiteDone(result) {
         if (result.failedExpectations && result.failedExpectations.length > 0) {
-            window._jasmineRetval = 1;
+            globalThis._jasmineRetval = 1;
             this._failedSuites.push(result);
         }
 
@@ -85,7 +85,7 @@ class TapReporter {
     specDone(result) {
         let tapReport;
         if (result.status === 'failed') {
-            window._jasmineRetval = 1;
+            globalThis._jasmineRetval = 1;
             tapReport = 'not ok';
         } else {
             tapReport = 'ok';
@@ -109,7 +109,7 @@ class TapReporter {
     }
 }
 
-window._jasmineEnv.addReporter(new TapReporter());
+globalThis._jasmineEnv.addReporter(new TapReporter());
 
 // If we're running the tests in certain JS_GC_ZEAL modes, then some will time
 // out if the CI machine is under a certain load. In that case increase the
diff --git a/installed-tests/js/testImporter.js b/installed-tests/js/testImporter.js
index 6100a412..31582a7e 100644
--- a/installed-tests/js/testImporter.js
+++ b/installed-tests/js/testImporter.js
@@ -166,7 +166,7 @@ describe('Importer', function () {
         let LexicalScope;
 
         beforeAll(function () {
-            window.expectMe = true;
+            globalThis.expectMe = true;
             LexicalScope = imports.lexicalScope;
         });
 
@@ -195,7 +195,7 @@ describe('Importer', function () {
         });
 
         it('does not leak module properties into the global scope', function () {
-            expect(window.d).not.toBeDefined();
+            expect(globalThis.d).not.toBeDefined();
         });
     });
 
diff --git a/modules/script/_bootstrap/coverage.js b/modules/script/_bootstrap/coverage.js
index 472de844..7cc58a6f 100644
--- a/modules/script/_bootstrap/coverage.js
+++ b/modules/script/_bootstrap/coverage.js
@@ -3,4 +3,4 @@
 
     exports.debugger = new Debugger(exports.debuggee);
     exports.debugger.collectCoverageInfo = true;
-})(window);
+})(globalThis);
diff --git a/modules/script/_bootstrap/default.js b/modules/script/_bootstrap/default.js
index cc5f94bb..86ef0211 100644
--- a/modules/script/_bootstrap/default.js
+++ b/modules/script/_bootstrap/default.js
@@ -4,4 +4,4 @@
     // Do early initialization here.
     void exports;
 
-})(window);
+})(globalThis);
diff --git a/modules/script/package.js b/modules/script/package.js
index e3c3d186..0fbc4188 100644
--- a/modules/script/package.js
+++ b/modules/script/package.js
@@ -117,7 +117,7 @@ function _makeNamePath(n) {
  * @param {object} params package parameters
  */
 function init(params) {
-    window.pkg = imports.package;
+    globalThis.pkg = imports.package;
     _pkgname = params.name;
     name = _findEffectiveEntryPointName();
     version = params.version;
@@ -312,9 +312,9 @@ function initGettext() {
     Gettext.textdomain(_pkgname);
 
     let gettext = imports.gettext;
-    window._ = gettext.gettext;
-    window.C_ = gettext.pgettext;
-    window.N_ = function (x) {
+    globalThis._ = gettext.gettext;
+    globalThis.C_ = gettext.pgettext;
+    globalThis.N_ = function (x) {
         return x;
     };
 }


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