Skip to content

Commit

Permalink
fix iteration over object with inherited properties (#2068)
Browse files Browse the repository at this point in the history
fixes #2055
  • Loading branch information
alexlamsl committed Jun 7, 2017
1 parent 9db0695 commit 9c30640
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/compress.js
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/minify.js
Expand Up @@ -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") {
Expand Down Expand Up @@ -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]);
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/mocha/minify.js
Expand Up @@ -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};';
Expand Down Expand Up @@ -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});");
});
});
});

0 comments on commit 9c30640

Please sign in to comment.