Skip to content

Commit

Permalink
opt out of tricky mangling when a function definition is placed insid…
Browse files Browse the repository at this point in the history
…e a block. Closes #1155
  • Loading branch information
fabiosantoscode committed Mar 14, 2022
1 parent a6c87ad commit 2c8b37d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/scope.js
Expand Up @@ -116,6 +116,11 @@ const MASK_EXPORT_WANT_MANGLE = 1 << 1;

let function_defs = null;
let unmangleable_names = null;
/**
* When defined, there is a function declaration somewhere that's inside of a block.
* See https://tc39.es/ecma262/multipage/additional-ecmascript-features-for-web-browsers.html#sec-block-level-function-declarations-web-legacy-compatibility-semantics
*/
let scopes_with_block_defuns = null;

class SymbolDef {
constructor(scope, orig, init) {
Expand Down Expand Up @@ -666,6 +671,15 @@ AST_Scope.DEFMETHOD("def_variable", function(symbol, init) {
});

function next_mangled(scope, options) {
let defun_scope;
if (
scopes_with_block_defuns
&& (defun_scope = scope.get_defun_scope())
&& scopes_with_block_defuns.has(defun_scope)
) {
scope = defun_scope;
}

var ext = scope.enclosed;
var nth_identifier = options.nth_identifier;
out: while (true) {
Expand Down Expand Up @@ -800,6 +814,13 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
lname = save_nesting;
return true; // don't descend again in TreeWalker
}
if (
node instanceof AST_Defun
&& !(tw.parent() instanceof AST_Scope)
) {
scopes_with_block_defuns = scopes_with_block_defuns || new Set();
scopes_with_block_defuns.add(node.parent_scope.get_defun_scope());
}
if (node instanceof AST_Scope) {
node.variables.forEach(collect);
return;
Expand Down Expand Up @@ -848,6 +869,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {

function_defs = null;
unmangleable_names = null;
scopes_with_block_defuns = null;

function collect(symbol) {
if (symbol.export & MASK_EXPORT_DONT_MANGLE) {
Expand Down
55 changes: 55 additions & 0 deletions test/compress/blocks.js
Expand Up @@ -183,6 +183,61 @@ issue_1672_if_strict: {
expect_stdout: true
}

issue_t1155_function_in_block: {
options = { unused: false }
mangle = {}
input: {
function f1() {
if ("aaaaaaa") {
function f2() {
return 1;
}

const var1 = "bbbbbbb";
}
}
}
expect: {
function f1() {
if ("aaaaaaa") {
function a() {
return 1;
}

const b = "bbbbbbb";
}
}
}
}

issue_t1155_function_in_other_block: {
options = { unused: false }
mangle = {}
input: {
function f1() {
if ("aaaaaaaaaannnnnnn") {
function f2() {
return 1;
}
}
if ("aaaaaaaaaannnnnnn") {
const f2 = "aaaaaaaaaannnnnnn";
}
}
}
expect: {
function f1() {
if ("aaaaaaaaaannnnnnn")
function a() {
return 1;
}
if ("aaaaaaaaaannnnnnn") {
const n = "aaaaaaaaaannnnnnn";
}
}
}
}

issue_2946_else_const: {
input: {
if (1) {
Expand Down

0 comments on commit 2c8b37d

Please sign in to comment.