From 8922f08fbf9719393d999eb148247f8968dc66af Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 28 Mar 2022 20:01:01 +0100 Subject: [PATCH] fix corner cases in `keep_fnames` (#5393) --- lib/compress.js | 1 + lib/minify.js | 18 +++++++++--------- test/compress.js | 6 ++---- test/compress/classes.js | 2 ++ test/compress/functions.js | 21 +++++++++++++++++++++ 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index a25c3d66..a0b8b302 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -7121,6 +7121,7 @@ Compressor.prototype.compress = function(node) { if (def.orig.length > 1) return null; if (def.assignments > 0) return false; if (def.name == name) return def; + if (compressor.option("keep_fnames")) return false; var forbidden; switch (name) { case "await": diff --git a/lib/minify.js b/lib/minify.js index aca7f5fd..c6560473 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -96,15 +96,14 @@ function minify(files, options) { }, true); if (options.validate) AST_Node.enable_validation(); var timings = options.timings && { start: Date.now() }; - if (options.rename === undefined) options.rename = options.compress && options.mangle; if (options.annotations !== undefined) set_shorthand("annotations", options, [ "compress", "output" ]); if (options.ie8) options.ie = options.ie || options.ie8; - if (options.ie) set_shorthand("ie", options, [ "compress", "mangle", "output" ]); - if (options.keep_fargs) set_shorthand("keep_fargs", options, [ "compress", "mangle" ]); - if (options.keep_fnames) set_shorthand("keep_fnames", options, [ "compress", "mangle" ]); - if (options.toplevel) set_shorthand("toplevel", options, [ "compress", "mangle" ]); - if (options.v8) set_shorthand("v8", options, [ "mangle", "output" ]); - if (options.webkit) set_shorthand("webkit", options, [ "compress", "mangle", "output" ]); + if (options.ie) set_shorthand("ie", options, [ "compress", "mangle", "output", "rename" ]); + if (options.keep_fargs) set_shorthand("keep_fargs", options, [ "compress", "mangle", "rename" ]); + if (options.keep_fnames) set_shorthand("keep_fnames", options, [ "compress", "mangle", "rename" ]); + if (options.toplevel) set_shorthand("toplevel", options, [ "compress", "mangle", "rename" ]); + if (options.v8) set_shorthand("v8", options, [ "mangle", "output", "rename" ]); + if (options.webkit) set_shorthand("webkit", options, [ "compress", "mangle", "output", "rename" ]); var quoted_props; if (options.mangle) { options.mangle = defaults(options.mangle, { @@ -135,6 +134,7 @@ function minify(files, options) { init_cache(options.mangle.cache); init_cache(options.mangle.properties.cache); } + if (options.rename === undefined) options.rename = options.compress && options.mangle; if (options.sourceMap) { options.sourceMap = defaults(options.sourceMap, { content: null, @@ -190,8 +190,8 @@ function minify(files, options) { if (options.validate) toplevel.validate_ast(); if (timings) timings.rename = Date.now(); if (options.rename) { - toplevel.figure_out_scope(options.mangle); - toplevel.expand_names(options.mangle); + toplevel.figure_out_scope(options.rename); + toplevel.expand_names(options.rename); } if (timings) timings.compress = Date.now(); if (options.compress) { diff --git a/test/compress.js b/test/compress.js index a45de96f..2b7259f7 100644 --- a/test/compress.js +++ b/test/compress.js @@ -183,13 +183,11 @@ function parse_test(file) { function reminify(orig_options, input_code, input_formatted, stdout) { for (var i = 0; i < minify_options.length; i++) { var options = JSON.parse(minify_options[i]); - if (options.compress) [ + [ "keep_fargs", "keep_fnames", ].forEach(function(name) { - if (name in orig_options) { - options.compress[name] = orig_options[name]; - } + if (name in orig_options) options[name] = orig_options[name]; }); var options_formatted = JSON.stringify(options, null, 4); options.validate = true; diff --git a/test/compress/classes.js b/test/compress/classes.js index afe814e1..11936b5f 100644 --- a/test/compress/classes.js +++ b/test/compress/classes.js @@ -939,6 +939,8 @@ keep_fnames: { class Foo {} console.log(Foo.name, class Bar {}.name); } + expect_stdout: "Foo Bar" + node_version: ">=4" } issue_805_1: { diff --git a/test/compress/functions.js b/test/compress/functions.js index 40ed7fc6..02f90f9b 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -3557,6 +3557,27 @@ functions_inner_var: { expect_stdout: "undefined undefined" } +functions_keep_fnames: { + options = { + functions: true, + keep_fnames: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var FAIL = function PASS() {}; + FAIL.p = 42; + console.log(FAIL.name, FAIL.p); + } + expect: { + var FAIL = function PASS() {}; + FAIL.p = 42; + console.log(FAIL.name, FAIL.p); + } + expect_stdout: "PASS 42" +} + issue_2437: { options = { collapse_vars: true,