aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Van de Gejuchte <anthonyvdgent@gmail.com>2016-06-10 00:34:20 +0200
committerRichard van Velzen <rvanvelzen@experty.com>2016-06-12 14:30:28 +0200
commitea31da24559b095a18f7615cfd3c992ecaf495a3 (patch)
tree3f3369671c77efe8b9dda1b62f185ceaf368e607
parent4d7746baf31405427209de0d8c44d9c8263a2563 (diff)
downloadtracifyjs-ea31da24559b095a18f7615cfd3c992ecaf495a3.tar.gz
tracifyjs-ea31da24559b095a18f7615cfd3c992ecaf495a3.zip
Don't drop unused if scope uses with statement
Fix provided by @kzc
-rw-r--r--lib/compress.js1
-rw-r--r--test/compress/issue-1105.js147
2 files changed, 148 insertions, 0 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 461c3c47..4e04e961 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1326,6 +1326,7 @@ merge(Compressor.prototype, {
if (compressor.option("unused")
&& !(self instanceof AST_Toplevel)
&& !self.uses_eval
+ && !self.uses_with
) {
var in_use = [];
var in_use_ids = {}; // avoid expensive linear scans of in_use
diff --git a/test/compress/issue-1105.js b/test/compress/issue-1105.js
new file mode 100644
index 00000000..4205fdf5
--- /dev/null
+++ b/test/compress/issue-1105.js
@@ -0,0 +1,147 @@
+with_in_global_scope: {
+ options = {
+ unused: true
+ }
+ input: {
+ var o = 42;
+ with(o) {
+ var foo = 'something'
+ }
+ doSomething(o);
+ }
+ expect: {
+ var o=42;
+ with(o)
+ var foo = "something";
+ doSomething(o);
+ }
+}
+with_in_function_scope: {
+ options = {
+ unused: true
+ }
+ input: {
+ function foo() {
+ var o = 42;
+ with(o) {
+ var foo = "something"
+ }
+ doSomething(o);
+ }
+ }
+ expect: {
+ function foo() {
+ var o=42;
+ with(o)
+ var foo = "something";
+ doSomething(o)
+ }
+ }
+}
+compress_with_with_in_other_scope: {
+ options = {
+ unused: true
+ }
+ input: {
+ function foo() {
+ var o = 42;
+ with(o) {
+ var foo = "something"
+ }
+ doSomething(o);
+ }
+ function bar() {
+ var unused = 42;
+ return something();
+ }
+ }
+ expect: {
+ function foo() {
+ var o = 42;
+ with(o)
+ var foo = "something";
+ doSomething(o)
+ }
+ function bar() {
+ return something()
+ }
+ }
+}
+with_using_existing_variable_outside_scope: {
+ options = {
+ unused: true
+ }
+ input: {
+ function f() {
+ var o = {};
+ var unused = {}; // Doesn't get removed because upper scope uses with
+ function foo() {
+ with(o) {
+ var foo = "something"
+ }
+ doSomething(o);
+ }
+ foo()
+ }
+ }
+ expect: {
+ function f() {
+ var o = {};
+ var unused = {};
+ function foo() {
+ with(o)
+ var foo = "something";
+ doSomething(o)
+ }
+ foo()
+ }
+ }
+}
+check_drop_unused_in_peer_function: {
+ options = {
+ unused: true
+ }
+ input: {
+ function outer() {
+ var o = {};
+ var unused = {}; // should be kept
+ function foo() { // should be kept
+ function not_in_use() {
+ var nested_unused = "foo"; // should be dropped
+ return 24;
+ }
+ var unused = {}; // should be kept
+ with (o) {
+ var foo = "something";
+ }
+ doSomething(o);
+ }
+ function bar() {
+ var unused = {}; // should be dropped
+ doSomethingElse();
+ }
+ foo();
+ bar();
+ }
+ }
+ expect: {
+ function outer() {
+ var o = {};
+ var unused = {}; // should be kept
+ function foo() { // should be kept
+ function not_in_use() {
+ return 24;
+ }
+ var unused = {}; // should be kept
+ with (o)
+ var foo = "something";
+ doSomething(o);
+ }
+ function bar() {
+ doSomethingElse();
+ }
+ foo();
+ bar();
+ }
+ }
+} \ No newline at end of file