conditionals.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ifs_1: {
  2. options = {
  3. conditionals: true
  4. };
  5. input: {
  6. if (foo) bar();
  7. if (!foo); else bar();
  8. if (foo); else bar();
  9. if (foo); else;
  10. }
  11. expect: {
  12. foo&&bar();
  13. foo&&bar();
  14. foo||bar();
  15. foo;
  16. }
  17. }
  18. ifs_2: {
  19. options = {
  20. conditionals: true
  21. };
  22. input: {
  23. if (foo) {
  24. x();
  25. } else if (bar) {
  26. y();
  27. } else if (baz) {
  28. z();
  29. }
  30. if (foo) {
  31. x();
  32. } else if (bar) {
  33. y();
  34. } else if (baz) {
  35. z();
  36. } else {
  37. t();
  38. }
  39. }
  40. expect: {
  41. foo ? x() : bar ? y() : baz && z();
  42. foo ? x() : bar ? y() : baz ? z() : t();
  43. }
  44. }
  45. ifs_3_should_warn: {
  46. options = {
  47. conditionals : true,
  48. dead_code : true,
  49. evaluate : true,
  50. booleans : true
  51. };
  52. input: {
  53. if (x && !(x + "1") && y) { // 1
  54. var qq;
  55. foo();
  56. } else {
  57. bar();
  58. }
  59. if (x || !!(x + "1") || y) { // 2
  60. foo();
  61. } else {
  62. var jj;
  63. bar();
  64. }
  65. }
  66. expect: {
  67. var qq; bar(); // 1
  68. var jj; foo(); // 2
  69. }
  70. }
  71. ifs_4: {
  72. options = {
  73. conditionals: true
  74. };
  75. input: {
  76. if (foo && bar) {
  77. x(foo)[10].bar.baz = something();
  78. } else
  79. x(foo)[10].bar.baz = something_else();
  80. }
  81. expect: {
  82. x(foo)[10].bar.baz = (foo && bar) ? something() : something_else();
  83. }
  84. }
  85. ifs_5: {
  86. options = {
  87. if_return: true,
  88. conditionals: true,
  89. comparisons: true,
  90. };
  91. input: {
  92. function f() {
  93. if (foo) return;
  94. bar();
  95. baz();
  96. }
  97. function g() {
  98. if (foo) return;
  99. if (bar) return;
  100. if (baz) return;
  101. if (baa) return;
  102. a();
  103. b();
  104. }
  105. }
  106. expect: {
  107. function f() {
  108. if (!foo) {
  109. bar();
  110. baz();
  111. }
  112. }
  113. function g() {
  114. if (!(foo || bar || baz || baa)) {
  115. a();
  116. b();
  117. }
  118. }
  119. }
  120. }
  121. ifs_6: {
  122. options = {
  123. conditionals: true,
  124. comparisons: true
  125. };
  126. input: {
  127. if (!foo && !bar && !baz && !boo) {
  128. x = 10;
  129. } else {
  130. x = 20;
  131. }
  132. }
  133. expect: {
  134. x = foo || bar || baz || boo ? 20 : 10;
  135. }
  136. }