From: Alex Lam S.L Date: Sun, 8 Nov 2020 05:17:53 +0000 (+0000) Subject: support shorthand method name in object literal (#4264) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=1cbd07e7897bcbb879921c63b9794f7a278ca5a6;p=UglifyJS.git support shorthand method name in object literal (#4264) --- diff --git a/lib/parse.js b/lib/parse.js index 78311f89..84b14439 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1342,6 +1342,20 @@ function parse($TEXT, options) { var start = S.token; var type = start.type; var name = as_property_name(); + if (is("punc", "(")) { + var func_start = S.token; + var func = function_(AST_Function); + func.start = func_start; + func.end = prev(); + a.push(new AST_ObjectKeyVal({ + start: start, + quote: start.quote, + key: "" + name, + value: func, + end: prev(), + })); + continue; + } if (!is("punc", ":") && type == "name") switch (name) { case "get": a.push(new AST_ObjectGetter({ @@ -1379,11 +1393,11 @@ function parse($TEXT, options) { } expect(":"); a.push(new AST_ObjectKeyVal({ - start : start, - quote : start.quote, - key : "" + name, - value : expression(false), - end : prev() + start: start, + quote: start.quote, + key: "" + name, + value: expression(false), + end: prev(), })); } next(); diff --git a/test/ufuzz/index.js b/test/ufuzz/index.js index 48e5a2fe..f158854d 100644 --- a/test/ufuzz/index.js +++ b/test/ufuzz/index.js @@ -276,6 +276,7 @@ var NO_DEFUN = false; var DEFUN_OK = true; var DONT_STORE = true; var NO_CONST = true; +var NO_DUPLICATE = true; var VAR_NAMES = [ "a", @@ -356,11 +357,15 @@ function createFunctions(n, recurmax, allowDefun, canThrow, stmtDepth) { return s; } -function createParams() { +function createParams(noDuplicate) { + var len = unique_vars.length; var params = []; for (var n = rng(4); --n >= 0;) { - params.push(createVarName(MANDATORY)); + var name = createVarName(MANDATORY); + if (noDuplicate) unique_vars.push(name); + params.push(name); } + unique_vars.length = len; return params.join(", "); } @@ -908,21 +913,23 @@ function getDotKey(assign) { return key; } -function createAccessor(recurmax, stmtDepth, canThrow) { +function createObjectFunction(type, recurmax, stmtDepth, canThrow) { var namesLenBefore = VAR_NAMES.length; var s; createBlockVariables(recurmax, stmtDepth, canThrow, function(defns) { - var prop1 = getDotKey(); - if (rng(2) == 0) { + switch (type) { + case "get": s = [ - "get " + prop1 + "(){", + "get " + getDotKey() + "(){", strictMode(), defns(), _createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), createStatement(recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth, STMT_RETURN_ETC), - "}," + "},", ]; - } else { + break; + case "set": + var prop1 = getDotKey(); var prop2; do { prop2 = getDotKey(); @@ -933,8 +940,18 @@ function createAccessor(recurmax, stmtDepth, canThrow) { defns(), _createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), "this." + prop2 + createAssignment() + _createBinaryExpr(recurmax, COMMA_OK, stmtDepth, canThrow) + ";", - "}," + "},", ]; + break; + default: + s = [ + type + "(" + createParams(NO_DUPLICATE) + "){", + strictMode(), + defns(), + _createStatements(3, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), + "},", + ] + break; } }); VAR_NAMES.length = namesLenBefore; @@ -944,11 +961,17 @@ function createAccessor(recurmax, stmtDepth, canThrow) { function createObjectLiteral(recurmax, stmtDepth, canThrow) { recurmax--; var obj = ["({"]; - for (var i = rng(6); --i >= 0;) switch (rng(20)) { + for (var i = rng(6); --i >= 0;) switch (rng(50)) { case 0: - obj.push(createAccessor(recurmax, stmtDepth, canThrow)); + obj.push(createObjectFunction("get", recurmax, stmtDepth, canThrow)); break; case 1: + obj.push(createObjectFunction("set", recurmax, stmtDepth, canThrow)); + break; + case 2: + obj.push(createObjectFunction(KEYS[rng(KEYS.length)], recurmax, stmtDepth, canThrow)); + break; + case 3: obj.push(getVarName() + ","); break; default: