switch.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. constant_switch_1: {
  2. options = { dead_code: true, evaluate: true };
  3. input: {
  4. switch (1+1) {
  5. case 1: foo(); break;
  6. case 1+1: bar(); break;
  7. case 1+1+1: baz(); break;
  8. }
  9. }
  10. expect: {
  11. bar();
  12. }
  13. }
  14. constant_switch_2: {
  15. options = { dead_code: true, evaluate: true };
  16. input: {
  17. switch (1) {
  18. case 1: foo();
  19. case 1+1: bar(); break;
  20. case 1+1+1: baz();
  21. }
  22. }
  23. expect: {
  24. foo();
  25. bar();
  26. }
  27. }
  28. constant_switch_3: {
  29. options = { dead_code: true, evaluate: true };
  30. input: {
  31. switch (10) {
  32. case 1: foo();
  33. case 1+1: bar(); break;
  34. case 1+1+1: baz();
  35. default:
  36. def();
  37. }
  38. }
  39. expect: {
  40. def();
  41. }
  42. }
  43. constant_switch_4: {
  44. options = { dead_code: true, evaluate: true };
  45. input: {
  46. switch (2) {
  47. case 1:
  48. x();
  49. if (foo) break;
  50. y();
  51. break;
  52. case 1+1:
  53. bar();
  54. default:
  55. def();
  56. }
  57. }
  58. expect: {
  59. bar();
  60. def();
  61. }
  62. }
  63. constant_switch_5: {
  64. options = { dead_code: true, evaluate: true };
  65. input: {
  66. switch (1) {
  67. case 1:
  68. x();
  69. if (foo) break;
  70. y();
  71. break;
  72. case 1+1:
  73. bar();
  74. default:
  75. def();
  76. }
  77. }
  78. expect: {
  79. // the break inside the if ruins our job
  80. // we can still get rid of irrelevant cases.
  81. switch (1) {
  82. case 1:
  83. x();
  84. if (foo) break;
  85. y();
  86. }
  87. // XXX: we could optimize this better by inventing an outer
  88. // labeled block, but that's kinda tricky.
  89. }
  90. }
  91. constant_switch_6: {
  92. options = { dead_code: true, evaluate: true };
  93. input: {
  94. OUT: {
  95. foo();
  96. switch (1) {
  97. case 1:
  98. x();
  99. if (foo) break OUT;
  100. y();
  101. case 1+1:
  102. bar();
  103. break;
  104. default:
  105. def();
  106. }
  107. }
  108. }
  109. expect: {
  110. OUT: {
  111. foo();
  112. x();
  113. if (foo) break OUT;
  114. y();
  115. bar();
  116. }
  117. }
  118. }
  119. constant_switch_7: {
  120. options = { dead_code: true, evaluate: true };
  121. input: {
  122. OUT: {
  123. foo();
  124. switch (1) {
  125. case 1:
  126. x();
  127. if (foo) break OUT;
  128. for (var x = 0; x < 10; x++) {
  129. if (x > 5) break; // this break refers to the for, not to the switch; thus it
  130. // shouldn't ruin our optimization
  131. console.log(x);
  132. }
  133. y();
  134. case 1+1:
  135. bar();
  136. break;
  137. default:
  138. def();
  139. }
  140. }
  141. }
  142. expect: {
  143. OUT: {
  144. foo();
  145. x();
  146. if (foo) break OUT;
  147. for (var x = 0; x < 10; x++) {
  148. if (x > 5) break;
  149. console.log(x);
  150. }
  151. y();
  152. bar();
  153. }
  154. }
  155. }
  156. constant_switch_8: {
  157. options = { dead_code: true, evaluate: true };
  158. input: {
  159. OUT: switch (1) {
  160. case 1:
  161. x();
  162. for (;;) break OUT;
  163. y();
  164. break;
  165. case 1+1:
  166. bar();
  167. default:
  168. def();
  169. }
  170. }
  171. expect: {
  172. OUT: {
  173. x();
  174. for (;;) break OUT;
  175. y();
  176. }
  177. }
  178. }
  179. constant_switch_9: {
  180. options = { dead_code: true, evaluate: true };
  181. input: {
  182. OUT: switch (1) {
  183. case 1:
  184. x();
  185. for (;;) if (foo) break OUT;
  186. y();
  187. case 1+1:
  188. bar();
  189. default:
  190. def();
  191. }
  192. }
  193. expect: {
  194. OUT: {
  195. x();
  196. for (;;) if (foo) break OUT;
  197. y();
  198. bar();
  199. def();
  200. }
  201. }
  202. }
  203. drop_default_1: {
  204. options = { dead_code: true };
  205. input: {
  206. switch (foo) {
  207. case 'bar': baz();
  208. default:
  209. }
  210. }
  211. expect: {
  212. switch (foo) {
  213. case 'bar': baz();
  214. }
  215. }
  216. }
  217. drop_default_2: {
  218. options = { dead_code: true };
  219. input: {
  220. switch (foo) {
  221. case 'bar': baz(); break;
  222. default:
  223. break;
  224. }
  225. }
  226. expect: {
  227. switch (foo) {
  228. case 'bar': baz();
  229. }
  230. }
  231. }
  232. keep_default: {
  233. options = { dead_code: true };
  234. input: {
  235. switch (foo) {
  236. case 'bar': baz();
  237. default:
  238. something();
  239. break;
  240. }
  241. }
  242. expect: {
  243. switch (foo) {
  244. case 'bar': baz();
  245. default:
  246. something();
  247. }
  248. }
  249. }