From 996fc0da8145446150b31ae464fd981d240e0108 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Thu, 16 Sep 2021 00:51:54 -0400 Subject: [PATCH] Fix a collision with exported variables with short names In some cases, it's possible to generate a collision between an exported variable declaration (which cannot be mangled) and a local reference. Eg, ```js // reduce_vars: false const n = null; export const o = {o: n}; ``` --- lib/scope.js | 10 ++++++---- test/compress/export.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index 572f7d322..9501418fe 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -781,8 +781,11 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) { } const mangled_names = this.mangled_names = new Set(); + unmangleable_names = new Set(); + if (options.cache) { this.globals.forEach(collect); + // TODO if (options.cache.props) { options.cache.props.forEach(function(mangled_name) { mangled_names.add(mangled_name); @@ -833,7 +836,6 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) { this.walk(tw); if (options.keep_fnames || options.keep_classnames) { - unmangleable_names = new Set(); // Collect a set of short names which are unmangleable, // for use in avoiding collisions in next_mangled. to_mangle.forEach(def => { @@ -849,9 +851,9 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) { unmangleable_names = null; function collect(symbol) { - const should_mangle = !options.reserved.has(symbol.name) - && !(symbol.export & MASK_EXPORT_DONT_MANGLE); - if (should_mangle) { + if (symbol.export & MASK_EXPORT_DONT_MANGLE) { + unmangleable_names.add(symbol.name); + } else if (!options.reserved.has(symbol.name)) { to_mangle.push(symbol); } } diff --git a/test/compress/export.js b/test/compress/export.js index 417989ada..17b8e03e3 100644 --- a/test/compress/export.js +++ b/test/compress/export.js @@ -921,3 +921,34 @@ issue_333_toplevel: { export { _setToString }; } } + +export_object_property_mangle: { + module = true + options = { + reduce_vars: false, + } + mangle = true + input: { + const n = null; + export const o = { o: n }; + } + expect: { + const n = null; + export const o = {o: n}; + } +} + +export_object_property_mangle_2: { + module = true + mangle = true + input: { + const n = null; + const o = { o: n }; + export { o }; + } + expect: { + const o = null; + const n = {o: o}; + export {n as o}; + } +}