support default values (#4442)

This commit is contained in:
Alex Lam S.L
2020-12-23 22:22:55 +00:00
committed by GitHub
parent 56fce2131c
commit 2390fae5c4
10 changed files with 1430 additions and 208 deletions

View File

@@ -0,0 +1,952 @@
arrow_1: {
input: {
console.log(((a = "PASS") => a)());
}
expect_exact: 'console.log(((a="PASS")=>a)());'
expect_stdout: "PASS"
node_version: ">=6"
}
arrow_2: {
input: {
console.log((([ a = "FAIL" ]) => a)([ "PASS" ]));
}
expect_exact: 'console.log((([a="FAIL"])=>a)(["PASS"]));'
expect_stdout: "PASS"
node_version: ">=6"
}
arrow_3: {
input: {
(([ a = console ] = null) => a.log("PASS"))("");
}
expect_exact: '(([a=console]=null)=>a.log("PASS"))("");'
expect_stdout: "PASS"
node_version: ">=6"
}
assign: {
input: {
[ a = "PASS" ] = [];
console.log(a);
}
expect_exact: '[a="PASS"]=[];console.log(a);'
expect_stdout: "PASS"
node_version: ">=6"
}
declaration_var: {
input: {
var [ a = "PASS" ] = [ , ];
console.log(a);
}
expect_exact: 'var[a="PASS"]=[,];console.log(a);'
expect_stdout: "PASS"
node_version: ">=6"
}
declaration_const: {
input: {
const [ a = "FAIL" ] = [ "PASS" ];
console.log(a);
}
expect_exact: 'const[a="FAIL"]=["PASS"];console.log(a);'
expect_stdout: "PASS"
node_version: ">=6"
}
declaration_let: {
input: {
let [ a = "PASS" ] = [ void 42 ];
console.log(a);
}
expect_exact: 'let[a="PASS"]=[void 42];console.log(a);'
expect_stdout: "PASS"
node_version: ">=6"
}
object_shorthand_assign: {
input: {
({ a = "PASS" } = 42);
console.log(a);
}
expect_exact: '({a:a="PASS"}=42);console.log(a);'
expect_stdout: "PASS"
node_version: ">=6"
}
object_shorthand_declaration: {
input: {
var { a = "PASS" } = 42;
console.log(a);
}
expect_exact: 'var{a:a="PASS"}=42;console.log(a);'
expect_stdout: "PASS"
node_version: ">=6"
}
object_shorthand_function: {
input: {
(function({ a = "PASS" }) {
console.log(a);
})(42);
}
expect_exact: '(function({a:a="PASS"}){console.log(a)})(42);'
expect_stdout: "PASS"
node_version: ">=6"
}
retain_arguments_1: {
options = {
arguments: true,
}
input: {
console.log(function(a = "FAIL") {
return arguments[0];
}() || "PASS");
}
expect: {
console.log(function(a = "FAIL") {
return arguments[0];
}() || "PASS");
}
expect_stdout: "PASS"
node_version: ">=6"
}
retain_arguments_2: {
options = {
arguments: true,
}
input: {
console.log(function(a, b = null) {
a = "FAIL";
return arguments[0];
}("PASS", 42));
}
expect: {
console.log(function(a, b = null) {
a = "FAIL";
return arguments[0];
}("PASS", 42));
}
expect_stdout: "PASS"
node_version: ">=6"
}
process_boolean_returns: {
options = {
booleans: true,
evaluate: true,
reduce_vars: true,
}
input: {
console.log(function(a = console.log("FAIL 1")) {
return a() ? "PASS" : "FAIL 2";
}(function() {
return 42;
}));
}
expect: {
console.log(function(a = console.log("FAIL 1")) {
return a() ? "PASS" : "FAIL 2";
}(function() {
return 1;
}));
}
expect_stdout: "PASS"
node_version: ">=6"
}
collapse_value_1: {
options = {
collapse_vars: true,
unused: true,
}
input: {
console.log(function(a = "PASS") {
return a;
}());
}
expect: {
console.log(function(a) {
return "PASS";
}());
}
expect_stdout: "PASS"
node_version: ">=6"
}
collapse_value_2: {
options = {
collapse_vars: true,
unused: true,
}
input: {
(function(a = console) {
return a;
})().log("PASS");
}
expect: {
(function(a) {
return console;
})().log("PASS");
}
expect_stdout: "PASS"
node_version: ">=6"
}
flatten_if: {
options = {
conditionals: true,
}
input: {
if (console.log("PASS")) {
var [
a = function b() {
for (c in b);
},
] = 0;
}
}
expect: {
var a;
console.log("PASS") && ([
a = function b() {
for (c in b);
},
] = 0);
}
expect_stdout: "PASS"
node_version: ">=6"
}
maintain_if: {
options = {
conditionals: true,
}
input: {
if (a)
for (;;);
else
var [ a = "PASS" ] = [];
console.log(a);
}
expect: {
if (a)
for (;;);
else
var [ a = "PASS" ] = [];
console.log(a);
}
expect_stdout: "PASS"
node_version: ">=6"
}
reduce_value: {
options = {
evaluate: true,
reduce_vars: true,
unused: true,
}
input: {
console.log(function(a = "PASS") {
return a;
}());
}
expect: {
console.log("PASS");
}
expect_stdout: "PASS"
node_version: ">=6"
}
evaluate_iife: {
options = {
evaluate: true,
}
input: {
console.log(function(a = "PASS") {
return a;
}());
}
expect: {
console.log("PASS");
}
expect_stdout: "PASS"
node_version: ">=6"
}
unsafe_evaluate_iife_1: {
options = {
evaluate: true,
unsafe: true,
}
input: {
console.log(function([ a ] = []) {
return "PASS";
}());
}
expect: {
console.log(function([ a ] = []) {
return "PASS";
}());
}
expect_stdout: "PASS"
node_version: ">=6"
}
unsafe_evaluate_iife_2: {
options = {
evaluate: true,
reduce_vars: true,
unsafe: true,
}
input: {
console.log(function([ a ] = []) {
return a[0];
}([ [ "PASS" ] ]));
}
expect: {
console.log(function([ a ] = []) {
return a[0];
}([ [ "PASS" ] ]));
}
expect_stdout: "PASS"
node_version: ">=6"
}
inline_direct: {
options = {
default_values: true,
inline: true,
unused: true,
}
input: {
console.log(function(a = "FAIL") {
return a;
}("PASS"));
}
expect: {
console.log("PASS");
}
expect_stdout: "PASS"
node_version: ">=6"
}
inline_constant: {
options = {
inline: true,
}
input: {
console.log(function(a = console.log("foo")) {
return "bar";
}(void console.log("baz")));
}
expect: {
console.log((void console.log("baz"), console.log("foo"), "bar"));
}
expect_stdout: [
"baz",
"foo",
"bar",
]
node_version: ">=6"
}
inline_function: {
options = {
default_values: true,
inline: true,
sequences: true,
side_effects: true,
toplevel: true,
unused: true,
}
input: {
(function(a = console.log("foo"), b = console.log("bar")) {
console.log("baz");
}(void console.log("moo"), 42));
}
expect: {
console.log("moo"),
console.log("foo"),
console.log("baz");
}
expect_stdout: [
"moo",
"foo",
"baz",
]
node_version: ">=6"
}
inline_loop_1: {
options = {
inline: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
while (function f(a = "PASS") {
console.log(a);
}());
}
expect: {
while (a = "PASS", void console.log(a));
var a;
}
expect_stdout: "PASS"
node_version: ">=6"
}
inline_loop_2: {
options = {
inline: true,
toplevel: true,
}
input: {
while (function(a = [ "PASS" ]) {
var a = function f(b) {
console.log(a[b]);
}(0);
}());
}
expect: {
while (a = [ "PASS" ], a = function f(b) {
console.log(a[b]);
}(0), void 0) ;
var a;
}
expect_stdout: "PASS"
node_version: ">=6"
}
drop_empty_iife: {
options = {
side_effects: true,
}
input: {
console.log(function(a = console.log("foo")) {}(void console.log("baz")));
}
expect: {
console.log((console.log("baz"), void console.log("foo")));
}
expect_stdout: [
"baz",
"foo",
"undefined",
]
node_version: ">=6"
}
retain_empty_iife: {
options = {
side_effects: true,
}
input: {
var a;
try {
(function(a = a) {})();
} catch (e) {
console.log("PASS");
}
}
expect: {
var a;
try {
(function(a = a) {})();
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
node_version: ">=6"
}
retain_fargs: {
options = {
unused: true,
}
input: {
(function([ a = console.log("PASS") ]) {})([]);
}
expect: {
(function([ a = console.log("PASS") ]) {})([]);
}
expect_stdout: "PASS"
node_version: ">=6"
}
drop_fargs: {
options = {
keep_fargs: "strict",
unused: true,
}
input: {
console.log(function(a = 42, b = console.log("foo"), c = true) {
return "bar";
}(console.log("baz"), "moo", false));
}
expect: {
console.log(function(b = console.log("foo")) {
return "bar";
}((console.log("baz"), "moo")));
}
expect_stdout: [
"baz",
"bar",
]
expect_warnings: [
"WARN: Dropping unused function argument c [test/compress/default-values.js:1,61]",
"WARN: Side effects in default value of unused variable b [test/compress/default-values.js:1,37]",
]
node_version: ">=6"
}
unused_var_1: {
options = {
toplevel: true,
unused: true,
}
input: {
var [ a = 42 ] = [ console.log("PASS") ];
}
expect: {
console.log("PASS");
}
expect_stdout: "PASS"
node_version: ">=6"
}
unused_var_2: {
options = {
toplevel: true,
unused: true,
}
input: {
var {
p: [ a ] = "" + console.log("FAIL"),
} = {
p: [ console.log("PASS") ],
};
}
expect: {
var {
p: [] = [ console.log("FAIL") ],
} = {
p: [ console.log("PASS") ],
};
}
expect_stdout: "PASS"
node_version: ">=6"
}
mangle_var_1: {
mangle = {
toplevel: false,
}
input: {
var N = 1, [ {
pname: p = "x",
i: n = N,
}, {
[p + n]: v,
} ] = [ {}, {
x1: "PASS",
} ];
console.log(v);
}
expect: {
var N = 1, [ {
pname: p = "x",
i: n = N,
}, {
[p + n]: v,
} ] = [ {}, {
x1: "PASS",
} ];
console.log(v);
}
expect_stdout: "PASS"
node_version: ">=6"
}
mangle_var_1_toplevel: {
mangle = {
toplevel: true,
}
input: {
var N = 1, [ {
pname: p = "x",
i: n = N,
}, {
[p + n]: v,
} ] = [ {}, {
x1: "PASS",
} ];
console.log(v);
}
expect: {
var o = 1, [ {
pname: a = "x",
i: e = o,
}, {
[a + e]: l,
} ] = [ {}, {
x1: "PASS",
} ];
console.log(l);
}
expect_stdout: "PASS"
node_version: ">=6"
}
mangle_var_2: {
mangle = {
toplevel: false,
}
input: {
var N = 1, [ {
pname: p = "x",
i: n = N,
} = {}, {
[p + n]: v,
} ] = [ , {
x1: "PASS",
} ];
console.log(v);
}
expect: {
var N = 1, [ {
pname: p = "x",
i: n = N,
} = {}, {
[p + n]: v,
} ] = [ , {
x1: "PASS",
} ];
console.log(v);
}
expect_stdout: "PASS"
node_version: ">=6"
}
mangle_var_2_toplevel: {
mangle = {
toplevel: true,
}
input: {
var N = 1, [ {
pname: p = "x",
i: n = N,
} = {}, {
[p + n]: v,
} ] = [ , {
x1: "PASS",
} ];
console.log(v);
}
expect: {
var o = 1, [ {
pname: a = "x",
i: e = o,
} = {}, {
[a + e]: l,
} ] = [ , {
x1: "PASS",
} ];
console.log(l);
}
expect_stdout: "PASS"
node_version: ">=6"
}
mangle_function_1: {
mangle = {
toplevel: false,
}
input: {
var N = 1;
(function(o, {
pname: p,
} = o, {
[p + N]: v,
} = o) {
let N;
console.log(v);
})({
pname: "x",
x1: "PASS",
});
}
expect: {
var N = 1;
(function(n, {
pname: e,
} = n, {
[e + N]: o,
} = n) {
let a;
console.log(o);
})({
pname: "x",
x1: "PASS",
});
}
expect_stdout: "PASS"
node_version: ">=6"
}
mangle_function_1_toplevel: {
mangle = {
toplevel: true,
}
input: {
var N = 1;
(function(o, {
pname: p,
} = o, {
[p + N]: v,
} = o) {
let N;
console.log(v);
})({
pname: "x",
x1: "PASS",
});
}
expect: {
var l = 1;
(function(n, {
pname: e,
} = n, {
[e + l]: o,
} = n) {
let a;
console.log(o);
})({
pname: "x",
x1: "PASS",
});
}
expect_stdout: "PASS"
node_version: ">=6"
}
mangle_function_2: {
mangle = {
toplevel: false,
}
input: {
var N = 1;
(function({
pname: p = "x",
i: n = N,
}, {
[p + n]: v,
}) {
let N;
console.log(v);
})({}, {
x1: "PASS",
});
}
expect: {
var N = 1;
(function({
pname: n = "x",
i: o = N,
}, {
[n + o]: e,
}) {
let l;
console.log(e);
})({}, {
x1: "PASS",
});
}
expect_stdout: "PASS"
node_version: ">=6"
}
mangle_function_2_toplevel: {
mangle = {
toplevel: true,
}
input: {
var N = 1;
(function({
pname: p = "x",
i: n = N,
}, {
[p + n]: v,
}) {
let N;
console.log(v);
})({}, {
x1: "PASS",
});
}
expect: {
var a = 1;
(function({
pname: n = "x",
i: o = a,
}, {
[n + o]: e,
}) {
let l;
console.log(e);
})({}, {
x1: "PASS",
});
}
expect_stdout: "PASS"
node_version: ">=6"
}
mangle_arrow_1: {
mangle = {
toplevel: false,
}
input: {
var N = 1;
((o, {
pname: p,
} = o, {
[p + N]: v,
} = o) => {
let N;
console.log(v);
})({
pname: "x",
x1: "PASS",
});
}
expect: {
var N = 1;
((e, {
pname: a,
} = e, {
[a + N]: l,
} = e) => {
let n;
console.log(l);
})({
pname: "x",
x1: "PASS",
});
}
expect_stdout: "PASS"
node_version: ">=6"
}
mangle_arrow_1_toplevel: {
mangle = {
toplevel: true,
}
input: {
var N = 1;
((o, {
pname: p,
} = o, {
[p + N]: v,
} = o) => {
let N;
console.log(v);
})({
pname: "x",
x1: "PASS",
});
}
expect: {
var o = 1;
((e, {
pname: a,
} = e, {
[a + o]: l,
} = e) => {
let n;
console.log(l);
})({
pname: "x",
x1: "PASS",
});
}
expect_stdout: "PASS"
node_version: ">=6"
}
mangle_arrow_2: {
mangle = {
toplevel: false,
}
input: {
var N = 1;
(({
pname: p = "x",
i: n = N,
}, {
[p + n]: v,
}) => {
let N;
console.log(v);
})({}, {
x1: "PASS",
});
}
expect: {
var N = 1;
(({
pname: e = "x",
i: l = N,
}, {
[e + l]: o,
}) => {
let a;
console.log(o);
})({}, {
x1: "PASS",
});
}
expect_stdout: "PASS"
node_version: ">=6"
}
mangle_arrow_2_toplevel: {
mangle = {
toplevel: true,
}
input: {
var N = 1;
(({
pname: p = "x",
i: n = N,
}, {
[p + n]: v,
}) => {
let N;
console.log(v);
})({}, {
x1: "PASS",
});
}
expect: {
var n = 1;
(({
pname: e = "x",
i: l = n,
}, {
[e + l]: o,
}) => {
let a;
console.log(o);
})({}, {
x1: "PASS",
});
}
expect_stdout: "PASS"
node_version: ">=6"
}

View File

@@ -691,6 +691,28 @@ funarg_inline: {
node_version: ">=6"
}
process_boolean_returns: {
options = {
booleans: true,
}
input: {
console.log(function({ length }) {
return length ? "FAIL" : "PASS";
}(function() {
return 42;
}));
}
expect: {
console.log(function({ length }) {
return length ? "FAIL" : "PASS";
}(function() {
return 42;
}));
}
expect_stdout: "PASS"
node_version: ">=6"
}
simple_const: {
options = {
evaluate: true,

View File

@@ -3,7 +3,7 @@ var UglifyJS = require("../node");
describe("Getters and setters", function() {
it("Should not accept operator symbols as getter/setter name", function() {
var illegalOperators = [
[
"++",
"--",
"+",
@@ -42,43 +42,26 @@ describe("Getters and setters", function() {
"&=",
"&&",
"||"
];
var generator = function() {
var results = [];
for (var i in illegalOperators) {
results.push({
code: "var obj = { get " + illegalOperators[i] + "() { return test; }};",
operator: illegalOperators[i],
method: "get"
});
results.push({
code: "var obj = { set " + illegalOperators[i] + "(value) { test = value}};",
operator: illegalOperators[i],
method: "set"
});
}
return results;
};
var testCase = function(data) {
return function() {
UglifyJS.parse(data.code);
};
};
var fail = function(data) {
return function(e) {
].reduce(function(tests, illegalOperator) {
tests.push({
code: "var obj = { get " + illegalOperator + "() { return test; }};",
operator: illegalOperator,
});
tests.push({
code: "var obj = { set " + illegalOperator + "(value) { test = value; }};",
operator: illegalOperator,
});
return tests;
}, []).forEach(function(test) {
assert.throws(function() {
UglifyJS.parse(test.code);
}, test.operator == "=" ? function(e) {
return e instanceof UglifyJS.JS_Parse_Error
&& e.message === "Unexpected token: operator «" + data.operator + "»";
};
};
var errorMessage = function(data) {
return "Expected but didn't get a syntax error while parsing following line:\n" + data.code;
};
var tests = generator();
for (var i = 0; i < tests.length; i++) {
var test = tests[i];
assert.throws(testCase(test), fail(test), errorMessage(test));
}
&& /^Unexpected token: punc «{», expected: punc «.*?»$/.test(e.message);
} : function(e) {
return e instanceof UglifyJS.JS_Parse_Error
&& e.message === "Unexpected token: operator «" + test.operator + "»";
}, "Expected but didn't get a syntax error while parsing following line:\n" + test.code);
});
});
});

View File

@@ -211,6 +211,11 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
node.alternative,
][ ((node.start._permute += step) * steps | 0) % 3 ];
}
else if (node instanceof U.AST_DefaultValue) {
node.start._permute++;
CHANGED = true;
return node.name;
}
else if (node instanceof U.AST_Defun) {
switch (((node.start._permute += step) * steps | 0) % 2) {
case 0:

View File

@@ -137,6 +137,7 @@ var SUPPORT = function(matrix) {
catch_omit_var: "try {} catch {}",
computed_key: "({[0]: 0});",
const_block: "var a; { const a = 0; }",
default_value: "[ a = 0 ] = [];",
destructuring: "[] = [];",
let: "let a;",
spread: "[...[]];",
@@ -425,18 +426,35 @@ function createArgs(recurmax, stmtDepth, canThrow) {
function createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, was_async) {
var avoid = [];
var len = unique_vars.length;
var pairs = createPairs(recurmax);
var pairs = createPairs(recurmax, !nameLenBefore);
unique_vars.length = len;
return pairs;
function createAssignmentValue(recurmax) {
function fill(nameFn, valueFn) {
var save_async = async;
if (was_async != null) async = was_async;
if (was_async != null) {
async = false;
if (save_async || was_async) addAvoidVar("await");
}
avoid.forEach(addAvoidVar);
var save_vars = nameLenBefore && VAR_NAMES.splice(nameLenBefore);
var value = nameLenBefore && rng(2) ? createValue() : createExpression(recurmax, NO_COMMA, stmtDepth, canThrow);
if (nameFn) nameFn();
if (was_async != null) {
async = was_async;
if (save_async || was_async) removeAvoidVar("await");
}
if (valueFn) valueFn();
if (save_vars) [].push.apply(VAR_NAMES, save_vars);
avoid.forEach(removeAvoidVar);
async = save_async;
return value;
}
function createAssignmentValue(recurmax) {
return nameLenBefore && rng(2) ? createValue() : createExpression(recurmax, NO_COMMA, stmtDepth, canThrow);
}
function createDefaultValue(recurmax, noDefault) {
return !noDefault && SUPPORT.default_value && rng(20) == 0 ? " = " + createAssignmentValue(recurmax) : "";
}
function createKey(recurmax, keys) {
@@ -459,20 +477,22 @@ function createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, was
return name;
}
function createPairs(recurmax) {
function createPairs(recurmax, noDefault) {
var names = [], values = [];
var m = rng(4), n = rng(4);
if (!nameLenBefore) m = Math.max(m, n, 1);
for (var i = Math.max(m, n); --i >= 0;) {
if (i < m && i < n) {
createDestructured(recurmax, names, values);
continue;
}
if (i < m) {
names.unshift(createName());
}
if (i < n) {
values.unshift(createAssignmentValue(recurmax));
createDestructured(recurmax, noDefault, names, values);
} else if (i < m) {
var name = createName();
fill(function() {
names.unshift(name + createDefaultValue(recurmax, noDefault));
});
} else {
fill(null, function() {
values.unshift(createAssignmentValue(recurmax));
});
}
}
return {
@@ -481,7 +501,7 @@ function createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, was
};
}
function createDestructured(recurmax, names, values) {
function createDestructured(recurmax, noDefault, names, values) {
switch (rng(20)) {
case 0:
if (--recurmax < 0) {
@@ -489,20 +509,25 @@ function createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, was
values.unshift('""');
} else {
var pairs = createPairs(recurmax);
while (!rng(10)) {
var index = rng(pairs.names.length + 1);
pairs.names.splice(index, 0, "");
if (index < pairs.values.length) {
pairs.values.splice(index, 0, rng(2) ? createAssignmentValue(recurmax) : "");
} else switch (rng(5)) {
case 0:
pairs.values[index] = createAssignmentValue(recurmax);
case 1:
pairs.values.length = index + 1;
var default_value;
fill(function() {
default_value = createDefaultValue(recurmax, noDefault);
}, function() {
while (!rng(10)) {
var index = rng(pairs.names.length + 1);
pairs.names.splice(index, 0, "");
if (index < pairs.values.length) {
pairs.values.splice(index, 0, rng(2) ? createAssignmentValue(recurmax) : "");
} else switch (rng(5)) {
case 0:
pairs.values[index] = createAssignmentValue(recurmax);
case 1:
pairs.values.length = index + 1;
}
}
}
names.unshift("[ " + pairs.names.join(", ") + " ]");
values.unshift("[ " + pairs.values.join(", ") + " ]");
names.unshift("[ " + pairs.names.join(", ") + " ]" + default_value);
values.unshift("[ " + pairs.values.join(", ") + " ]");
});
}
break;
case 1:
@@ -521,33 +546,26 @@ function createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, was
keys[index] = key;
}
});
var save_async = async;
if (was_async != null) {
async = false;
if (save_async || was_async) avoid.push("await");
}
addAvoidVars(avoid);
var save_vars = nameLenBefore && VAR_NAMES.splice(nameLenBefore);
names.unshift("{ " + addTrailingComma(pairs.names.map(function(name, index) {
var key = index in keys ? keys[index] : rng(10) && createKey(recurmax, keys);
return key ? key + ": " + name : name;
}).join(", ")) + " }");
if (was_async != null) {
async = was_async;
if (save_async || was_async) removeAvoidVars([ avoid.pop() ]);
}
values.unshift("{ " + addTrailingComma(pairs.values.map(function(value, index) {
var key = index in keys ? keys[index] : createKey(recurmax, keys);
return key + ": " + value;
}).join(", ")) + " }");
if (save_vars) [].push.apply(VAR_NAMES, save_vars);
removeAvoidVars(avoid);
async = save_async;
fill(function() {
names.unshift("{ " + addTrailingComma(pairs.names.map(function(name, index) {
var key = index in keys ? keys[index] : rng(10) && createKey(recurmax, keys);
return key ? key + ": " + name : name;
}).join(", ")) + " }" + createDefaultValue(recurmax, noDefault));
}, function() {
values.unshift("{ " + addTrailingComma(pairs.values.map(function(value, index) {
var key = index in keys ? keys[index] : createKey(recurmax, keys);
return key + ": " + value;
}).join(", ")) + " }");
});
}
break;
default:
names.unshift(createName());
values.unshift(createAssignmentValue(recurmax));
var name = createName();
fill(function() {
names.unshift(name + createDefaultValue(recurmax, noDefault));
}, function() {
values.unshift(createAssignmentValue(recurmax));
});
break;
}
}
@@ -575,8 +593,8 @@ function createBlockVariables(recurmax, stmtDepth, canThrow, fn) {
}
unique_vars.length -= 6;
fn(function() {
addAvoidVars(consts);
addAvoidVars(lets);
consts.forEach(addAvoidVar);
lets.forEach(addAvoidVar);
if (rng(2)) {
return createDefinitions("const", consts) + "\n" + createDefinitions("let", lets) + "\n";
} else {
@@ -610,17 +628,17 @@ function createBlockVariables(recurmax, stmtDepth, canThrow, fn) {
default:
s += names.map(function(name) {
if (type == "let" && !rng(10)) {
removeAvoidVars([ name ]);
removeAvoidVar(name);
return name;
}
var value = createExpression(recurmax, NO_COMMA, stmtDepth, canThrow);
removeAvoidVars([ name ]);
removeAvoidVar(name);
return name + " = " + value;
}).join(", ") + ";";
names.length = 0;
break;
}
removeAvoidVars(names);
names.forEach(removeAvoidVar);
return s;
}
}
@@ -877,9 +895,9 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn
unique_vars.length -= 6;
if (SUPPORT.computed_key && rng(10) == 0) {
s += " catch ({ message: " + message + ", ";
addAvoidVars([ name ]);
addAvoidVar(name);
s += "[" + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + "]: " + name;
removeAvoidVars([ name ]);
removeAvoidVar(name);
s += " }) { ";
} else {
s += " catch ({ name: " + name + ", message: " + message + " }) { ";
@@ -1483,15 +1501,13 @@ function createUnaryPostfix() {
return UNARY_POSTFIX[rng(UNARY_POSTFIX.length)];
}
function addAvoidVars(names) {
avoid_vars = avoid_vars.concat(names);
function addAvoidVar(name) {
avoid_vars.push(name);
}
function removeAvoidVars(names) {
names.forEach(function(name) {
var index = avoid_vars.lastIndexOf(name);
if (index >= 0) avoid_vars.splice(index, 1);
});
function removeAvoidVar(name) {
var index = avoid_vars.lastIndexOf(name);
if (index >= 0) avoid_vars.splice(index, 1);
}
function getVarName(noConst) {
@@ -1799,6 +1815,8 @@ for (var round = 1; round <= num_iterations; round++) {
var orig_result = [ sandbox.run_code(original_code), sandbox.run_code(original_code, true) ];
errored = typeof orig_result[0] != "string";
if (errored) {
println();
println();
println("//=============================================================");
println("// original code");
try_beautify(original_code, false, orig_result[0], println);