From f778a0aa01fc3b6fe3ff3c53bb1b7eefdb26d4fe Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Sun, 10 Dec 2017 14:25:23 +0800 Subject: [PATCH] fix escape analysis for `AST_Yield` fixes #2565 --- lib/compress.js | 3 ++- test/compress/reduce_vars.js | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index 293905da..45a2cdff 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -681,7 +681,8 @@ merge(Compressor.prototype, { if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right || parent instanceof AST_Call && node !== parent.expression || parent instanceof AST_Exit && node === parent.value && node.scope !== d.scope - || parent instanceof AST_VarDef && node === parent.value) { + || parent instanceof AST_VarDef && node === parent.value + || parent instanceof AST_Yield && node === parent.value && node.scope !== d.scope) { d.escaped = true; return; } else if (parent instanceof AST_Array diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index adac930f..38207cd9 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -5148,3 +5148,42 @@ issue_2560_6: { } expect_stdout: "PASS" } + +escape_yield: { + options = { + reduce_funcs: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + function main() { + var thing = gen.next().value; + if (thing !== (thing = gen.next().value)) + console.log("FAIL"); + else + console.log("PASS"); + } + function foo() {} + function* baz(s) { + for (;;) yield foo; + } + var gen = baz(); + main(); + } + expect: { + function foo() {} + var gen = function*(s) { + for (;;) yield foo; + }(); + (function() { + var thing = gen.next().value; + if (thing !== (thing = gen.next().value)) + console.log("FAIL"); + else + console.log("PASS"); + })(); + } + expect_stdout: "PASS" + node_version: ">=4" +}