[Pkg-javascript-commits] [uglifyjs] 24/228: smarter const replacement taking name length into account
Jonas Smedegaard
dr at jones.dk
Sat Apr 15 14:25:13 UTC 2017
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository uglifyjs.
commit 6ffbecb72b515d6e9e6dee1f76d8f27e4b014854
Author: kzc <zaxxon2011 at gmail.com>
Date: Sat Feb 18 19:12:57 2017 +0800
smarter const replacement taking name length into account
closes #1459
---
lib/compress.js | 12 ++++
test/compress/const.js | 161 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 173 insertions(+)
diff --git a/lib/compress.js b/lib/compress.js
index b66c558..1f710f1 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2784,6 +2784,18 @@ merge(Compressor.prototype, {
}
}
}
+ if (compressor.option("evaluate") && !isLHS(self, compressor.parent())) {
+ var d = self.definition();
+ if (d && d.constant && d.init && d.init.is_constant(compressor)) {
+ var original_as_string = self.print_to_string();
+ var const_node = make_node_from_constant(compressor, d.init.constant_value(compressor), self);
+ var const_node_as_string = const_node.print_to_string();
+ var per_const_overhead = d.global || !d.references.length ? 0
+ : (d.name.length + 2 + const_node_as_string.length) / d.references.length;
+ if (const_node_as_string.length <= original_as_string.length + per_const_overhead)
+ return const_node;
+ }
+ }
return self;
});
diff --git a/test/compress/const.js b/test/compress/const.js
new file mode 100644
index 0000000..dd175fc
--- /dev/null
+++ b/test/compress/const.js
@@ -0,0 +1,161 @@
+issue_1191: {
+ options = {
+ evaluate : true,
+ booleans : true,
+ comparisons : true,
+ dead_code : true,
+ conditionals : true,
+ side_effects : true,
+ unused : true,
+ hoist_funs : true,
+ if_return : true,
+ join_vars : true,
+ sequences : false,
+ collapse_vars : false,
+ }
+ input: {
+ function foo(rot) {
+ const rotTol = 5;
+ if (rot < -rotTol || rot > rotTol)
+ bar();
+ baz();
+ }
+ }
+ expect: {
+ function foo(rot) {
+ (rot < -5 || rot > 5) && bar();
+ baz();
+ }
+ }
+}
+
+issue_1194: {
+ options = {
+ evaluate : true,
+ booleans : true,
+ comparisons : true,
+ dead_code : true,
+ conditionals : true,
+ side_effects : true,
+ unused : true,
+ hoist_funs : true,
+ if_return : true,
+ join_vars : true,
+ sequences : false,
+ collapse_vars : false,
+ }
+ input: {
+ function f1() {const a = "X"; return a + a;}
+ function f2() {const aa = "X"; return aa + aa;}
+ function f3() {const aaa = "X"; return aaa + aaa;}
+ }
+ expect: {
+ function f1(){return"XX"}
+ function f2(){return"XX"}
+ function f3(){return"XX"}
+ }
+}
+
+issue_1396: {
+ options = {
+ evaluate : true,
+ booleans : true,
+ comparisons : true,
+ dead_code : true,
+ conditionals : true,
+ side_effects : true,
+ unused : true,
+ hoist_funs : true,
+ if_return : true,
+ join_vars : true,
+ sequences : false,
+ collapse_vars : false,
+ }
+ input: {
+ function foo(a) {
+ const VALUE = 1;
+ console.log(2 | VALUE);
+ console.log(VALUE + 1);
+ console.log(VALUE);
+ console.log(a & VALUE);
+ }
+ function bar() {
+ const s = "01234567890123456789";
+ console.log(s + s + s + s + s);
+
+ const CONSTANT = "abc";
+ console.log(CONSTANT + CONSTANT + CONSTANT + CONSTANT + CONSTANT);
+ }
+ }
+ expect: {
+ function foo(a) {
+ console.log(3);
+ console.log(2);
+ console.log(1);
+ console.log(1 & a);
+ }
+ function bar() {
+ const s = "01234567890123456789";
+ console.log(s + s + s + s + s);
+
+ console.log("abcabcabcabcabc");
+ }
+ }
+}
+
+unused_regexp_literal: {
+ options = {
+ evaluate : true,
+ booleans : true,
+ comparisons : true,
+ dead_code : true,
+ conditionals : true,
+ side_effects : true,
+ unused : true,
+ hoist_funs : true,
+ if_return : true,
+ join_vars : true,
+ sequences : false,
+ collapse_vars : false,
+ }
+ input: {
+ function f(){ var a = /b/; }
+ }
+ expect: {
+ function f(){}
+ }
+}
+
+regexp_literal_not_const: {
+ options = {
+ evaluate : true,
+ booleans : true,
+ comparisons : true,
+ dead_code : true,
+ conditionals : true,
+ side_effects : true,
+ unused : true,
+ hoist_funs : true,
+ if_return : true,
+ join_vars : true,
+ sequences : false,
+ collapse_vars : false,
+ }
+ input: {
+ (function(){
+ var result;
+ const s = 'acdabcdeabbb';
+ const REGEXP_LITERAL = /ab*/g;
+ while (result = REGEXP_LITERAL.exec(s)) {
+ console.log(result[0]);
+ }
+ })();
+ }
+ expect: {
+ (function() {
+ var result;
+ const REGEXP_LITERAL = /ab*/g;
+ while (result = REGEXP_LITERAL.exec("acdabcdeabbb")) console.log(result[0]);
+ })();
+ }
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git
More information about the Pkg-javascript-commits
mailing list