Skip to content

Commit

Permalink
Fix a collision with exported variables with short names (#1072)
Browse files Browse the repository at this point in the history
* 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};
```

* Remove TODO comment
  • Loading branch information
jridgewell committed Sep 22, 2021
1 parent ad494a7 commit ecb869e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/scope.js
Expand Up @@ -781,6 +781,8 @@ 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);
if (options.cache.props) {
Expand Down Expand Up @@ -833,7 +835,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 => {
Expand All @@ -849,9 +850,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);
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/compress/export.js
Expand Up @@ -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};
}
}

0 comments on commit ecb869e

Please sign in to comment.