aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-11-08 05:17:53 +0000
committerGitHub <noreply@github.com>2020-11-08 13:17:53 +0800
commit1cbd07e7897bcbb879921c63b9794f7a278ca5a6 (patch)
tree0289e7cac60d186f9f2af9a98c1fd3b7808d7af7
parentb82de04775a490f7a1ebf7a8911f7d1a24a5f0b6 (diff)
downloadtracifyjs-1cbd07e7897bcbb879921c63b9794f7a278ca5a6.tar.gz
tracifyjs-1cbd07e7897bcbb879921c63b9794f7a278ca5a6.zip
support shorthand method name in object literal (#4264)
-rw-r--r--lib/parse.js24
-rw-r--r--test/ufuzz/index.js45
2 files changed, 53 insertions, 16 deletions
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: