Skip to content

Commit

Permalink
Fix a collision with exported variables with short names
Browse files Browse the repository at this point in the history
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};
```
  • Loading branch information
jridgewell committed Sep 16, 2021
1 parent c4fcdcc commit 60c3fe6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/scope.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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 => {
Expand All @@ -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);
}
}
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 60c3fe6

Please sign in to comment.