Skip to content

Commit

Permalink
Fix #167: non-enumerable globals are now also prefixed with var
Browse files Browse the repository at this point in the history
  • Loading branch information
rensbaardman committed Jun 11, 2019
1 parent 5bea3d8 commit e45e986
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
14 changes: 11 additions & 3 deletions lib/getImportGlobalsSrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
function getImportGlobalsSrc(ignore) {
var key,
value,
src = "",
globalObj = typeof global === "undefined"? window: global;

Expand All @@ -20,12 +19,21 @@ function getImportGlobalsSrc(ignore) {
// shadow the module-internal variables
// @see https://github.com/jhnns/rewire-webpack/pull/6
ignore.push("module", "exports", "require");
// strict mode doesn't allow to (re)define 'undefined', 'eval' & 'arguments'
ignore.push("undefined", "eval", "arguments");
// 'GLOBAL' and 'root' are deprecated in Node
// (assigning them causes a DeprecationWarning)
ignore.push("GLOBAL", "root");
// 'NaN' and 'Infinity' are immutable
// (doesn't throw an error if you set 'var NaN = ...', but doesn't work either)
ignore.push("NaN", "Infinity");

for (key in globalObj) { /* jshint forin: false */
const globals = Object.getOwnPropertyNames(globalObj);

for (key of globals) {
if (ignore.indexOf(key) !== -1) {
continue;
}
value = globalObj[key];

// key may be an invalid variable name (e.g. 'a-b')
try {
Expand Down
16 changes: 13 additions & 3 deletions test/getImportGlobalsSrc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var expect = require("expect.js"),
getImportGlobalsSrc = require("../lib/getImportGlobalsSrc.js");

describe("getImportGlobalsSrc", function () {

it("should declare all globals with a var", function () {
var context = {
global: global
Expand All @@ -28,23 +29,31 @@ describe("getImportGlobalsSrc", function () {
delete global['__core-js_shared__'];
delete global['a-b'];

expectedGlobals = Object.keys(global);
const ignoredGlobals = ["module", "exports", "require", "undefined", "eval", "arguments", "GLOBAL", "root", "NaN", "Infinity"]

const globals = Object.getOwnPropertyNames(global);
expectedGlobals = globals.filter((el) => !ignoredGlobals.includes(el))

vm.runInNewContext(src, context);
actualGlobals = Object.keys(context);
actualGlobals = Object.getOwnPropertyNames(context);

actualGlobals.sort();
expectedGlobals.sort();
expect(actualGlobals).to.eql(expectedGlobals);
expect(actualGlobals.length).to.be.above(1);
});

it("should ignore the given variables", function () {
var context = {
global: global
},
ignore = ["console", "setTimeout"],
src,
actualGlobals,
expectedGlobals = Object.keys(global);
expectedGlobals = Object.getOwnPropertyNames(global);

const ignoredGlobals = ["module", "exports", "require", "undefined", "eval", "arguments", "GLOBAL", "root", "NaN", "Infinity"]
ignore = ignore.concat(ignoredGlobals)

// getImportGlobalsSrc modifies the ignore array, so let's create a copy
src = getImportGlobalsSrc(ignore.slice(0));
Expand All @@ -58,4 +67,5 @@ describe("getImportGlobalsSrc", function () {
expect(actualGlobals).to.eql(expectedGlobals);
expect(actualGlobals.length).to.be.above(1);
});

});

0 comments on commit e45e986

Please sign in to comment.