From: Alex Lam S.L Date: Wed, 7 Jun 2017 19:27:03 +0000 (+0800) Subject: fix iteration over object with inherited properties (#2068) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=9c306406f14087ad0a62141bf7bb9e12afcc1a75;p=UglifyJS.git fix iteration over object with inherited properties (#2068) fixes #2055 --- diff --git a/lib/compress.js b/lib/compress.js index 81b9ccac..5f2ac2b0 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1419,7 +1419,7 @@ merge(Compressor.prototype, { }); if (value && typeof value == "object") { var props = []; - for (var key in value) { + for (var key in value) if (HOP(value, key)) { props.push(make_node(AST_ObjectKeyVal, orig, { key: key, value: to_node(value[key], orig) diff --git a/lib/minify.js b/lib/minify.js index 16f5b189..cc638be3 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -86,7 +86,7 @@ function minify(files, options) { } options.parse = options.parse || {}; options.parse.toplevel = null; - for (var name in files) { + for (var name in files) if (HOP(files, name)) { options.parse.filename = name; options.parse.toplevel = parse(files[name], options.parse); if (options.sourceMap && options.sourceMap.content == "inline") { @@ -134,7 +134,7 @@ function minify(files, options) { if (options.sourceMap.includeSources) { if (files instanceof AST_Toplevel) { throw new Error("original source content unavailable"); - } else for (var name in files) { + } else for (var name in files) if (HOP(files, name)) { options.output.source_map.get().setSourceContent(name, files[name]); } } diff --git a/test/mocha/minify.js b/test/mocha/minify.js index 519b725c..638e79f7 100644 --- a/test/mocha/minify.js +++ b/test/mocha/minify.js @@ -13,6 +13,13 @@ describe("minify", function() { assert.strictEqual(result.code, 'function foo(n){return n?3:7}'); }); + it("Should skip inherited keys from `files`", function() { + var files = Object.create({ skip: this }); + files[0] = "alert(1 + 1)"; + var result = Uglify.minify(files); + assert.strictEqual(result.code, "alert(2);"); + }); + describe("keep_quoted_props", function() { it("Should preserve quotes in object literals", function() { var js = 'var foo = {"x": 1, y: 2, \'z\': 3};'; @@ -207,5 +214,17 @@ describe("minify", function() { assert.ok(err instanceof Error); assert.strictEqual(err.stack.split(/\n/)[0], "Error: Can't handle expression: debugger"); }); + it("should skip inherited properties", function() { + var foo = Object.create({ skip: this }); + foo.bar = 42; + var result = Uglify.minify("alert(FOO);", { + compress: { + global_defs: { + FOO: foo + } + } + }); + assert.strictEqual(result.code, "alert({bar:42});"); + }); }); });