diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-12-20 00:19:04 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-20 08:19:04 +0800 |
commit | b7c49b72b3abd5da869d882a4e122cb155382874 (patch) | |
tree | 7488822884452643995370842eafc7fd23982e72 | |
parent | 8ce3c7d70fb476a65dc23f27bb5444ee89e59652 (diff) | |
download | tracifyjs-b7c49b72b3abd5da869d882a4e122cb155382874.tar.gz tracifyjs-b7c49b72b3abd5da869d882a4e122cb155382874.zip |
support async function within object literal (#4424)
-rw-r--r-- | lib/parse.js | 50 | ||||
-rw-r--r-- | test/compress/async.js | 21 | ||||
-rw-r--r-- | test/compress/objects.js | 23 | ||||
-rw-r--r-- | test/ufuzz/index.js | 10 |
4 files changed, 85 insertions, 19 deletions
diff --git a/lib/parse.js b/lib/parse.js index 3b7d15ca..27fa0424 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1482,7 +1482,39 @@ function parse($TEXT, options) { })); continue; } - if (!is("punc", ":") && start.type == "name") switch (key) { + if (is("punc", ":")) { + next(); + a.push(new AST_ObjectKeyVal({ + start: start, + key: key, + value: maybe_assign(), + end: prev(), + })); + continue; + } + if (is("punc", ",") || is("punc", "}")) { + a.push(new AST_ObjectKeyVal({ + start: start, + key: key, + value: _make_symbol(AST_SymbolRef, start), + end: prev(), + })); + continue; + } + if (start.type == "name") switch (key) { + case "async": + key = as_property_key(); + var func_start = S.token; + var func = function_(AST_AsyncFunction); + func.start = func_start; + func.end = prev(); + a.push(new AST_ObjectKeyVal({ + start: start, + key: key, + value: func, + end: prev(), + })); + continue; case "get": a.push(new AST_ObjectGetter({ start: start, @@ -1499,22 +1531,8 @@ function parse($TEXT, options) { end: prev(), })); continue; - default: - a.push(new AST_ObjectKeyVal({ - start: start, - key: key, - value: _make_symbol(AST_SymbolRef, start), - end: prev(), - })); - continue; } - expect(":"); - a.push(new AST_ObjectKeyVal({ - start: start, - key: key, - value: maybe_assign(), - end: prev(), - })); + unexpected(); } next(); return new AST_Object({ properties: a }); diff --git a/test/compress/async.js b/test/compress/async.js index d73bba41..ffd941c8 100644 --- a/test/compress/async.js +++ b/test/compress/async.js @@ -143,6 +143,27 @@ negate_iife: { node_version: ">=8" } +object_function: { + options = { + properties: true, + side_effects: true, + } + input: { + ({ + async f() { + console.log("PASS"); + }, + }).f(); + } + expect: { + (async function() { + console.log("PASS"); + })(); + } + expect_stdout: "PASS" + node_version: ">=8" +} + collapse_vars_1: { options = { collapse_vars: true, diff --git a/test/compress/objects.js b/test/compress/objects.js index d35f1538..67acefd6 100644 --- a/test/compress/objects.js +++ b/test/compress/objects.js @@ -257,6 +257,29 @@ keep_computed_key: { node_version: ">=4" } +shorthand_keywords: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unsafe: true, + unused: true, + } + input: { + var async = 1, get = 2, set = 3, o = { + async, + get, + set, + }; + console.log(o.async, o.get, o.set); + } + expect: { + console.log(1, 2, 3); + } + expect_stdout: "1 2 3" + node_version: ">=6" +} + issue_4269_1: { options = { evaluate: true, diff --git a/test/ufuzz/index.js b/test/ufuzz/index.js index 77a69a51..8fce55de 100644 --- a/test/ufuzz/index.js +++ b/test/ufuzz/index.js @@ -1262,13 +1262,14 @@ function createObjectKey(recurmax, stmtDepth, canThrow) { function createObjectFunction(recurmax, stmtDepth, canThrow) { var nameLenBefore = VAR_NAMES.length; var save_async = async; - async = SUPPORT.async && rng(50) == 0; var s; createBlockVariables(recurmax, stmtDepth, canThrow, function(defns) { switch (rng(SUPPORT.computed_key ? 3 : 2)) { case 0: + var name = createObjectKey(recurmax, stmtDepth, canThrow); + async = false; s = [ - "get " + createObjectKey(recurmax, stmtDepth, canThrow) + "(){", + "get " + name + "(){", strictMode(), defns(), _createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), @@ -1282,6 +1283,7 @@ function createObjectFunction(recurmax, stmtDepth, canThrow) { do { prop2 = getDotKey(); } while (prop1 == prop2); + async = false; s = [ "set " + prop1 + "(" + createVarName(MANDATORY) + "){", strictMode(), @@ -1292,8 +1294,10 @@ function createObjectFunction(recurmax, stmtDepth, canThrow) { ]; break; default: + var name = createObjectKey(recurmax, stmtDepth, canThrow); + async = SUPPORT.async && rng(50) == 0; s = [ - createObjectKey(recurmax, stmtDepth, canThrow) + "(" + createParams(save_async, NO_DUPLICATE) + "){", + (async ? "async " : "") + name + "(" + createParams(save_async, NO_DUPLICATE) + "){", strictMode(), defns(), _createStatements(3, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), |