Skip to content

Commit

Permalink
strict mode compatibility
Browse files Browse the repository at this point in the history
This fixes qunitjs#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`.
  • Loading branch information
ef4 committed Feb 26, 2021
1 parent d80a986 commit 8f7a2e5
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"rest-spread-spacing": ["error", "never"],
"wrap-iife": ["error", "any"]
},
"globals": {
"globalThis": true
},
"ignorePatterns": [
"__codeorigin/**",
"coverage/**",
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 0 additions & 7 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ module.exports = {
__dirname + "/src/html-reporter/es6-map.js",
"utf8"
).toString().trim();
},

globals: {
global: "(function() { return this; }())"
}
},
plugins: [
Expand All @@ -57,8 +53,5 @@ module.exports = {
babelHelpers: "bundled",
babelrc: true
} )
],
external: [
"global"
]
};
1 change: 1 addition & 0 deletions src/core.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "@ungap/global-this";
import { window, document, setTimeout } from "./globals";

import equiv from "./equiv";
Expand Down
3 changes: 1 addition & 2 deletions src/export.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}
18 changes: 8 additions & 10 deletions src/globals.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
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;

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;
}
Expand Down
6 changes: 2 additions & 4 deletions src/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import global from "global";

import { begin } from "./core";
import { setTimeout, clearTimeout } from "./globals";
import { emit } from "./events";
Expand Down Expand Up @@ -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 ) ) {
Expand Down

0 comments on commit 8f7a2e5

Please sign in to comment.