From 8f7a2e58b52846d685249590f1301e32cbd1ebb5 Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Thu, 25 Feb 2021 22:54:23 -0500 Subject: [PATCH] strict mode compatibility This fixes #1557 by adopting [globalThis](https://github.com/tc39/proposal-global), and polyfilling it for environments that don't implement it yet via `@ungap/global-this`. --- .eslintrc.json | 3 +++ package-lock.json | 6 ++++++ package.json | 1 + rollup.config.js | 7 ------- src/core.js | 1 + src/export.js | 3 +-- src/globals.js | 18 ++++++++---------- src/test.js | 6 ++---- 8 files changed, 22 insertions(+), 23 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 9423106dc..5e80aa5e0 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -32,6 +32,9 @@ "rest-spread-spacing": ["error", "never"], "wrap-iife": ["error", "any"] }, + "globals": { + "globalThis": true + }, "ignorePatterns": [ "__codeorigin/**", "coverage/**", diff --git a/package-lock.json b/package-lock.json index 3c71b5b0c..aad31ed70 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1477,6 +1477,12 @@ "@types/node": "*" } }, + "@ungap/global-this": { + "version": "0.4.4", + "resolved": "https://registry.npmjs.org/@ungap/global-this/-/global-this-0.4.4.tgz", + "integrity": "sha512-mHkm6FvepJECMNthFuIgpAEFmPOk71UyXuIxYfjytvFTnSDBIz7jmViO+LfHI/AjrazWije0PnSP3+/NlwzqtA==", + "dev": true + }, "abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", diff --git a/package.json b/package.json index 1b01d9613..344089080 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "@babel/core": "^7.11.1", "@babel/plugin-external-helpers": "^7.10.4", "@babel/preset-env": "^7.11.0", + "@ungap/global-this": "^0.4.4", "coveralls": "^3.1.0", "eslint-config-jquery": "^3.0.0", "eslint-plugin-html": "^6.1.1", diff --git a/rollup.config.js b/rollup.config.js index 1d0f7a364..65b4bdabf 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -40,10 +40,6 @@ module.exports = { __dirname + "/src/html-reporter/es6-map.js", "utf8" ).toString().trim(); - }, - - globals: { - global: "(function() { return this; }())" } }, plugins: [ @@ -57,8 +53,5 @@ module.exports = { babelHelpers: "bundled", babelrc: true } ) - ], - external: [ - "global" ] }; diff --git a/src/core.js b/src/core.js index dd5d98375..a72b310e1 100644 --- a/src/core.js +++ b/src/core.js @@ -1,3 +1,4 @@ +import "@ungap/global-this"; import { window, document, setTimeout } from "./globals"; import equiv from "./equiv"; diff --git a/src/export.js b/src/export.js index fb90b37da..42db88b37 100644 --- a/src/export.js +++ b/src/export.js @@ -1,6 +1,5 @@ /* global module, exports, define */ import { window, document, self } from "./globals"; -import global from "global"; export default function exportQUnit( QUnit ) { let exportedModule = false; @@ -54,6 +53,6 @@ export default function exportQUnit( QUnit ) { // For other environments, such as SpiderMonkey (mozjs) and other // embedded JavaScript engines if ( !exportedModule ) { - global.QUnit = QUnit; + globalThis.QUnit = QUnit; } } diff --git a/src/globals.js b/src/globals.js index 1544028ad..cca9379b3 100644 --- a/src/globals.js +++ b/src/globals.js @@ -1,10 +1,8 @@ -import global from "global"; - -export const window = global.window; -export const self = global.self; -export const console = global.console; -export const setTimeout = global.setTimeout; -export const clearTimeout = global.clearTimeout; +export const window = globalThis.window; +export const self = globalThis.self; +export const console = globalThis.console; +export const setTimeout = globalThis.setTimeout; +export const clearTimeout = globalThis.clearTimeout; export const document = window && window.document; export const navigator = window && window.navigator; @@ -12,9 +10,9 @@ export const navigator = window && window.navigator; export const localSessionStorage = ( function() { const x = "qunit-test-string"; try { - global.sessionStorage.setItem( x, x ); - global.sessionStorage.removeItem( x ); - return global.sessionStorage; + globalThis.sessionStorage.setItem( x, x ); + globalThis.sessionStorage.removeItem( x ); + return globalThis.sessionStorage; } catch ( e ) { return undefined; } diff --git a/src/test.js b/src/test.js index 3bd4abc04..8134fc68f 100644 --- a/src/test.js +++ b/src/test.js @@ -1,5 +1,3 @@ -import global from "global"; - import { begin } from "./core"; import { setTimeout, clearTimeout } from "./globals"; import { emit } from "./events"; @@ -648,8 +646,8 @@ function saveGlobal() { config.pollution = []; if ( config.noglobals ) { - for ( const key in global ) { - if ( hasOwn.call( global, key ) ) { + for ( const key in globalThis ) { + if ( hasOwn.call( globalThis, key ) ) { // In Opera sometimes DOM element ids show up here, ignore them if ( /^qunit-test-output/.test( key ) ) {