From 45b66e564137e4be59bca83384618a39b7907e10 Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Fri, 20 Jan 2017 23:45:14 -0800 Subject: [PATCH 1/3] Improve test for CommonJS environment, to not be fooled by a browser environment that happens to have `require` and `exports` defined as globals --- lib/jison.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jison.js b/lib/jison.js index d078056ee..88596b506 100755 --- a/lib/jison.js +++ b/lib/jison.js @@ -966,7 +966,7 @@ lrGeneratorMixin.generateCommonJSModule = function generateCommonJSModule (opt) opt = typal.mix.call({}, this.options, opt); var moduleName = opt.moduleName || "parser"; var out = this.generateModule(opt) - + "\n\n\nif (typeof require !== 'undefined' && typeof exports !== 'undefined') {" + + "\n\n\nif (typeof require !== 'undefined' && typeof exports !== 'undefined' && new Function('try{return this===global;}catch(e){return false;}')()) {" + "\nexports.parser = "+moduleName+";" + "\nexports.Parser = "+moduleName+".Parser;" + "\nexports.parse = function () { return "+moduleName+".parse.apply("+moduleName+", arguments); };" From de43eb3904e41b796f86a3307143d5c1e7b6c437 Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Sat, 21 Jan 2017 02:17:38 -0800 Subject: [PATCH 2/3] =?UTF-8?q?Actually,=20rather=20than=20test=20for=20No?= =?UTF-8?q?de,=20let=E2=80=99s=20specifically=20check=20that=20require(?= =?UTF-8?q?=E2=80=98fs=E2=80=99)=20returns=20something=20before=20we=20try?= =?UTF-8?q?=20to=20use=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/jison.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/jison.js b/lib/jison.js index 88596b506..762689cfb 100755 --- a/lib/jison.js +++ b/lib/jison.js @@ -966,7 +966,7 @@ lrGeneratorMixin.generateCommonJSModule = function generateCommonJSModule (opt) opt = typal.mix.call({}, this.options, opt); var moduleName = opt.moduleName || "parser"; var out = this.generateModule(opt) - + "\n\n\nif (typeof require !== 'undefined' && typeof exports !== 'undefined' && new Function('try{return this===global;}catch(e){return false;}')()) {" + + "\n\n\nif (typeof require !== 'undefined' && typeof exports !== 'undefined') {" + "\nexports.parser = "+moduleName+";" + "\nexports.Parser = "+moduleName+".Parser;" + "\nexports.parse = function () { return "+moduleName+".parse.apply("+moduleName+", arguments); };" @@ -1280,7 +1280,12 @@ function commonjsMain (args) { console.log('Usage: '+args[0]+' FILE'); process.exit(1); } - var source = require('fs').readFileSync(require('path').normalize(args[1]), "utf8"); + var source = null; + var fs = require('fs'); + var path = require('path'); + if (typeof fs !== 'undefined' && fs !== null && typeof path !== 'undefined' && path !== null) { + source = fs.readFileSync(path.normalize(args[1]), "utf8"); + } return exports.parser.parse(source); } From 91e23075c824cf273f0c727febfd172707161c92 Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Sun, 22 Jan 2017 02:52:27 -0800 Subject: [PATCH 3/3] Set the fallback value of source to an empty string, which is parseable, instead of null, which is not. --- lib/jison.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jison.js b/lib/jison.js index 762689cfb..876b3a2fc 100755 --- a/lib/jison.js +++ b/lib/jison.js @@ -1280,7 +1280,7 @@ function commonjsMain (args) { console.log('Usage: '+args[0]+' FILE'); process.exit(1); } - var source = null; + var source = ''; var fs = require('fs'); var path = require('path'); if (typeof fs !== 'undefined' && fs !== null && typeof path !== 'undefined' && path !== null) {