From 862b1b77b5215dce8b4b3ff7a266e4083554869a Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 11 Mar 2021 01:16:16 +0000 Subject: [PATCH] fix corner cases in `merge_vars` & with `exports` (#4762) fixes #4761 --- lib/compress.js | 18 +++++++++++------- lib/parse.js | 8 ++++---- test/compress/exports.js | 7 +++++++ test/compress/merge_vars.js | 20 ++++++++++++++++++++ 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index d01ebbea..807872f4 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5605,21 +5605,25 @@ merge(Compressor.prototype, { } function mark(sym, read) { - if (in_try) push(); - var def = sym.definition(), ldef; + var def = sym.definition(), ldef, seg = segment; + if (in_try) { + push(); + seg = segment; + pop(); + } if (def.id in references) { var refs = references[def.id]; if (!refs) return; - if (refs.start.block !== segment.block) return references[def.id] = false; + if (refs.start.block !== seg.block) return references[def.id] = false; refs.push(sym); - refs.end = segment; + refs.end = seg; if (def.id in prev) { last[prev[def.id]] = null; } else if (!read) { return; } } else if ((ldef = self.variables.get(def.name)) !== def) { - if (ldef && root === segment) references[ldef.id] = false; + if (ldef && root === seg) references[ldef.id] = false; return references[def.id] = false; } else if (compressor.exposed(def) || NO_MERGE[sym.name]) { return references[def.id] = false; @@ -5628,13 +5632,13 @@ merge(Compressor.prototype, { refs.push(sym); references[def.id] = refs; if (!read) { - refs.start = segment; + refs.start = seg; return first.push({ index: index++, definition: def, }); } - if (segment.block !== self) return references[def.id] = false; + if (seg.block !== self) return references[def.id] = false; refs.start = root; } prev[def.id] = last.length; diff --git a/lib/parse.js b/lib/parse.js index 5a4d9811..ef88e72c 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1504,14 +1504,14 @@ function parse($TEXT, options) { } function export_default_decl() { - switch (S.token.value) { - case "async": + if (is("name", "async")) { if (!is_token(peek(), "keyword", "function")) return; next(); next(); if (!is("operator", "*")) return maybe_named(AST_AsyncDefun, function_(AST_AsyncFunction)); next(); return maybe_named(AST_AsyncGeneratorDefun, function_(AST_AsyncGeneratorFunction)); + } else if (is("keyword")) switch (S.token.value) { case "class": next(); return maybe_named(AST_DefClass, class_(AST_ClassExpression)); @@ -1524,13 +1524,13 @@ function parse($TEXT, options) { } var export_decl = embed_tokens(function() { - switch (S.token.value) { - case "async": + if (is("name", "async")) { next(); expect_token("keyword", "function"); if (!is("operator", "*")) return function_(AST_AsyncDefun); next(); return function_(AST_AsyncGeneratorDefun); + } else if (is("keyword")) switch (S.token.value) { case "class": next(); return class_(AST_DefClass); diff --git a/test/compress/exports.js b/test/compress/exports.js index 3aaae4ae..33059bca 100644 --- a/test/compress/exports.js +++ b/test/compress/exports.js @@ -457,3 +457,10 @@ issue_4742_unused_2: { a = "bar"; } } + +issue_4761: { + input: { + export default "function" == 42; + } + expect_exact: 'export default"function"==42;' +} diff --git a/test/compress/merge_vars.js b/test/compress/merge_vars.js index a26c0dd8..0494acfb 100644 --- a/test/compress/merge_vars.js +++ b/test/compress/merge_vars.js @@ -3281,3 +3281,23 @@ issue_4759: { } expect_stdout: "undefined" } + +issue_4761: { + options = { + merge_vars: true, + toplevel: true, + } + input: { + var a = "FAIL", b; + try { + !a && --a && (b = 0)[console] || console.log(b); + } catch (e) {} + } + expect: { + var a = "FAIL", b; + try { + !a && --a && (b = 0)[console] || console.log(b); + } catch (e) {} + } + expect_stdout: "undefined" +} -- 2.34.1