support shorthand method name in object literal (#4264)
This commit is contained in:
24
lib/parse.js
24
lib/parse.js
@@ -1342,6 +1342,20 @@ function parse($TEXT, options) {
|
|||||||
var start = S.token;
|
var start = S.token;
|
||||||
var type = start.type;
|
var type = start.type;
|
||||||
var name = as_property_name();
|
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) {
|
if (!is("punc", ":") && type == "name") switch (name) {
|
||||||
case "get":
|
case "get":
|
||||||
a.push(new AST_ObjectGetter({
|
a.push(new AST_ObjectGetter({
|
||||||
@@ -1379,11 +1393,11 @@ function parse($TEXT, options) {
|
|||||||
}
|
}
|
||||||
expect(":");
|
expect(":");
|
||||||
a.push(new AST_ObjectKeyVal({
|
a.push(new AST_ObjectKeyVal({
|
||||||
start : start,
|
start: start,
|
||||||
quote : start.quote,
|
quote: start.quote,
|
||||||
key : "" + name,
|
key: "" + name,
|
||||||
value : expression(false),
|
value: expression(false),
|
||||||
end : prev()
|
end: prev(),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
|
|||||||
@@ -276,6 +276,7 @@ var NO_DEFUN = false;
|
|||||||
var DEFUN_OK = true;
|
var DEFUN_OK = true;
|
||||||
var DONT_STORE = true;
|
var DONT_STORE = true;
|
||||||
var NO_CONST = true;
|
var NO_CONST = true;
|
||||||
|
var NO_DUPLICATE = true;
|
||||||
|
|
||||||
var VAR_NAMES = [
|
var VAR_NAMES = [
|
||||||
"a",
|
"a",
|
||||||
@@ -356,11 +357,15 @@ function createFunctions(n, recurmax, allowDefun, canThrow, stmtDepth) {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createParams() {
|
function createParams(noDuplicate) {
|
||||||
|
var len = unique_vars.length;
|
||||||
var params = [];
|
var params = [];
|
||||||
for (var n = rng(4); --n >= 0;) {
|
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(", ");
|
return params.join(", ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -908,21 +913,23 @@ function getDotKey(assign) {
|
|||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createAccessor(recurmax, stmtDepth, canThrow) {
|
function createObjectFunction(type, recurmax, stmtDepth, canThrow) {
|
||||||
var namesLenBefore = VAR_NAMES.length;
|
var namesLenBefore = VAR_NAMES.length;
|
||||||
var s;
|
var s;
|
||||||
createBlockVariables(recurmax, stmtDepth, canThrow, function(defns) {
|
createBlockVariables(recurmax, stmtDepth, canThrow, function(defns) {
|
||||||
var prop1 = getDotKey();
|
switch (type) {
|
||||||
if (rng(2) == 0) {
|
case "get":
|
||||||
s = [
|
s = [
|
||||||
"get " + prop1 + "(){",
|
"get " + getDotKey() + "(){",
|
||||||
strictMode(),
|
strictMode(),
|
||||||
defns(),
|
defns(),
|
||||||
_createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth),
|
_createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth),
|
||||||
createStatement(recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth, STMT_RETURN_ETC),
|
createStatement(recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth, STMT_RETURN_ETC),
|
||||||
"},"
|
"},",
|
||||||
];
|
];
|
||||||
} else {
|
break;
|
||||||
|
case "set":
|
||||||
|
var prop1 = getDotKey();
|
||||||
var prop2;
|
var prop2;
|
||||||
do {
|
do {
|
||||||
prop2 = getDotKey();
|
prop2 = getDotKey();
|
||||||
@@ -933,8 +940,18 @@ function createAccessor(recurmax, stmtDepth, canThrow) {
|
|||||||
defns(),
|
defns(),
|
||||||
_createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth),
|
_createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth),
|
||||||
"this." + prop2 + createAssignment() + _createBinaryExpr(recurmax, COMMA_OK, stmtDepth, canThrow) + ";",
|
"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;
|
VAR_NAMES.length = namesLenBefore;
|
||||||
@@ -944,11 +961,17 @@ function createAccessor(recurmax, stmtDepth, canThrow) {
|
|||||||
function createObjectLiteral(recurmax, stmtDepth, canThrow) {
|
function createObjectLiteral(recurmax, stmtDepth, canThrow) {
|
||||||
recurmax--;
|
recurmax--;
|
||||||
var obj = ["({"];
|
var obj = ["({"];
|
||||||
for (var i = rng(6); --i >= 0;) switch (rng(20)) {
|
for (var i = rng(6); --i >= 0;) switch (rng(50)) {
|
||||||
case 0:
|
case 0:
|
||||||
obj.push(createAccessor(recurmax, stmtDepth, canThrow));
|
obj.push(createObjectFunction("get", recurmax, stmtDepth, canThrow));
|
||||||
break;
|
break;
|
||||||
case 1:
|
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() + ",");
|
obj.push(getVarName() + ",");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user