From 1f34538576a72fc6ddafa533beec1c1c2f37d8d8 Mon Sep 17 00:00:00 2001 From: Anat Dagan Date: Thu, 9 Feb 2017 11:22:27 +0200 Subject: [PATCH] add support-ie7 option when true, catch vars are considered in the global scope. https://github.com/mishoo/UglifyJS2/issues/1039 --- README.md | 3 +++ bin/uglifyjs | 12 +++++++++--- lib/scope.js | 4 +++- test/mocha/support-ie7.js | 19 +++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 test/mocha/support-ie7.js diff --git a/README.md b/README.md index a8b55843..f601d881 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,9 @@ The available options are: for `compress`, `mangle` and `output` options. Note: `--support-ie8` may generate incorrect code for `try`/`catch` in ES5 compliant browsers. + --support-ie7 Use this flag to support Internet Explorer 6/7 + Note: `--support-ie7` may generate incorrect code + for `try`/`catch` in ES5 compliant browsers. --expr Parse a single expression, rather than a program (for parsing JSON) -p, --prefix Skip prefix for original filenames that appear diff --git a/bin/uglifyjs b/bin/uglifyjs index 8cb2f0df..a3b3d32b 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -11,6 +11,7 @@ var path = require("path"); var async = require("async"); var acorn; var screw_ie8 = true; +var support_ie7 = false; var ARGS = yargs .usage("$0 input1.js [input2.js ...] [options]\n\ Use a single dash to read input from the standard input.\ @@ -28,6 +29,7 @@ mangling you need to use `-c` and `-m`.\ .describe("in-source-map", "Input source map, useful if you're compressing JS that was generated from some other original code.") .describe("screw-ie8", "Do not support Internet Explorer 6/7/8. This flag is enabled by default.") .describe("support-ie8", "Support non-standard Internet Explorer 6/7/8 javascript. Note: may generate incorrect code for try/catch in ES5 compliant browsers.") + .describe("support-ie7", "Support non-standard Internet Explorer 6/7 javascript. Note: may generate incorrect code for try/catch in ES5 compliant browsers.") .describe("expr", "Parse a single expression, rather than a program (for parsing JSON)") .describe("p", "Skip prefix for original filenames that appear in source maps. \ For example -p 3 will drop 3 directories from file names and ensure they are relative paths. \ @@ -119,6 +121,7 @@ You need to pass an argument to this option to specify the name that your module .boolean("source-map-include-sources") .boolean("screw-ie8") .boolean("support-ie8") + .boolean("support-ie7") .boolean("export-all") .boolean("self") .boolean("v") @@ -242,7 +245,10 @@ if (ARGS.mangle_props == 2) { if (ARGS.support_ie8 === true && ARGS.screw_ie8 !== true) { screw_ie8 = false; } - +if (ARGS.support_ie7 === true) { + support_ie7 = true; + screw_ie8 = false; +} if (COMPRESS) COMPRESS.screw_ie8 = screw_ie8; if (MANGLE) MANGLE.screw_ie8 = screw_ie8; OUTPUT_OPTIONS.screw_ie8 = screw_ie8; @@ -432,7 +438,7 @@ async.eachLimit(files, 1, function (file, cb) { if (SCOPE_IS_NEEDED) { time_it("scope", function(){ - TOPLEVEL.figure_out_scope(MANGLE || { screw_ie8: screw_ie8, cache: TL_CACHE }); + TOPLEVEL.figure_out_scope(MANGLE || { screw_ie8: screw_ie8, support_ie7: support_ie7, cache: TL_CACHE }); if (ARGS.lint) { TOPLEVEL.scope_warnings(); } @@ -447,7 +453,7 @@ async.eachLimit(files, 1, function (file, cb) { if (SCOPE_IS_NEEDED) { time_it("scope", function(){ - TOPLEVEL.figure_out_scope(MANGLE || { screw_ie8: screw_ie8, cache: TL_CACHE }); + TOPLEVEL.figure_out_scope(MANGLE || { screw_ie8: screw_ie8, support_ie7: support_ie7, cache: TL_CACHE }); if (MANGLE && !TL_CACHE) { TOPLEVEL.compute_char_frequency(MANGLE); } diff --git a/lib/scope.js b/lib/scope.js index 55d1eff1..bf9e5e7a 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -89,6 +89,7 @@ SymbolDef.prototype = { AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ options = defaults(options, { screw_ie8: true, + support_ie7: false, cache: null }); @@ -99,6 +100,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ var defun = null; var last_var_had_const_pragma = false; var nesting = 0; + var global_scope = this; var tw = new TreeWalker(function(node, descend){ if (options.screw_ie8 && node instanceof AST_Catch) { var save_scope = scope; @@ -165,7 +167,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ def.init = tw.parent().value; } else if (node instanceof AST_SymbolCatch) { - (options.screw_ie8 ? scope : defun) + (options.support_ie7 ? global_scope : options.screw_ie8 ? scope : defun) .def_variable(node); } else if (node instanceof AST_LabelRef) { diff --git a/test/mocha/support-ie7.js b/test/mocha/support-ie7.js new file mode 100644 index 00000000..1188fa7c --- /dev/null +++ b/test/mocha/support-ie7.js @@ -0,0 +1,19 @@ +var assert = require("assert"); +var uglify = require("../../"); + +describe("support_ie7", function (){ + //solves issue https://github.com/mishoo/UglifyJS2/issues/1039 + it ("When support_ie7 is true, should treat the catch parameter as a global parameter", function() { + var ast = uglify.parse( "function a(b){\ + try {\ + throw 'Stuff';\ + } catch (e) {\ + console.log('caught: ' + undefined);\ + }\ + console.log('undefined is ' + undefined);\ + return b === undefined;\ + };"); + ast.figure_out_scope({support_ie7: true}); + assert.equal(ast.variables.has("e"), true); + }); +});