support computed property name in object literal (#4268)

This commit is contained in:
Alex Lam S.L
2020-11-08 15:38:32 +00:00
committed by GitHub
parent 810cd40356
commit 91fc1c82b5
10 changed files with 166 additions and 139 deletions

View File

@@ -1340,8 +1340,7 @@ function parse($TEXT, options) {
// allow trailing comma
if (!options.strict && is("punc", "}")) break;
var start = S.token;
var type = start.type;
var name = as_property_name();
var key = as_property_key();
if (is("punc", "(")) {
var func_start = S.token;
var func = function_(AST_Function);
@@ -1349,22 +1348,17 @@ function parse($TEXT, options) {
func.end = prev();
a.push(new AST_ObjectKeyVal({
start: start,
quote: start.quote,
key: "" + name,
key: key,
value: func,
end: prev(),
}));
continue;
}
if (!is("punc", ":") && type == "name") switch (name) {
if (!is("punc", ":") && start.type == "name") switch (key) {
case "get":
a.push(new AST_ObjectGetter({
start: start,
key: new AST_SymbolAccessor({
start: S.token,
name: "" + as_property_name(),
end: prev(),
}),
key: as_property_key(),
value: create_accessor(),
end: prev(),
}));
@@ -1372,11 +1366,7 @@ function parse($TEXT, options) {
case "set":
a.push(new AST_ObjectSetter({
start: start,
key: new AST_SymbolAccessor({
start: S.token,
name: "" + as_property_name(),
end: prev(),
}),
key: as_property_key(),
value: create_accessor(),
end: prev(),
}));
@@ -1384,8 +1374,7 @@ function parse($TEXT, options) {
default:
a.push(new AST_ObjectKeyVal({
start: start,
quote: start.quote,
key: "" + name,
key: key,
value: _make_symbol(AST_SymbolRef, start),
end: prev(),
}));
@@ -1394,8 +1383,7 @@ function parse($TEXT, options) {
expect(":");
a.push(new AST_ObjectKeyVal({
start: start,
quote: start.quote,
key: "" + name,
key: key,
value: expression(false),
end: prev(),
}));
@@ -1404,7 +1392,7 @@ function parse($TEXT, options) {
return new AST_Object({ properties: a });
});
function as_property_name() {
function as_property_key() {
var tmp = S.token;
switch (tmp.type) {
case "operator":
@@ -1415,7 +1403,13 @@ function parse($TEXT, options) {
case "keyword":
case "atom":
next();
return tmp.value;
return "" + tmp.value;
case "punc":
if (tmp.value != "[") unexpected();
next();
var key = expression(false);
expect("]");
return key;
default:
unexpected();
}