fix corner cases in dead_code & reduce_vars (#4213)

fixes #4212
This commit is contained in:
Alex Lam S.L
2020-10-13 17:09:17 +01:00
committed by GitHub
parent 0e234a25c5
commit c0c04c33bb
3 changed files with 70 additions and 20 deletions

View File

@@ -963,3 +963,49 @@ issue_4210: {
}
expect_stdout: true
}
issue_4212_1: {
options = {
dead_code: true,
}
input: {
console.log({
get b() {
const a = 0;
return a /= 0;
}
}.b);
}
expect: {
console.log({
get b() {
const a = 0;
return a /= 0;
}
}.b);
}
expect_stdout: true
}
issue_4212_2: {
options = {
reduce_vars: true,
}
input: {
console.log({
get b() {
const a = 0;
return a /= 0;
}
}.b);
}
expect: {
console.log({
get b() {
const a = 0;
return a /= 0;
}
}.b);
}
expect_stdout: true
}

View File

@@ -899,21 +899,22 @@ function getDotKey(assign) {
function createAccessor(recurmax, stmtDepth, canThrow) {
var namesLenBefore = VAR_NAMES.length;
var s;
var prop1 = getDotKey();
if (rng(2) == 0) {
s = [
"get " + prop1 + "(){",
strictMode(),
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 {
var prop2;
do {
prop2 = getDotKey();
} while (prop1 == prop2);
createBlockVariables(recurmax, stmtDepth, canThrow, function(defns) {
createBlockVariables(recurmax, stmtDepth, canThrow, function(defns) {
var prop1 = getDotKey();
if (rng(2) == 0) {
s = [
"get " + prop1 + "(){",
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 {
var prop2;
do {
prop2 = getDotKey();
} while (prop1 == prop2);
s = [
"set " + prop1 + "(" + createVarName(MANDATORY) + "){",
strictMode(),
@@ -922,8 +923,8 @@ function createAccessor(recurmax, stmtDepth, canThrow) {
"this." + prop2 + createAssignment() + _createBinaryExpr(recurmax, COMMA_OK, stmtDepth, canThrow) + ";",
"},"
];
});
}
}
});
VAR_NAMES.length = namesLenBefore;
return filterDirective(s).join("\n");
}