aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-04-17 08:13:49 +0100
committerGitHub <noreply@github.com>2020-04-17 15:13:49 +0800
commit9110fac9a28d18981e2297f4c84f4c18037ba88e (patch)
treeb71144f6a348e347758ab99649526c434e2879d0
parent83f42ede367316193205572a04959098b45e711d (diff)
downloadtracifyjs-9110fac9a28d18981e2297f4c84f4c18037ba88e.tar.gz
tracifyjs-9110fac9a28d18981e2297f4c84f4c18037ba88e.zip
suppress mutation of input `options` (#3785)
-rw-r--r--lib/utils.js12
-rw-r--r--test/mocha/minify.js26
2 files changed, 20 insertions, 18 deletions
diff --git a/lib/utils.js b/lib/utils.js
index 508aba9c..7932e05f 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -87,15 +87,11 @@ DefaultsError.prototype.name = "DefaultsError";
configure_error_stack(DefaultsError);
function defaults(args, defs, croak) {
- if (args === true) args = {};
- var ret = args || {};
- if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i)) {
- throw new DefaultsError("`" + i + "` is not a supported option", defs);
+ for (var i in args) if (HOP(args, i)) {
+ if (croak && !HOP(defs, i)) throw new DefaultsError("`" + i + "` is not a supported option", defs);
+ defs[i] = args[i];
}
- for (var i in defs) if (HOP(defs, i)) {
- ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
- }
- return ret;
+ return defs;
}
function merge(obj, ext) {
diff --git a/test/mocha/minify.js b/test/mocha/minify.js
index a41289f8..408df686 100644
--- a/test/mocha/minify.js
+++ b/test/mocha/minify.js
@@ -9,18 +9,30 @@ function read(path) {
describe("minify", function() {
it("Should test basic sanity of minify with default options", function() {
- var js = 'function foo(bar) { if (bar) return 3; else return 7; var u = not_called(); }';
+ var js = "function foo(bar) { if (bar) return 3; else return 7; var u = not_called(); }";
var result = UglifyJS.minify(js);
- assert.strictEqual(result.code, 'function foo(n){return n?3:7}');
+ if (result.error) throw result.error;
+ assert.strictEqual(result.code, "function foo(n){return n?3:7}");
});
-
+ it("Should not mutate minify `options`", function() {
+ var options = {
+ compress: true,
+ mangle: false,
+ output: {},
+ };
+ var value = JSON.stringify(options);
+ var result = UglifyJS.minify("print(6 * 7);", options);
+ if (result.error) throw result.error;
+ assert.strictEqual(result.code, "print(42);");
+ assert.strictEqual(JSON.stringify(options), value);
+ })
it("Should skip inherited keys from `files`", function() {
var files = Object.create({ skip: this });
files[0] = "alert(1 + 1)";
var result = UglifyJS.minify(files);
+ if (result.error) throw result.error;
assert.strictEqual(result.code, "alert(2);");
});
-
it("Should work with mangle.cache", function() {
var cache = {};
var original = "";
@@ -53,7 +65,6 @@ describe("minify", function() {
].join(""));
assert.strictEqual(run_code(compressed, true), run_code(original, true));
});
-
it("Should work with nameCache", function() {
var cache = {};
var original = "";
@@ -86,7 +97,6 @@ describe("minify", function() {
].join(""));
assert.strictEqual(run_code(compressed, true), run_code(original, true));
});
-
it("Should avoid cached names when mangling top-level variables", function() {
var cache = {};
var original = "";
@@ -115,7 +125,6 @@ describe("minify", function() {
].join(""));
assert.strictEqual(run_code(compressed, true), run_code(original, true));
});
-
it("Should avoid cached names when mangling inner-scoped variables", function() {
var cache = {};
var original = "";
@@ -139,7 +148,6 @@ describe("minify", function() {
].join(""));
assert.strictEqual(run_code(compressed, true), run_code(original, true));
});
-
it("Should not parse invalid use of reserved words", function() {
assert.strictEqual(UglifyJS.minify("function enum(){}").error, undefined);
assert.strictEqual(UglifyJS.minify("function static(){}").error, undefined);
@@ -155,7 +163,6 @@ describe("minify", function() {
}});
assert.strictEqual(result.code, 'var foo={"x":1,y:2,"z":3};');
});
-
it("Should preserve quote styles when quote_style is 3", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
var result = UglifyJS.minify(js, {
@@ -165,7 +172,6 @@ describe("minify", function() {
}});
assert.strictEqual(result.code, 'var foo={"x":1,y:2,\'z\':3};');
});
-
it("Should not preserve quotes in object literals when disabled", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
var result = UglifyJS.minify(js, {