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

@@ -221,3 +221,38 @@ numeric_literal: {
"8 7 8",
]
}
evaluate_computed_key: {
options = {
evaluate: true,
objects: true,
}
input: {
console.log({
["foo" + "bar"]: "PASS",
}.foobar);
}
expect: {
console.log({
foobar: "PASS",
}.foobar);
}
expect_stdout: "PASS"
node_version: ">=4"
}
keep_computed_key: {
options = {
side_effects: true,
}
input: {
({
[console.log("PASS")]: 42,
});
}
expect: {
console.log("PASS");
}
expect_stdout: "PASS"
node_version: ">=4"
}

View File

@@ -5,7 +5,7 @@ describe("tokens", function() {
it("Should give correct positions for accessors", function() {
// location 0 1 2 3 4
// 01234567890123456789012345678901234567890123456789
var ast = UglifyJS.parse("var obj = { get latest() { return undefined; } }");
var ast = UglifyJS.parse("var obj = { get [prop]() { return undefined; } }");
// test all AST_ObjectProperty tokens are set as expected
var found = false;
ast.walk(new UglifyJS.TreeWalker(function(node) {
@@ -13,9 +13,9 @@ describe("tokens", function() {
found = true;
assert.equal(node.start.pos, 12);
assert.equal(node.end.endpos, 46);
assert(node.key instanceof UglifyJS.AST_SymbolAccessor);
assert.equal(node.key.start.pos, 16);
assert.equal(node.key.end.endpos, 22);
assert(node.key instanceof UglifyJS.AST_SymbolRef);
assert.equal(node.key.start.pos, 17);
assert.equal(node.key.end.endpos, 21);
assert(node.value instanceof UglifyJS.AST_Accessor);
assert.equal(node.value.start.pos, 22);
assert.equal(node.value.end.endpos, 46);

View File

@@ -913,14 +913,18 @@ function getDotKey(assign) {
return key;
}
function createObjectFunction(type, recurmax, stmtDepth, canThrow) {
function createObjectKey(recurmax, stmtDepth, canThrow) {
return rng(10) ? KEYS[rng(KEYS.length)] : "[" + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + "]";
}
function createObjectFunction(recurmax, stmtDepth, canThrow) {
var namesLenBefore = VAR_NAMES.length;
var s;
createBlockVariables(recurmax, stmtDepth, canThrow, function(defns) {
switch (type) {
case "get":
switch (rng(3)) {
case 0:
s = [
"get " + getDotKey() + "(){",
"get " + createObjectKey(recurmax, stmtDepth, canThrow) + "(){",
strictMode(),
defns(),
_createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth),
@@ -928,8 +932,8 @@ function createObjectFunction(type, recurmax, stmtDepth, canThrow) {
"},",
];
break;
case "set":
var prop1 = getDotKey();
case 1:
var prop1 = createObjectKey(recurmax, stmtDepth, canThrow);
var prop2;
do {
prop2 = getDotKey();
@@ -945,7 +949,7 @@ function createObjectFunction(type, recurmax, stmtDepth, canThrow) {
break;
default:
s = [
type + "(" + createParams(NO_DUPLICATE) + "){",
createObjectKey(recurmax, stmtDepth, canThrow) + "(" + createParams(NO_DUPLICATE) + "){",
strictMode(),
defns(),
_createStatements(3, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth),
@@ -961,21 +965,15 @@ function createObjectFunction(type, recurmax, stmtDepth, canThrow) {
function createObjectLiteral(recurmax, stmtDepth, canThrow) {
recurmax--;
var obj = ["({"];
for (var i = rng(6); --i >= 0;) switch (rng(50)) {
for (var i = rng(6); --i >= 0;) switch (rng(30)) {
case 0:
obj.push(createObjectFunction("get", recurmax, stmtDepth, canThrow));
obj.push(createObjectFunction(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:
obj.push(KEYS[rng(KEYS.length)] + ":(" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + "),");
obj.push(createObjectKey(recurmax, stmtDepth, canThrow) + ":(" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + "),");
break;
}
obj.push("})");