This commit is contained in:
Dan 2018-02-06 23:22:57 +00:00 committed by GitHub
commit 005842cee8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 10 deletions

View File

@ -120,7 +120,7 @@ function minify(files, options) {
if (typeof files == "string") { if (typeof files == "string") {
files = [ files ]; files = [ files ];
} }
options.parse = options.parse || {}; options.parse = assign({}, options.parse);
options.parse.toplevel = null; options.parse.toplevel = null;
for (var name in files) if (HOP(files, name)) { for (var name in files) if (HOP(files, name)) {
options.parse.filename = name; options.parse.filename = name;
@ -159,6 +159,7 @@ function minify(files, options) {
toplevel = mangle_properties(toplevel, options.mangle.properties); toplevel = mangle_properties(toplevel, options.mangle.properties);
} }
if (timings) timings.output = Date.now(); if (timings) timings.output = Date.now();
options.output = assign({}, options.output);
var result = {}; var result = {};
if (options.output.ast) { if (options.output.ast) {
result.ast = toplevel; result.ast = toplevel;

View File

@ -405,7 +405,7 @@ AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options) {
reserved : [], reserved : [],
toplevel : false, toplevel : false,
}); });
if (!Array.isArray(options.reserved)) options.reserved = []; options.reserved = Array.isArray(options.reserved) ? options.reserved.slice() : [];
// Never mangle arguments // Never mangle arguments
push_uniq(options.reserved, "arguments"); push_uniq(options.reserved, "arguments");
return options; return options;

View File

@ -95,15 +95,14 @@ DefaultsError.croak = function(msg, defs) {
}; };
function defaults(args, defs, croak) { function defaults(args, defs, croak) {
if (args === true) if (args === true || !args) return defs;
args = {};
var ret = args || {}; for (var i in args) if (HOP(args, i)) {
if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i)) if (croak && !HOP(defs, i))
DefaultsError.croak("`" + i + "` is not a supported option", defs); DefaultsError.croak("`" + i + "` is not a supported option", defs);
for (var i in defs) if (HOP(defs, i)) { defs[i] = args[i];
ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
} }
return ret; return defs;
}; };
function merge(obj, ext) { function merge(obj, ext) {
@ -115,6 +114,11 @@ function merge(obj, ext) {
return count; return count;
}; };
function assign(obj, ext) {
merge(obj, ext);
return obj;
};
function noop() {} function noop() {}
function return_false() { return false; } function return_false() { return false; }
function return_true() { return true; } function return_true() { return true; }

View File

@ -122,6 +122,32 @@ describe("minify", function() {
assert.strictEqual(Uglify.minify("function this(){}").error.message, "Unexpected token: name (this)"); assert.strictEqual(Uglify.minify("function this(){}").error.message, "Unexpected token: name (this)");
}); });
it("Should not mutate options", function() {
[
{},
{
parse: {},
compress: {
global_defs: {},
},
mangle: {
reserved: [],
// expect `cache` to be mutated
},
output: {},
rename: {
reserved: [],
},
sourceMap: {},
// expect `nameCache` to be mutated
},
].forEach(function(options) {
var optsString = JSON.stringify(options);
Uglify.minify("", options);
assert.strictEqual(JSON.stringify(options), optsString);
});
});
describe("keep_quoted_props", function() { describe("keep_quoted_props", function() {
it("Should preserve quotes in object literals", function() { it("Should preserve quotes in object literals", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};'; var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';