diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-01-07 02:04:09 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-07 10:04:09 +0800 |
commit | c3d358a5b8e00c51ba7a2e9d8c25b7771f5f5a46 (patch) | |
tree | d255e5d319d1e872a966578b9377d31e87caaed3 /test | |
parent | 68497d025877a4f364187c9ce771d863c73fe7f0 (diff) | |
download | tracifyjs-c3d358a5b8e00c51ba7a2e9d8c25b7771f5f5a46.tar.gz tracifyjs-c3d358a5b8e00c51ba7a2e9d8c25b7771f5f5a46.zip |
support rest parameters (#4515)
Diffstat (limited to 'test')
-rw-r--r-- | test/compress/awaits.js (renamed from test/compress/async.js) | 0 | ||||
-rw-r--r-- | test/compress/rests.js | 487 | ||||
-rw-r--r-- | test/compress/spreads.js (renamed from test/compress/spread.js) | 28 | ||||
-rw-r--r-- | test/reduce.js | 4 | ||||
-rw-r--r-- | test/ufuzz/index.js | 41 |
5 files changed, 538 insertions, 22 deletions
diff --git a/test/compress/async.js b/test/compress/awaits.js index 7ae753fb..7ae753fb 100644 --- a/test/compress/async.js +++ b/test/compress/awaits.js diff --git a/test/compress/rests.js b/test/compress/rests.js new file mode 100644 index 00000000..1a27860d --- /dev/null +++ b/test/compress/rests.js @@ -0,0 +1,487 @@ +arrow_1: { + input: { + console.log.apply(console, ((...a) => a)("PASS", 42)); + } + expect_exact: 'console.log.apply(console,((...a)=>a)("PASS",42));' + expect_stdout: "PASS 42" + node_version: ">=6" +} + +arrow_2: { + input: { + console.log.apply(console, ((a, ...b) => b)("FAIL", "PASS", 42)); + } + expect_exact: 'console.log.apply(console,((a,...b)=>b)("FAIL","PASS",42));' + expect_stdout: "PASS 42" + node_version: ">=6" +} + +arrow_destructured_array_1: { + input: { + console.log.apply(console, (([ ...a ]) => a)("PASS")); + } + expect_exact: 'console.log.apply(console,(([...a])=>a)("PASS"));' + expect_stdout: "P A S S" + node_version: ">=6" +} + +arrow_destructured_array_2: { + input: { + console.log.apply(console, (([ a, ...b ]) => b)([ "FAIL", "PASS", 42 ])); + } + expect_exact: 'console.log.apply(console,(([a,...b])=>b)(["FAIL","PASS",42]));' + expect_stdout: "PASS 42" + node_version: ">=6" +} + +arrow_destructured_array_3: { + input: { + console.log((([ [ ...a ] = "FAIL" ]) => a)([ "PASS" ]).join("|")); + } + expect_exact: 'console.log((([[...a]="FAIL"])=>a)(["PASS"]).join("|"));' + expect_stdout: "P|A|S|S" + node_version: ">=6" +} + +arrow_destructured_object_1: { + input: { + var f = ({ ...a }) => a, o = f({ PASS: 42 }); + for (var k in o) + console.log(k, o[k]); + } + expect_exact: "var f=({...a})=>a,o=f({PASS:42});for(var k in o)console.log(k,o[k]);" + expect_stdout: "PASS 42" + node_version: ">=8" +} + +arrow_destructured_object_2: { + input: { + var f = ({ FAIL: a, ...b }) => b, o = f({ PASS: 42, FAIL: null }); + for (var k in o) + console.log(k, o[k]); + } + expect_exact: "var f=({FAIL:a,...b})=>b,o=f({PASS:42,FAIL:null});for(var k in o)console.log(k,o[k]);" + expect_stdout: "PASS 42" + node_version: ">=8" +} + +arrow_destructured_object_3: { + input: { + var f = ([ { ...a } = [ "FAIL" ] ]) => a; + var o = f([ "PASS" ]); + for (var k in o) + console.log(k, o[k]); + } + expect_exact: 'var f=([{...a}=["FAIL"]])=>a;var o=f(["PASS"]);for(var k in o)console.log(k,o[k]);' + expect_stdout: [ + "0 P", + "1 A", + "2 S", + "3 S", + ] + node_version: ">=8" +} + +funarg_1: { + input: { + console.log.apply(console, function(...a) { + return a; + }("PASS", 42)); + } + expect_exact: 'console.log.apply(console,function(...a){return a}("PASS",42));' + expect_stdout: "PASS 42" + node_version: ">=6" +} + +funarg_2: { + input: { + console.log.apply(console, function(a, ...b) { + return b; + }("FAIL", "PASS", 42)); + } + expect_exact: 'console.log.apply(console,function(a,...b){return b}("FAIL","PASS",42));' + expect_stdout: "PASS 42" + node_version: ">=6" +} + +destructured_array_1: { + input: { + var [ ...a ] = [ "PASS", 42 ]; + console.log.apply(console, a); + } + expect_exact: 'var[...a]=["PASS",42];console.log.apply(console,a);' + expect_stdout: "PASS 42" + node_version: ">=6" +} + +destructured_array_2: { + input: { + var [ a, ...b ] = [ "FAIL", "PASS", 42 ]; + console.log.apply(console, b); + } + expect_exact: 'var[a,...b]=["FAIL","PASS",42];console.log.apply(console,b);' + expect_stdout: "PASS 42" + node_version: ">=6" +} + +destructured_object_1: { + input: { + var { ...a } = [ "FAIL", "PASS", 42 ]; + console.log(a[1], a[2]); + } + expect_exact: 'var{...a}=["FAIL","PASS",42];console.log(a[1],a[2]);' + expect_stdout: "PASS 42" + node_version: ">=8" +} + +destructured_object_2: { + input: { + var { 0: a, ...b } = [ "FAIL", "PASS", 42 ]; + console.log(b[1], b[2]); + } + expect_exact: 'var{0:a,...b}=["FAIL","PASS",42];console.log(b[1],b[2]);' + expect_stdout: "PASS 42" + node_version: ">=8" +} + +drop_fargs: { + options = { + keep_fargs: false, + rests: true, + unused: true, + } + input: { + console.log(function(a, ...b) { + return b[0]; + }("FAIL", "PASS")); + } + expect: { + console.log(function(b) { + return b[0]; + }([ "PASS" ])); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +inline: { + options = { + inline: true, + toplevel: true, + } + input: { + console.log(function(a, ...[ b, c ]) { + return c + b + a; + }("SS", "A", "P")); + } + expect: { + console.log(([ a, ...[ b, c ] ] = [ "SS", "A", "P" ], c + b + a)); + var a, b, c; + } + expect_stdout: "PASS" + node_version: ">=6" +} + +retain_var: { + options = { + unused: true, + } + input: { + var [ ...a ] = [ "PASS" ]; + console.log(a[0]); + } + expect: { + var [ ...a ] = [ "PASS" ]; + console.log(a[0]); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +reduce_destructured_array: { + options = { + reduce_vars: true, + rests: true, + toplevel: true, + unused: true, + } + input: { + var [ ...a ] = [ "PASS" ]; + console.log(a[0]); + } + expect: { + console.log([ "PASS" ][0]); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +reduce_destructured_object: { + options = { + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var { ...a } = [ "PASS" ]; + console.log(a[0]); + } + expect: { + var { ...a } = [ "PASS" ]; + console.log(a[0]); + } + expect_stdout: "PASS" + node_version: ">=8" +} + +retain_destructured_array: { + options = { + toplevel: true, + unused: true, + } + input: { + var [ a, ...b ] = [ "FAIL", "PASS", 42 ]; + console.log.apply(console, b); + } + expect: { + var [ , ...b ] = [ "FAIL", "PASS", 42 ]; + console.log.apply(console, b); + } + expect_stdout: "PASS 42" + node_version: ">=6" +} + +retain_destructured_object_1: { + options = { + toplevel: true, + unused: true, + } + input: { + var { 0: a, ...b } = [ "FAIL", "PASS", 42 ]; + for (var k in b) + console.log(k, b[k]); + } + expect: { + var { 0: a, ...b } = [ "FAIL", "PASS", 42 ]; + for (var k in b) + console.log(k, b[k]); + } + expect_stdout: [ + "1 PASS", + "2 42", + ] + node_version: ">=8" +} + +retain_destructured_object_2: { + options = { + toplevel: true, + unused: true, + } + input: { + var { foo: [ a ], ...b } = { foo: [ "FAIL" ], bar: "PASS", baz: 42 }; + for (var k in b) + console.log(k, b[k]); + } + expect: { + var { foo: [], ...b } = { foo: [ "FAIL" ], bar: "PASS", baz: 42 }; + for (var k in b) + console.log(k, b[k]); + } + expect_stdout: [ + "bar PASS", + "baz 42", + ] + node_version: ">=8" +} + +retain_funarg_destructured_array_1: { + options = { + inline: true, + keep_fargs: false, + pure_getters: "strict", + unused: true, + } + input: { + console.log((([ ...a ]) => a)([ "PASS" ])[0]); + } + expect: { + console.log((([ ...a ]) => a)([ "PASS" ])[0]); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +retain_funarg_destructured_array_2: { + options = { + unused: true, + } + input: { + console.log(function([ a, ...b ]) { + return b; + }("bar")[1]); + } + expect: { + console.log(function([ , ...b ]) { + return b; + }("bar")[1]); + } + expect_stdout: "r" + node_version: ">=6" +} + +retain_funarg_destructured_object_1: { + options = { + inline: true, + keep_fargs: false, + pure_getters: "strict", + unused: true, + } + input: { + console.log((({ ...a }) => a)([ "PASS" ])[0]); + } + expect: { + console.log((({ ...a }) => a)([ "PASS" ])[0]); + } + expect_stdout: "PASS" + node_version: ">=8" +} + +retain_funarg_destructured_object_2: { + options = { + unused: true, + } + input: { + console.log(function({ p: a, ... b }) { + return b; + }({ p: "FAIL" }).p || "PASS"); + } + expect: { + console.log(function({ p: a, ... b }) { + return b; + }({ p: "FAIL" }).p || "PASS"); + } + expect_stdout: "PASS" + node_version: ">=8" +} + +drop_unused_call_args_1: { + options = { + rests: true, + unused: true, + } + input: { + (function(...a) { + console.log(a[0]); + })(42, console.log("PASS")); + } + expect: { + (function(a) { + console.log(a[0]); + })([ 42, console.log("PASS") ]); + } + expect_stdout: [ + "PASS", + "42", + ] + node_version: ">=6" +} + +drop_unused_call_args_2: { + options = { + keep_fargs: false, + rests: true, + unused: true, + } + input: { + console.log(function(a, ...b) { + return b; + }(console).length); + } + expect: { + console.log(function(b) { + return b; + }((console, [])).length); + } + expect_stdout: "0" + node_version: ">=6" +} + +merge_funarg: { + options = { + merge_vars: true, + } + input: { + (function(...a) { + var b = a.length; + console.log(b); + })(); + } + expect: { + (function(...b) { + var b = b.length; + console.log(b); + })(); + } + expect_stdout: "0" + node_version: ">=6" +} + +merge_funarg_destructured_array: { + options = { + merge_vars: true, + } + input: { + (function([ ...a ]) { + var b = a.length; + console.log(b); + })([]); + } + expect: { + (function([ ...b ]) { + var b = b.length; + console.log(b); + })([]); + } + expect_stdout: "0" + node_version: ">=6" +} + +merge_funarg_destructured_object: { + options = { + merge_vars: true, + } + input: { + (function({ ...a }) { + var b = a[0]; + console.log(b); + })([ "PASS" ]); + } + expect: { + (function({ ...b }) { + var b = b[0]; + console.log(b); + })([ "PASS" ]); + } + expect_stdout: "PASS" + node_version: ">=8" +} + +keep_arguments: { + options = { + arguments: true, + keep_fargs: false, + } + input: { + (function(...[ {} ]) { + console.log(arguments[0]); + })("PASS"); + } + expect: { + (function(...[ {} ]) { + console.log(arguments[0]); + })("PASS"); + } + expect_stdout: "PASS" + node_version: ">=6" +} diff --git a/test/compress/spread.js b/test/compress/spreads.js index 944915fe..1e4dcbf6 100644 --- a/test/compress/spread.js +++ b/test/compress/spreads.js @@ -128,7 +128,7 @@ dont_inline: { do_inline: { options = { inline: true, - spread: true, + spreads: true, } input: { console.log(function(a) { @@ -167,7 +167,7 @@ drop_empty_call_1: { drop_empty_call_2: { options = { side_effects: true, - spread: true, + spreads: true, } input: { (function() {})(...[ console.log("PASS") ]); @@ -181,7 +181,7 @@ drop_empty_call_2: { convert_hole: { options = { - spread: true, + spreads: true, } input: { console.log(...[ "PASS", , 42 ]); @@ -275,7 +275,7 @@ reduce_vars_2: { convert_setter: { options = { objects: true, - spread: true, + spreads: true, } input: { var o = { @@ -419,7 +419,7 @@ keep_getter_4: { keep_accessor: { options = { objects: true, - spread: true, + spreads: true, } input: { var o = { @@ -467,7 +467,7 @@ keep_accessor: { object_key_order_1: { options = { objects: true, - spread: true, + spreads: true, } input: { var o = { @@ -497,7 +497,7 @@ object_key_order_1: { object_key_order_2: { options = { objects: true, - spread: true, + spreads: true, } input: { var o = { @@ -527,7 +527,7 @@ object_key_order_2: { object_key_order_3: { options = { objects: true, - spread: true, + spreads: true, } input: { var o = { @@ -557,7 +557,7 @@ object_key_order_3: { object_key_order_4: { options = { objects: true, - spread: true, + spreads: true, } input: { var o = { @@ -587,7 +587,7 @@ object_key_order_4: { object_spread_array: { options = { objects: true, - spread: true, + spreads: true, } input: { var o = { @@ -613,7 +613,7 @@ object_spread_array: { object_spread_string: { options = { objects: true, - spread: true, + spreads: true, } input: { var o = { @@ -670,7 +670,7 @@ unused_var_side_effects: { issue_4329: { options = { objects: true, - spread: true, + spreads: true, } input: { console.log({ @@ -749,7 +749,7 @@ issue_4342: { issue_4345: { options = { objects: true, - spread: true, + spreads: true, } input: { console.log({ @@ -809,7 +809,7 @@ issue_4361: { issue_4363: { options = { objects: true, - spread: true, + spreads: true, } input: { ({ diff --git a/test/reduce.js b/test/reduce.js index 54494c56..cd00d5e9 100644 --- a/test/reduce.js +++ b/test/reduce.js @@ -452,6 +452,10 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options) CHANGED = true; return List.skip; } + } else if (parent.rest === node) { + node.start._permute++; + CHANGED = true; + return null; } // replace this node diff --git a/test/ufuzz/index.js b/test/ufuzz/index.js index 23b3a144..7f2a7d72 100644 --- a/test/ufuzz/index.js +++ b/test/ufuzz/index.js @@ -140,6 +140,8 @@ var SUPPORT = function(matrix) { default_value: "[ a = 0 ] = [];", destructuring: "[] = [];", let: "let a;", + rest: "var [...a] = [];", + rest_object: "var {...a} = {};", spread: "[...[]];", spread_object: "({...0});", trailing_comma: "function f(a,) {}", @@ -427,9 +429,22 @@ function createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, was var avoid = []; var len = unique_vars.length; var pairs = createPairs(recurmax, !nameLenBefore); + pairs.has_rest = nameLenBefore && convertToRest(pairs.names); unique_vars.length = len; return pairs; + function convertToRest(names) { + var last = names.length - 1; + if (last >= 0 && SUPPORT.rest && rng(20) == 0) { + var name = names[last]; + if (name && name.indexOf("=") < 0) { + if (/^[[{]/.test(name)) name = "[ " + name + " ]"; + names[last] = "..." + name; + return true; + } + } + } + function fill(nameFn, valueFn) { var save_async = async; if (was_async != null) { @@ -519,12 +534,13 @@ function createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, was if (index < pairs.values.length) { pairs.values.splice(index, 0, rng(2) ? createAssignmentValue(recurmax) : ""); } else switch (rng(5)) { - case 0: + case 0: pairs.values[index] = createAssignmentValue(recurmax); - case 1: + case 1: pairs.values.length = index + 1; } } + convertToRest(pairs.names); names.unshift("[ " + pairs.names.join(", ") + " ]" + default_value); values.unshift("[ " + pairs.values.join(", ") + " ]"); }); @@ -547,10 +563,17 @@ function createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, was } }); 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)); + var last = pairs.names.length - 1, has_rest = false; + var s = pairs.names.map(function(name, index) { + if (index in keys) return keys[index] + ": " + name; + if (index == last && SUPPORT.rest_object && rng(20) == 0 && name.indexOf("=") < 0) { + has_rest = true; + return "..." + name; + } + return rng(10) == 0 ? name : createKey(recurmax, keys) + ": " + name; + }).join(", "); + if (!has_rest) s = addTrailingComma(s); + names.unshift("{ " + s + " }" + createDefaultValue(recurmax, noDefault)); }, function() { values.unshift("{ " + addTrailingComma(pairs.values.map(function(value, index) { var key = index in keys ? keys[index] : createKey(recurmax, keys); @@ -677,7 +700,8 @@ function createFunction(recurmax, allowDefun, canThrow, stmtDepth) { if (SUPPORT.destructuring && (!allowDefun || !(name in called)) && rng(2)) { called[name] = false; var pairs = createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, save_async); - params = addTrailingComma(pairs.names.join(", ")); + params = pairs.names.join(", "); + if (!pairs.has_rest) params = addTrailingComma(params); args = addTrailingComma(pairs.values.join(", ")); } else { params = createParams(save_async); @@ -1037,7 +1061,8 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) { var params; if (SUPPORT.destructuring && rng(2)) { var pairs = createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, save_async); - params = addTrailingComma(pairs.names.join(", ")); + params = pairs.names.join(", "); + if (!pairs.has_rest) params = addTrailingComma(params); args = addTrailingComma(pairs.values.join(", ")); } else { params = createParams(save_async, NO_DUPLICATE); |