Skip to content

Commit 7bec7f8

Browse files
committed
Fix #167: non-enumerable globals are now also prefixed with var
1 parent 5bea3d8 commit 7bec7f8

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

lib/getImportGlobalsSrc.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
function getImportGlobalsSrc(ignore) {
1111
var key,
12-
value,
1312
src = "",
1413
globalObj = typeof global === "undefined"? window: global;
1514

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

24-
for (key in globalObj) { /* jshint forin: false */
31+
const globals = Object.getOwnPropertyNames(globalObj);
32+
33+
for (key of globals) {
2534
if (ignore.indexOf(key) !== -1) {
2635
continue;
2736
}
28-
value = globalObj[key];
2937

3038
// key may be an invalid variable name (e.g. 'a-b')
3139
try {

test/getImportGlobalsSrc.test.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var expect = require("expect.js"),
33
getImportGlobalsSrc = require("../lib/getImportGlobalsSrc.js");
44

55
describe("getImportGlobalsSrc", function () {
6+
67
it("should declare all globals with a var", function () {
78
var context = {
89
global: global
@@ -28,34 +29,43 @@ describe("getImportGlobalsSrc", function () {
2829
delete global['__core-js_shared__'];
2930
delete global['a-b'];
3031

31-
expectedGlobals = Object.keys(global);
32+
const ignoredGlobals = ["module", "exports", "require", "undefined", "eval", "arguments", "GLOBAL", "root", "NaN", "Infinity"];
33+
34+
const globals = Object.getOwnPropertyNames(global);
35+
expectedGlobals = globals.filter((el) => !ignoredGlobals.includes(el));
3236

3337
vm.runInNewContext(src, context);
34-
actualGlobals = Object.keys(context);
38+
actualGlobals = Object.getOwnPropertyNames(context);
39+
3540
actualGlobals.sort();
3641
expectedGlobals.sort();
3742
expect(actualGlobals).to.eql(expectedGlobals);
3843
expect(actualGlobals.length).to.be.above(1);
3944
});
45+
4046
it("should ignore the given variables", function () {
4147
var context = {
4248
global: global
4349
},
4450
ignore = ["console", "setTimeout"],
4551
src,
4652
actualGlobals,
47-
expectedGlobals = Object.keys(global);
53+
expectedGlobals = Object.getOwnPropertyNames(global);
54+
55+
const ignoredGlobals = ["module", "exports", "require", "undefined", "eval", "arguments", "GLOBAL", "root", "NaN", "Infinity"];
56+
ignore = ignore.concat(ignoredGlobals);
4857

4958
// getImportGlobalsSrc modifies the ignore array, so let's create a copy
5059
src = getImportGlobalsSrc(ignore.slice(0));
51-
expectedGlobals = expectedGlobals.filter(function filterIgnoredVars(value) {
52-
return ignore.indexOf(value) === -1;
53-
});
60+
expectedGlobals = expectedGlobals.filter((el) => !ignore.includes(el));
61+
5462
vm.runInNewContext(src, context);
5563
actualGlobals = Object.keys(context);
64+
5665
actualGlobals.sort();
5766
expectedGlobals.sort();
5867
expect(actualGlobals).to.eql(expectedGlobals);
5968
expect(actualGlobals.length).to.be.above(1);
6069
});
70+
6171
});

0 commit comments

Comments
 (0)