8000 Merge pull request #36 from claydotio/you-dont-even-want-to-know-how-… · ShMcK/rewire-coderoad@a23473b · GitHub
[go: up one dir, main page]

Skip to content

Commit a23473b

Browse files
committed
Merge pull request jhnns#36 from claydotio/you-dont-even-want-to-know-how-long-this-took-to-find
Ignore invalid variable names during injection of global params
2 parents 79aac24 + 15dc51b commit a23473b

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/getImportGlobalsSrc.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ function getImportGlobalsSrc(ignore) {
2626
continue;
2727
}
2828
value = globalObj[key];
29-
src += "var " + key + " = global." + key + "; ";
29+
30+
// key may be an invalid variable name (e.g. 'a-b')
31+
try {
32+
eval("var " + key + ";");
33+
src += "var " + key + " = global." + key + "; ";
34+
} catch(e) {}
3035
}
3136

3237
return src;
3338
}
3439

35-
module.exports = getImportGlobalsSrc;
40+
module.exports = getImportGlobalsSrc;

test/getImportGlobalsSrc.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ describe("getImportGlobalsSrc", function () {
1717
global.exports = exports;
1818
global.require = require;
1919

20+
// Also make sure it ignores invalid variable names
21+
global['a-b'] = true;
22+
2023
src = getImportGlobalsSrc();
2124

2225
delete global.module;
2326
delete global.exports;
2427
delete global.require;
28+
delete global['a-b'];
2529

2630
expectedGlobals = Object.keys(global);
2731

@@ -63,4 +67,4 @@ describe("getImportGlobalsSrc", function () {
6367
expect(actualGlobals).to.eql(expectedGlobals);
6468
expect(actualGlobals.length).to.be.above(1);
6569
});
66-
});
70+
});

0 commit comments

Comments
 (0)
0